Sorting CS 105
10/02/05 L12: Sorting Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The Sorting problem Input: a collection S of n elements that can be ordered Output: the same collection of elements arranged in increasing (or non-decreasing) order *typically, S would be stored in an array, and the problem is to rearrange the elements in that array
10/02/05 L12: Sorting Slide 3 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved About sorting In practical contexts, Entry objects are sorted according by its keys As in priority queues and dictionaries, keys can be of any type as long as they can be ordered (e.g., int, String, …) Keys may duplicate, in which case, we assert that the result of sorting be non-decreasing For simplicity, it is sufficient to assume that we are sorting integers (no more entry objects) and that the values are distinct
10/02/05 L12: Sorting Slide 4 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example
10/02/05 L12: Sorting Slide 5 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Sorting algorithms Insertion sort Selection sort Bubble sort Heap sort Merge sort Quick sort Bucket sort Radix sort O( n 2 ) O( n log n ) O( n )
10/02/05 L12: Sorting Slide 6 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Insertion sort Strategy: treat each s[i] as an incoming element that you will insert into the already sorted sequence s[0],s[i],…s[i-1] Requires locating the proper position of the incoming element and adjusting elements to the right Best case: the array is already sorted so that no “insertions” are carried out -> O(n) Worst case: the array is in decreasing order; incoming elements are always inserted at the beginning -> O( n 2 )
10/02/05 L12: Sorting Slide 7 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Insertion sort for i 1 to n-1 do temp s[i] // incoming element j i // adjust elements to the right while ( j > 0 && s[j-1] > temp ) s[j] = s[j-1]; j--; s[j] = temp; // insert incoming element
10/02/05 L12: Sorting Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Insertion sort example
10/02/05 L12: Sorting Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Selection sort Strategy: locate the minimum element, place it at the first position, locate the next minimum and place it at the second position … Requires a scan ( O(n) ) for each of the n elements -> O( n 2 ) best and worst case Variation: can repeatedly select the maximum instead and place it at the last position
10/02/05 L12: Sorting Slide 10 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Selection sort for i 0 to n-2 do lowIndex i// determine for j i+1 to n-1 do// minimum if (s[j] < s[lowIndex] ) lowIndex j swap( s[i], s[lowIndex] ) // place minimum // in proper place Why not n-1 ?
10/02/05 L12: Sorting Slide 11 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Selection sort example
10/02/05 L12: Sorting Slide 12 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Bubble sort Essentially selection sort but the sort is carried out by swapping adjacent elements only Minimum elements are repeatedly “bubbled-up”, maximum elements are repeatedly “bubbled-down” the array O( n 2 ) because of the comparisons (actual swaps are carried out only when elements are out of place)
10/02/05 L12: Sorting Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Bubble Sort for i n-1 down to 1 do for j 0 to i-1 do if (s[j] > s[j+1] ) swap( s[j], s[j+1] ) Puts the ith element in its proper place
10/02/05 L12: Sorting Slide 14 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Exercise: Bubble sort Perform a trace for this array
10/02/05 L12: Sorting Slide 15 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Time complexity summary AlgorithmBest case Worst case Insertion sort O( n )O(n 2 ) Selection sort O(n 2 ) Bubble sortO(n 2 )
10/02/05 L12: Sorting Slide 16 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved What’s next? O( n log n ) algorithms Heap sort: Use heap to determine minimum/ maximum Divide and conquer methods (Quick sort and Merge sort) O( n ) algorithms Not comparison based Assumes some restrictions on the input array