CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER 11
HW4• Passing in command line arguments
• Iterative Merge Sort
• What to turn in
• Other questions, clarifications
• Due date postponed until Friday 2.27
Command lines arguments to a C++ program• Program.exe param1 param2 param3
main(argc, *argv[])
{
}
• argc: number of command line arguments
• argv[]• Array of pointers to characters• argv[0] being the program name• Others each hold a command line argument
• Quick/simple programming example: write a program which outputs the command line arguments if there are 3 of them
int main(int argc, char* argv[]){
if (argc == 4){
for (int i = 1; i < argc; i++){cout << argv[i] << endl;}}
return 0;}
Simple command line arg program
Review: Analysis and Big O Notation◦Algorithm A is order f ( n ), denoted O( f ( n ))
◦ If constants k and n0 exist ◦ Such that A requires no more than k f ( n ) time units to solve a
problem of size n ≥ n0
Big-O class problem(s)• What is the Big-O complexity of an algorithm which requires 7n + 5n3 + 9 operations for a data set of size n.
• Prove your answer.for (int i = 1; i <= n; i++){
for (int j = i; j < i * n; j++){
FuncX();}
}
http://courses.washington.edu/css342/dimpsey/ProgramExamples/BigOwAnswers.pdf
Sorting the Sorts Selection Sort worst/average O(n2) Bubble Sort worst/average O(n2) Insertion Sort worst/average O(n2) Shell Sort worst O(n2)/average O(n3/2) Merge Sort worst/average O(n log n) Quick Sort worst O(n2)/average O(n log n) Radix Sort worst/average O(n)
Sorts previously covered• Bubble Sort: http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort-example-300px.gif
let’s determine why it is O(n2)
• Insertion Sort: http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort-example-300px.gif
let’s determine why it is O(n2)
Efficiency of Bubble Sort: O(n2)
29 10 14 1337
2910 14 1337
2910 14 1337
2910 14 1337
2910 14 13 37
2910 14 13 37
2910 14 13 37
2910 14 13 37
2910 14 13 37
ComparisonSwapping
N-1N-1
N-2N-2
11
……
Pass 1 Pass 2
Efficiency of Insertion Sort: O(n2)
29 10 14 13372910 14 13372910 14 13371410 29 13371410 29 13371410 29 13371410 29 13371310 14 3729
Sorted Unsorted Comp. Shift Insert Operations
1 1 1 2+1
2 1 1 3+1
3 1 1 4+1
2 3 1 5+1
CSS342: SORTING ALGORITHMS 13
MergeSort
1 4 8 13 14 20 25 2 3 5 7 11 23
Key: THE MERGE!Assuming that we have already had two sorted array,How can we merge them into one sorted array?
2 3 5 7 11 23 25201413841
14
first mid last
sorted sorted
first
1
last
1fir
st2
last
2
theArray
tempArray
< >=
inde
x
first midsorted sorted
first
1
last
1
first
2las
t2
theArray
tempArray
inde
x
Computer Scientist of the week(very difficult…. possibly NP-Complete)
Stephen Cook
• Forefather of computational complexity theory• Theory of NP-Completeness• Prof. at University of Toronto• 1982: Turing award winner
void Merge(vector<int> &itemVector, int first, int mid, int last){ int *tempArr; int size = last - first + 1; tempArr = new int[size]; int first1 = first; int last1 = mid; int first2 = mid + 1; int last2 = last; int index = 0; while ((first1 <= last1) && (first2 <= last2)) {
if (itemVector[first1] < itemVector[first2]) {
tempArr[index] = itemVector[first1]; first1++;
} else {
tempArr[index] = itemVector[first2]; first2++;
} index++; }
while (first1 <= last1){ tempArr[index] = itemVector[first1]; first1++; index++;}
while (first2 <= last2){ tempArr[index] = itemVector[first2]; first2++; index++;}
for (index = 0; index < size; index++){ itemVector[first] = tempArr[index]; first++;}
delete[] tempArr;}
Computer Scientist of the weekJohn Von Neumann!
• Innovations in Set theory, Geometry, Quantum Mechanics, Economics, Statistics
• Founded Game Theory• Monte Carlo Method• EDVAC: data and program both in same address space• ENIAC: First computer to use a stored program• Von Neumann Architecture!• Manhattan Project• MAD: Mutually Assured Destruction• Merge Sort Algorithm
MergeSort: successive merges38 16 17123927 24 5
3816 27 39 12 17 245
16 3827 39 5 12 2417
5 12 16 17 24 38 3927
Use recursion to get here
MergeSort
38 16 17123927 24 5
38 16 17123927 24 5
firstmid=(fist + last)/2
last
theArray
38 16 3927 1712 24 5first last first last
mid=(fist + last)/2mid=(fist + last)/2
38 16 3927 1712 24 5first
first
last
last
first < last
MergeSort: Overview 38 16 17123927 24 5first
mid=(fist + last)/2last
theArray
38 16 3927 1712 24 5
38 16 3927 1712 24 5firstlast
38 16 17123927 24 5
3816 27 39 12 17 245
16 3827 39 5 12 2417
5 12 16 17 24 38 3927
void MergeSort(vector<int> &iVector, int first, int last){
if (first < last){
int mid = (first + last) / 2;MergeSort(iVector, first, mid);MergeSort(iVector, mid + 1, last);Merge(iVector, first, mid, last);
}}
http://en.wikipedia.org/wiki/Merge_sort#mediaviewer/File:Merge-sort-example-300px.gif
MergeSort: (Efficiency Analysis)38 16 17123927 24 5
3816 27 39 12 17 245
16 3827 39 5 12 2417
5 12 16 17 24 38 3927
Level # sub-arrays #comparisons # copies per merge
1 4 12 * 2
2 2 34 * 2
3 1 78 * 2
X n/2x 2x-12x * 2 At level X, #nodes in each sub-array = 2x
At level X, # major operations = n/2x * (3 * 2x – 1) = O(3n)#levels = log n, where n = # array elements ( if n is a power of 2 )#levels = log n + 1 if n is not a power of 2# operations = O(3n) * (log n + 1) = O(3 n log n) = O(n log n)