Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.

Similar presentations


Presentation on theme: "1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009."— Presentation transcript:

1 1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009

2 2 Sorting a List: Bubble Sort Suppose list[0]...list[n - 1] is a list of n elements, indexed 0 to n – 1 Bubble sort algorithm: In a series of n - 1 iterations, compare successive elements, list[index] and list[index + 1] If list[index] is greater than list[index + 1], then swap them

3 3

4 4

5 5 Bubble Sort Implementation After one pass, the largest item must be the last in the list Start at the front again: the second pass will bring the second largest element into the second last position Repeat n – 1 times, after which, all entries will be in place

6 6 Bubble Sort Implementation Round 1: Consider elements from a[0] to a[n-1] Compare each pair of adjacent element (n-1 comparisons) For each pair of adjacent element, if the first one is greater then the second one, swap; Round 2: Consider elements from a[0] to a[n-2] Compare each pair of adjacent element (n-2 comparisons) For each pair of adjacent element, if the first one is larger then the second one, swap; … Round n-1: Consider elements from a[0] to a[1] (1 comparison) Compare each pair of adjacent element For each pair of adjacent element, if the first one is larger then the second one, swap;

7 7 Bubble Sort Implementation for(int i=1; i<=n-1;i++) Consider elements from a[0] to a[n-i] Compare each pair of adjacent element For each pair of adjacent element, if the first one is larger then the second one, swap;

8 8 Bubble Sort Implementation for(int i=1; i<=n-1;i++) for(int j=0; j<n-i;j++) if a[j]>a[j+1] swap a[j] and a[j+1]

9 9 Bubble Sort Implementation for(int i=1; i<=n-1;i++) for(int j=0; j<n-i;j++) if (a[j]>a[j+1]) //swap a[j] and a[j+1] { double tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; }

10 10 Bubble Sort Run-Time Analysis for(int i=1; i<=n-1;i++) for(int j=0; j<n-i;j++) if (a[j]>a[j+1]) //n-i comparisons //swap a[j] and a[j+1] { double tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; } Total comparisons: n-1+n-2+…+2+1=n(n-1)/2 So the time complexity is O(n 2 ). Because the bubble sort simply swaps adjacent entries, it cannot be any better than insertion sort which does n + d comparisons where d is the number of inversions

11 11 Bubble Sort Improvement: Flagged Bubble Sort One useful modification would be to check if no swaps occur: if no swaps occur, the list is sorted in this example, no swaps occurred during the 5 th pass Use a Boolean flag to check if no swaps occurred

12 12 Bubble Sort Improvement: Flagged Bubble Sort for(int i=1; i<=n-1;i++) { bool sorted=true; for(int j=0; j<n-i;j++) if (a[j]>a[j+1]) //swap a[j] and a[j+1] { sorted=false; double tmp=a[j]; a[j]=a[j+1]; a[j+1]=tmp; } if (sorted) break; }

13 13 Flagged Bubble Sort Run-Time Analysis The following table summarizes the run-times of flagged bubble sort Case Run Time Comments Worst (n2)(n2) Reverse sorted Average O(n + d) Slower than insertion Best (n)(n) Already sorted

14 14 Sorting a List: Selection Sort Selection sort: rearrange list by selecting an element and moving it to its proper position Find the smallest (or largest) element and move it to the beginning (end) of the list On successive passes, locate the smallest item in the list starting from the next element

15 15 Selection Sort Implementation Round 1: Consider elements from a[0] to a[n-1] Find the index of smallest element smallIndex (n-1 comparisons) Swap a[0] and a[smallIndex]; Round 2: Consider elements from a[1] to a[n-1] Find the index of smallest element smallIndex (n-2 comparisons) Swap a[1] and a[smallIndex]; … Round n-1: Consider elements from a[n-2] to a[n-1] Find the index of smallest element smallIndex(1 comparison) Swap a[n-2] and a[smallIndex];

16 16 Selection Sort Implementation for(int i=1; i<=n-1;i++) Consider elements from a[i-1] to a[n-1] Find the index of smallest element smallIndex Swap a[i-1] and a[smallIndex]; or for(int i=0; i<n-1;i++) Consider elements from a[i] to a[n-1] Find the index of smallest element smallIndex Swap a[i] and a[smallIndex];

17 17 void selectionSort(int list[], int length) { int i; int smallestIndex; int minIndex; int temp; for (i = 0; i < length - 1; i++) { //Step a smallestIndex = i; for (minIndex = i + 1; minIndex < n; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; //Step b temp = list[smallestIndex]; list[smallestIndex] = list[i]; list[i] = temp; } Selection Sort Algorithm for (i = 0; i < n - 1; i++) { a.Find the location, smallestIndex, of the smallest element in list[i]…list[n-1]. a.Swap the smallest element with list[i]. That is, swap list[smallestIndex] with list[i] }

18 18 Selection Sort Implementation Round 1: Consider elements from a[0] to a[n-1] Find the index of smallest element smallIndex (n-1 comparisons) Swap a[0] and a[smallIndex]; Round 2: Consider elements from a[1] to a[n-1] Find the index of smallest element smallIndex (n-2 comparisons) Swap a[1] and a[smallIndex]; … Round n-1: Consider elements from a[n-2] to a[n-1] Find the index of smallest element smallIndex(1 comparison) Swap a[n-2] and a[smallIndex]; Total comparisons: n-1+n-2+…+2+1=n(n-1)/2 So the time complexity is O(n 2 ).

19 19 Bubble Sort and selection sorting References [1]Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, 2 nd Ed., Addison Wesley, 1998, §5.2.

20 20 End of lecture Thank you!


Download ppt "1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009."

Similar presentations


Ads by Google