Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER 11.

Similar presentations


Presentation on theme: "CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER 11."— Presentation transcript:

1 CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER 11

2 Agenda HW 4 Review Big O notation. Sorts, MergeSort Hand Back Midterm

3 HW4 Passing in command line arguments Iterative Merge Sort What to turn in Other questions, clarifications Due date postponed until Friday 2.27

4 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

5 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

6 Review: Analysis and Big O Notation ◦Algorithm A is order f ( n ), denoted O( f ( n )) ◦If constants k and n 0 exist ◦Such that A requires no more than k  f ( n ) time units to solve a problem of size n ≥ n 0

7 Big-O class problem(s) What is the Big-O complexity of an algorithm which requires 7n + 5n 3 + 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

8 Sorts and sorting

9 Sorting the Sorts Selection Sortworst/average O(n 2 ) Bubble Sortworst/average O(n 2 ) Insertion Sortworst/average O(n 2 ) Shell Sortworst O(n 2 )/average O(n 3/2 ) Merge Sortworst/average O(n log n) Quick Sortworst O(n 2 )/average O(n log n) Radix Sortworst/average O(n)

10 Sorts previously covered Bubble Sort: http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort- example-300px.gif http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort- example-300px.gif  let’s determine why it is O(n 2 ) Insertion Sort: http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort- example-300px.gif http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort- example-300px.gif  let’s determine why it is O(n 2 )

11 Efficiency of Bubble Sort: O(n 2 ) 2910141337 2910141337 2910141337 2910141337 2910141337 2910141337 2910141337 2910141337 29 10141337 Comparison Swapping N-1 N-2 1111 ………… Pass 1Pass 2

12 Efficiency of Insertion Sort: O(n 2 ) 29 10141337 2910 141337 2910 141337 141029 1337 141029 1337 141029 1337 141029 37 1310143729 Sorted Unsorted Comp.ShiftInsert Operations 111 2+1 211 3+1 311 4+1 2 3 1 5+1

13 CSS342: SORTING ALGORITHMS13 MergeSort 1481314202523571123 Key: THE MERGE! Assuming that we have already had two sorted array, How can we merge them into one sorted array? 2357112325201413841

14 14 firstmid last sorted first1 last1 first2 last2 theArray tempArray < >= index firstmid sorted first1 last1 first2 last2 theArray tempArray index

15 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

16 void Merge(vector &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++; }

17 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; }

18 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

19 MergeSort: successive merges 381617123927245 381627391217245 163827395122417 512161724383927 Use recursion to get here

20 MergeSort 381617123927245 381617123927245 first mid=(fist + last)/2 last theArray 381639271712245 firstlastfirstlast mid=(fist + last)/2 38 16 39271712245 first last first < last

21 MergeSort: Overview 381617123927245 first mid=(fist + last)/2 last theArray 381639271712245 381639271712245 firstlast 381617123927245 381627391217245 163827395122417 512161724383927 void MergeSort(vector &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

22 MergeSort: (Efficiency Analysis) 381617123927245 381627391217245 163827395122417 512161724383927 Level# sub-arrays#comparisons # copies per merge 141 2 * 2 223 4 * 2 317 8 * 2 Xn/2 x 2 x -1 2 x * 2 At level X, #nodes in each sub-array = 2 x At level X, # major operations = n/2 x * (3 * 2 x – 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)


Download ppt "CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 12. 150223. CARRANO CHAPTER 11."

Similar presentations


Ads by Google