Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms.

Similar presentations


Presentation on theme: "1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms."— Presentation transcript:

1 1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms

2 2 Remember Bubble Sort? void bubble_sort (int list[], int size) // Sort list[0]..list[size-1] in increasing order { int pass, cell_to_fill; for (pass = 1; pass < size; pass++) // At start of pass list[ctf+1]..list[size-1] are sorted { cell_to_fill = size - pass; for (int i = 0; i < cell_to_fill; i++) if (list[i] > list[i+1]) swap (list[i], list[i+1]); // At end of pass list[ctf]..list[size-1] are sorted }

3 3 How Much Work is Bubble Sort? The number of key comparisons for each iteration: → bubble sort is O(n 2 )

4 4 Selection Sort Sorts a list by –finding smallest (or equivalently largest) element in the list –moving it to the beginning (or end) of the list by swapping it with element in beginning (or end) position Part of class orderedArrayListType: template class orderedArrayListType: public arrayListType { public: void selectionSort();... };

5 5 Selection Sort Example:

6 6 Selection Sort Next Step: The number of key comparisons for each iteration: → selection sort is O(n 2 ):

7 7 Selection Sort Function template void orderedArrayListType ::selectionSort() { int loc, minIndex; for(loc = 0; loc < length - 1; loc++) { minIndex = minLocation(loc, length - 1); swap(loc, minIndex); } } Appreciate how much simpler the two functions make this

8 8 Smallest Element in List Function template int orderedArrayListType ::minLocation(int first, int last) { int loc, minIndex; minIndex = first; for(loc = first + 1; loc <= last; loc++) if(list[loc] < list[minIndex]) minIndex = loc; return minIndex; }//end minLocation Swap Function template void orderedArrayListType ::swap(int first, int second) { elemType temp; temp = list[first]; list[first] = list[second]; list[second] = temp; }//end swap

9 9 Insertion Sort Reduces number of key comparisons made in selection sort Can be applied to both arrays and linked lists (examples follow) Sorts list by –finding first unsorted element in list –moving it to its proper position

10 10 Insertion Sort Example

11 11 Insertion Sort Example

12 12 Insertion Sort Code: Array-Based Lists template void orderedArrayListType ::insertionSort() { int firstOutOfOrder, location; elemType temp; for(firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++) if(list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; }while(location > 0 && list[location - 1] > temp); list[location] = temp; } }//end insertionSort

13 13 Comparison Selection Sort:Insertion Sort: 40 25 57 6 91 72 3 9 5 77 3 25 57 6 91 72 40 9 5 77 3 5 6 57 91 72 40 9 25 77 3 5 6 9 91 72 40 57 25 77 3 5 6 9 25 40 57 72 91 77 3 5 57 6 91 72 40 9 25 77 3 5 6 9 25 40 57 72 91 77 3 5 6 9 25 40 57 72 77 91 40 25 57 6 91 72 3 9 5 77 25 40 57 6 91 72 3 9 5 77 3 5 6 9 25 40 57 72 77 91 6 25 40 57 91 72 3 9 5 77 6 25 40 57 72 91 3 9 5 77 3 6 25 40 57 72 91 9 5 77 3 6 9 25 40 57 72 91 5 77 3 5 6 9 25 40 57 72 91 77

14 14 Analysis: Insertion and Selection Sort

15 15 Insertion Sort: Linked List-Based List Same idea: find the first element out of place find where it goes move it Is this harder or easier than with arrays? harder: messy link stuff easier: less moving around

16 16 Quick Sort Recursive algorithm using divide-and-conquer Partition into two sublists–first list elements smaller than those in the second Sort the two sublists List before partitioning using 52 as pivot List after partitioning, pivot position 6 is returned as a parameter Next?

17 17 Quick Sort: Array-Based Lists template int orderedArrayListType ::partition(int first, int last) //returns the position of the pivot element { //separates into 2 sublists //returns the array index of the pivot element }

18 18 Quick Sort: Array-Based Lists template void orderedArrayListType ::recQuickSort(int first, int last) { int pivotLocation; if(first <last) { pivotLocation = partition(first, last); recQuickSort(first, pivotLocation - 1); recQuickSort(pivotLocation + 1, last); } } //end recQuickSort template void orderedArrayListType ::quickSort() { recQuickSort(0, length - 1); }//end quickSort

19 19 Analysis of Quick Sort How many iterations of partitioning? –we halve the size each step –~ log(n)) iterations How much work for each iteration? –iteration 1: "process" each of the n elements = n steps –iteration 2: "process" n/2 elements for each half = n steps –iteration 4: "process" n/4 elements for each quarter = n steps –looks like it is n steps for each iteration n * log(n) = O(n log(n))

20 20 Merge Sort Also uses divide-and-conquer to sort a list Algorithm –partition the list into two sublists –sort the sublists –combine the sorted sublists into one sorted list

21 21 Merge Sort Algorithm

22 22 Efficiency O(n 2 ): –bubble sort –selection sort –insertion sort O(n log n): –quick sort –merge sort –heap sort (later after we study trees)


Download ppt "1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms."

Similar presentations


Ads by Google