# Sort - CSS342 Sorting Algorithms Professor Munehiro Fukuda...

This preview shows pages 1–9. Sign up to view the full content.

CSS342: Sorting Algorithms 1 CSS342: Sorting Algorithms Professor: Munehiro Fukuda

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CSS342: Sorting Algorithms 2 Why We Desperately Need Efficient Sorting Algorithms? Data must be sorted before we run the following programs: Search algorithms such as binary search and interpolation search Many computational geometry/graphics algorithm such as the convex hull We always or frequently need to sort the following data: Dictionary White/yellow pages Student grades
CSS342: Sorting Algorithms 3 Topics Day 1: Lecture Selection Sort worst/average O(n 2 ) Bubble Sort worst/average O(n 2 ) Insertion Sort worst/average O(n 2 ) Shell Sort worst O(n 2 ) average O(n 3/2 ) Merge Sort worst/average O(n log n) Quick Sort worst O(n 2 ) average O(n log n) Radix Sort worst/average O(n) Day 2: Lab Work Partial Quick Sort Homework Assignment Non-recursive Semi-In-Place Merge Sort

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CSS342: Sorting Algorithms 4 Selection Sort Initial array After 1 st swap After 2 nd swap After 3 rd swap After 4 th swap 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 Scan item 0 to size-1 , locate the largest item, and swap it with the rightmost item. Scan item 0 to size-2 , locate the 2nd largest item, and swap it with the 2nd rightmost item. Scan item 0 to size-3 , locate the 3rd largest item, and swap it with the 3rd rightmost item. Scan item 0 to size-4 , locate the 4th largest item, and swap it with the 4th rightmost item. Scan item 0 to size-5 , locate the 5 th largest item, and swap it with the 5th rightmost item. 0 size-1 O(n 2 ) sorting
CSS342: Sorting Algorithms 5 Selection Sort template <class Object> void selectionSort( vector<Object> & a ) { for ( int last = a.size( ) - 1; last >= 1; --last ) { int indexSoFar = 0; // Index of largest item found so far. // Assume 0th item is the largest first for ( int i = 1; i <= last; ++i ) { if ( a[i] > a[indexSoFar] ) indexSoFar = i; } // indexSoFar points to the largest item at this point swap( a[indexSoFar], a[last] ); } } last = a.size( ) - 1 last last 0 0 0 indexSoFar swap swap swap indexSoFar indexSoFar O(n 2 ) sorting

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CSS342: Sorting Algorithms 6 Efficiency of Selection Sort 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 Initial array After 1 st swap After 2 nd swap After 3 rd swap After 4 th swap N-1 (=4) 1 N-2 (=3) 1 N-3 (=2) 1 N-4 (=1) 1 Comparisons Swapping O(n(n-1)/2) O(n-1) O(n 2 ) O(n 2 ) sorting
CSS342: Sorting Algorithms 7 Bubble Sort 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 37 29 10 14 13 37 37 29 10 14 13 37 37 29 10 14 13 37 37 29 29 10 14 13 37 37 Pass 1 Pass 2 13 10 14 29 29 37 37 13 10 14 29 29 37 37 29 29 10 13 14 14 37 37 Pass 3 14 14 10 13 29 29 37 37 29 29 10 13 13 14 14 37 37 Pass 4 O(n 2 ) sorting

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CSS342: Sorting Algorithms 8 Bubble Sort 15 10 index nextIndex swap #include <iostream> #include <vector> #include <string> using namespace std; template <class Object> void bubbleSort( vector<Object> & a ) { bool swapOccurred = true; // true when swaps occur for ( int pass = 1; ( pass < a.size( ) ) && swapOccurred; ++pass ) { swapOccurred = false; // swaps have not occurred at the beginning for ( int i = 0; i < a.size( ) - pass; ++i ) { // a bubble(i) goes from 0 to size - pass if ( a[i] > a[i + 1] ) { swap( a[i], a[i + 1] ); swapOccurred = true; // a swap has occured } } } } O(n 2 ) sorting
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 36

Sort - CSS342 Sorting Algorithms Professor Munehiro Fukuda...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online