Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.

Similar presentations


Presentation on theme: "Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion."— Presentation transcript:

1 Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

2 Lecture Objectives Learn how to implement the simple sorting algorithms (selection, bubble and insertion) To learn how to estimate and compare the performance of basic sorting algorithms To appreciate that algorithms for the same task can differ widely in performance

3 Bubble Sort The idea: Make repeated passes through a list of items, exchanging adjacent items if necessary. At each pass, the largest unsorted item will be pushed in its proper place. Stop when no more exchanges were performed during the last pass.

4 Bubble Sort 11 23 2 56 9 8 10 100 21 2 23 56 9 8 10 100 31 2 23 9 56 8 10 100 41 2 23 9 8 56 10 100 51 2 23 9 8 10 56 100 ---- finish the first traversal ---- 11 2 23 9 8 10 56 100 21 2 9 23 8 10 56 100 31 2 9 8 23 10 56 100 41 2 9 8 10 23 56 100 ---- finish the second traversal ---- …

5 Bubble Sort Algorithm private int[] a = new int[100]; int x; public void sortArray() { int i; int j; int temp; for( i = (x - 1); i >= 0; i-- ) { for( j = 1; j <= i; j++ ) { if( a[ j-1] > a[ j ] ) { temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; }

6 Run time efficiency of bubble sort Two operations affect the run time efficiency of the bubble sort the most:  the comparison operation in the inner loop, and  the exchange operation also in the inner loop. Therefore, we must say how efficient the bubble sort is. each of the two operations. 1. Efficiency of the number of comparisons:  during the first iteration of the outer loop, in the inner loop (N - 1 comparisons);  during the second iteration of the outer loop: (N - 2 comparisons); .........  during the (N - 1)-th iteration of the outer loop: 1 comparison. Total number of comparisons: (N - 1) + (N - 2) +... + 2 + 1 = (N * (N - 1)) / 2 = (N^2 - N) / 2 < N^2 Bubble sort is O(N^2) algorithm. ( the number of comparisons.)

7 Run time efficiency of bubble sort (cont.) 2. Efficiency of the number of exchanges:  during the first iteration of the outer loop, in the inner loop: at most (N - 1 exchanges);  during the second iteration of the outer loop: at most (N - 2 exchanges); .........  during the (N - 1)-th iteration of the outer loop: at most 1 exchange. Total number of exchanges: (N - 1) + (N - 2) +... + 2 + 1 = (N * (N - 1)) / 2 = (N^2 - N) / 2 < N^2 Bubble sort is O(N^2) algorithm of the number of exchanges. Note that only one pass through the inner loop is required if the list is already sorted. That is, bubble sort is sensitive to the input, and in the best case (for sorted lists) it is O(N) algorithm of the number of comparisons, and O(1) of the number of exchanges.

8 Where we use Bubble Sort? This algorithm's average and worst case performance is O(n 2 ), so it is rarely used to sort large, unordered, data sets. Bubble sort can be used to sort a small number of items (where its inefficiency is not a high penalty). Bubble sort may also be efficiently used on a list that is already sorted except for a very small number of elements.

9 Practice (Lab Work) Increase the bubble sort algorithm efficiency so that the loop ends if there is no more exchange.

10 Selection Sort Algorithm List is sorted by selecting list element and moving it to its proper position Algorithm finds position of smallest element and moves it to top of unsorted portion of list Start with the 1st element, scan the entire list to find its smallest element and exchange it with the 1st element Start with the 2 nd element, scan the remaining list to find the the smallest among the last (N-1) elements and exchange it with the 2 nd element Repeats process above until entire list is sorted

11 11 Selection sort

12 Selection Sort Algorithm (Cont’d) public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; }

13 Note that the particular arrangement of values in the array does not affect the amount of work done. Even if the array is in sorted order before the call to Selection Sort, the function still makes n(n-1)/2 comparisons. Selection Sort Runtime Selection Sort runtime 8 Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

14 Selection Sort Runtime (cont…) Best case: Already sorted  Passes: (n - 1 )  Comparisons each pass: (n-k) where k pass number.  Number of comparisons: (n - 1 ) + (n - 2 ) + (n - 3 ) + … + 1 = n(n-1)/2 = n 2 /2 - n/2 = O(n 2 ) Worst case: Same. Number of exchanges:  Always (n - 1 ) (better than Bubble Sort). 9 Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7) Selection Sort Runtime

15 Insertion Algorithm In each pass of an insertion sort, one or more pieces of data are inserted into their correct location in an ordered list.In each pass of an insertion sort, one or more pieces of data are inserted into their correct location in an ordered list. Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly sorted lists, and often is used as part of more sophisticated algorithms.

16 Data Structures: A Pseudocode Approach with C, Second Edition 16 Straight insertion sort example

17 public void InsertionSort() { for (int i = 1; i < size; i++) { int temp = a[i]; // slide elements down to make room for a[i] int j = i; while (j > 0 && a[j - 1] > temp) { a[j] = a[j - 1]; j--; } a[j] = temp; } Insertion Sort code 17 Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

18 Best case: Already sorted  O(n) comparisons. Worst case: O(n 2 ) comparisons. Number of exchanges:  Best case: O(n).  Worst case: O(n 2 ). In practice, best for small sets ( < 30 items). Very efficient on nearly-sorted inputs. Insertion Sort Runtime 18 Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

19 Comparing Simple Sorts We've seen "simple" sorting algorithms, such as:  Bubble sort, Selection sort, and Insertion sort. They all use nested loops and perform approximately n 2 comparisons. They are relatively inefficient. ComparisonsSwaps Bubble Worst n(n-1)/2 Best n-1 Selection n(n-1)/2n-1 Insertion Worst n(n-1)/2 Best n-1 19 Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)


Download ppt "Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion."

Similar presentations


Ads by Google