Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 14. 150302. CARRANO CHAPT 12.

Similar presentations


Presentation on theme: "CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 14. 150302. CARRANO CHAPT 12."— Presentation transcript:

1 CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 14. 150302. CARRANO CHAPT 12

2 Agenda Lab 3: how to run the test cases Lab 5: Preview Sorts Shell Sort Radix Sort Sort Summary Complexity theory example.

3 Lab 5 The Jolly Banker Purpose: Learn Queues Learn Binary Search Trees Practice class design Two phase project Design Review: Wednesday, 3/4. Lab turned in 3/13.

4 Sorts and sorting

5 Sorting the Sorts Selection Sortworst/average O(n2) 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(n2)/average O(n log n) Radix Sortworst/average O(n)

6 The Quick Sort Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

7 Quicksort: Recursive Overview void quicksort(vector &a, int first, int last) { int pivotIndex; if ( first < last ) { pivotIndex = choosePivot(a, first, last); partition( a, fist, last, pivotIndex ); quicksort( a, first, pivotIndex - 1 ); quicksort( a, pivotIndex + 1, last ); }

8 Quicksort: Efficiency Analysis Worst case: If the pivot is the smallest item in the array segment, S1 will remain empty. ◦S2 decreases in size by only 1 at each recursive call. ◦Level 1 requires n-1 comparisons. ◦Level 2 requires n-2 comparisons. ◦Thus, (n-1) + (n-2) + …. + 2 + 1 = n(n-1)/2 = O(n 2 ) Average case: S1 and S2 contain the same number of items. ◦log n or log n + 1 levels of recursions occur. ◦Each level requires n-k comparisons ◦Thus, at most (n-1) * (log n + 1) = O(n log n )

9 Shell Sort Generalization of the Insertion Sort Optimized to reduce data movement Developed 1959 by Donald Shell Choose an Interleave/gap size (n) and sort the arrays chosen by that size This moves data large distances quickly Complexity has not been fully determined Depends on gap size (see appendix) Works best on partially sorted data

10 Shell Sort Example Using gaps of size 5, 3, 1.

11 Computer Scientist of last week Andrew Tanenbaum Professor Vrije Universiteit, Amsterdam Minix, father of Microkernel Famous Tanenbaum-Tovalds debate Recognized for creating computer science textbooks

12 8120388785157541582895173512961194 0 16 8195173512961194 3887851575415828 20 gap = 17/2 = 8 81 95 173512 96 11 94 38 878515 75 41 58 28 20 gap = 8/2.2 = 3 8195 17 3512 96 11 94 38 878515 75 41 58 28 20 81 95 17 35 12 96 11 94 38 87 85 15 75 41 58 28 20 gap = 3/2.2 = 1 81 95 17 35 12 96 11 94 38 87 85 15 75 41 58 28 20 81 95 17 35 12 96 11 94 38 87 85 15 75 41 58 28 20 sort

13 for (int gap = size / 2; gap > 0; gap = (gap == 2) ? 1 : int(gap / 2.2)) { for (int i = gap; i < size; i++) { int tmp = arr[i]; int j = i; for ( ; (j >= gap) && (tmp < arr[j - gap]); j -= gap) { arr[j] = arr[j - gap]; } arr[j] = tmp; } http://en.wikipedia.org/wiki/Shellsort#mediaviewer/File:Sorting_shellsort_anim.gif

14 Mind the Gap

15 Mind the Gap (more)

16 The Radix Sort Uses the idea of forming groups, then combining them to sort a collection of data. Consider collection of three letter groups ABC, XYZ, BWZ, AAC, RLT, JBX, RDT, KLT, AEO, TLJ Group strings by rightmost letter (ABC, AAC) (TLJ) (AEO) (RLT, RDT, KLT) (JBX) (XYZ, BWZ) Combine groups ABC, AAC, TLJ, AEO, RLT, RDT, KLT, JBX, XYZ, BWZ DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

17 The Radix Sort Group strings by middle letter (AAC) (A B C, J B X) (R D T) (A E O) (T L J, R L T, K L T) (B W Z) (X Y Z) Combine groups AAC, ABC, JBX, RDT, AEO, TLJ, RLT, KLT, BWZ, XYZ Group by first letter, combine again ( A AC, A BC, A EO) ( B WZ) ( J BX) ( K LT) ( R DT, R LT) ( T LJ) ( X YZ) Sorted strings AAC, ABC, AEO, BWZ, JBX, KLT, RDT, RLT, TLJ, XYZ DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

18 Radix Sort: Overview) 01232154022200040283156010612150Original integers 15602150106102220123028321540004Grouped by 4 th digit 15602150106102220123028321540004Combined 00040222012321502154156010610283Grouped by 3 rd digit 00040222012321502154156010610283Combined 00041061012321502154022202831560Grouped by 2 nd digit 00041061012321502154022202831560Combined 00040123022202831061156021502154Grouped by 1 st digit 00040123022202831061156021502154Combined (sorted)

19 Radix Sort (Efficiency Analysis) Each grouping work requires n shuffles. # grouping and combining steps is # digits. ◦The previous case is 4. Thus, for k digit number, the performance is: ◦K * n = O( n )where k is irrelevant to n Disadvantage: ◦Memory inefficient ◦K is not really a constant ◦Need to compare digits in the same order rather than items ◦Need to accommodate 10 groups for numbers ◦Need to accommodate 27 groups for strings (alphabet + blank)

20 Write a member function, MoveToEnd which takes in an item by value, finds the first occurrence and moves it to the end of the list. Return false if item could not be found; otherwise true.

21 Efficiency comparisons http://www.sorting-algorithms.com/ https://www.youtube.com/watch?v=kPRA0W1kECg

22 STL WHY REINVENT THE WHEEL?

23 STL Sequence Containers: the Big 3 (recap) Vector Flexibly sized array Access any element in constant time (index into array) Add/Remove from the end of array Data kept contiguous in memory Deque Double ended queue Can add/look from front or back Access any element in constant time Not guaranteed to be contiguous in memory List Linked list Need iterator to traverse Can add anywhere in list in constant time

24 Recall the stack Last In First Out (LIFO) We implemented with following structures: Array Linked List (aStack.push(newItem)).pop() is equal to aStack STL has a stack implementation as a Container Adapter Container adapter on vector, deque, or list Default is deque Functions: empty, size, push, pop, top push pop

25 #include int main( ) { stack aStack; stack > bStack; for (int i = 0; i < 3; i++) { bStack.push(i); } cout << "stack size is: " << bStack.size() << endl; cout << "top element is: " << bStack.top() << endl; bStack.pop(); cout << "Popped! " << endl; cout << "top element is: " << bStack.top() << endl; return 0; }


Download ppt "CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 14. 150302. CARRANO CHAPT 12."

Similar presentations


Ads by Google