CSS 342 DATA ST R UC TURES, A LG OR ITHMS, A N D DI S CR ETE M AT HEMATI CS I LEC T UR E 1 2 . 1 5 0 2 23. CA R R A NO CHA PT ER 1 1 Agenda • HW 4 • Review Big O notation. • Sorts, MergeSort • Hand Back Midterm 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 Simple command line arg program int main(int argc, char* argv[]) { if (argc == 4) { for (int i = 1; i < argc; i++) { cout << argv[i] << endl; } } return 0; } 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 Sorts and sorting 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-sortexample-300px.gif let’s determine why it is O(n2) • Insertion Sort: http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sortexample-300px.gif let’s determine why it is O(n2) Efficiency of Bubble Sort: 2 O(n ) Pass 1 29 10 14 37 13 Pass 2 10 14 29 13 37 10 29 14 37 13 10 14 29 13 37 10 14 29 37 13 10 14 29 13 37 10 14 29 37 13 10 14 13 29 37 10 14 29 13 37 Comparison Swapping N-1 N-1 N-2 N-2 … … 1 1 Efficiency of Insertion Sort: O(n2) Sorted Unsorted 29 10 14 37 13 10 29 14 37 13 Comp. Shift Insert Operations 1 1 1 2+1 2 1 1 3+1 3 1 1 4+1 2 3 1 5+1 10 29 14 37 13 10 14 29 37 13 10 14 29 37 13 10 14 29 37 13 10 14 29 37 13 10 13 14 29 37 MergeSort Key: THE MERGE! Assuming that we have already had two sorted array, How can we merge them into one sorted array? 1 1 4 2 8 3 13 14 20 25 4 5 7 8 2 3 5 7 11 23 11 13 14 20 23 25 CSS342: SORTING ALGORITHMS 13 sorted mid first1 < last2 last last1 first2 first theArray sorted >= index tempArray sorted last1 first2 last2 mid first1 first theArray sorted 14 index tempArray 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 week John 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 merges 38 16 27 39 12 17 24 5 16 38 27 39 12 17 5 24 16 27 38 39 5 12 17 24 5 12 16 17 24 27 38 39 Use recursion to get here MergeSort mid=(fist + last)/2 last first 38 16 27 39 12 17 24 5 theArray mid=(fist + last)/2 mid=(fist + last)/2 first last first last first < last 38 16 27 39 12 17 24 5 first last 38 16 27 39 12 17 24 5 first last 38 16 27 39 12 17 24 5 MergeSort: Overview 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); } } mid=(fist + last)/2 first last theArray 38 16 27 39 12 17 24 5 38 16 27 39 firstlast 38 16 38 16 16 38 27 39 27 27 39 16 27 38 39 http://en.wikipedia.org/wiki/Merge_sort#mediaviewer/File:Mergesort-example-300px.gif 39 12 17 24 5 12 17 12 24 5 17 12 17 24 5 24 5 12 17 24 5 12 16 17 24 27 38 39 5 MergeSort: (Efficiency Analysis) Level 1 2 3 X # sub-arrays 4 2 1 n/2x #comparisons # copies per merge 38 1 2*2 16 38 3 4*2 7 8*2 2x-1 2x * 2 16 27 39 27 39 16 27 38 39 12 17 12 17 24 5 5 24 5 12 17 24 5 12 16 17 24 27 38 39 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)
© Copyright 2024 Paperzz