Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elementary Sorting 30 January 2003. 2 Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j <= n; j++) if (List[i] > List[j])

Similar presentations


Presentation on theme: "Elementary Sorting 30 January 2003. 2 Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j <= n; j++) if (List[i] > List[j])"— Presentation transcript:

1 Elementary Sorting 30 January 2003

2 2 Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j <= n; j++) if (List[i] > List[j]) swap(List[i], List[j]); Simple Sort

3 3 The algorithm starts with the first element of the list. The first element is compared with the second, third and all subsequent elements. If any of these other elements is less than the current first element, then the first element is swapped with that element. Eventually, after the last element of the list is considered, and swapped if need be, then the first element has the smallest element in the list. We then repeat with the second, third and all subsequent list elements. When the last element is handled in this manner, the sort is done.

4 4 Analysis of Simple Sort First element is compared to each of the remaining N-1 elements (where N is the total number of elements. Algorithm performs N-1 steps to move the smallest element to the front of the list. The second element needs to be compared with the remaining N-2 elements of the list Algorithm will perform N-2 steps for second. To process the entire list the total number of steps performed is: (N-1)+(N-2)+(N-3)+...+1 = N(N-1)/2 or O(N 2 ) steps.

5 5 Bubble Sort // List is an integer array of size == n for (i = (n-1); i >= 1; i--) for (j = 0; j <= (i-1); j++) if (List[j] > List[j+1]) swap(List[j], List[j+1]); Bubble Sort Animation

6 6 Bubble Sort Consider the pivot element to be the last element of the list. Then consider the rest of the list, starting from element 0. Every element's magnitude is considered vis-a-vis its immediately succeeding neighbor, e.g., element 0's magnitude vis-a-vis element 1's magnitude, element 1's vis-a-vis element 2's, and so on. If the current element's magnitude is greater than its immediately succeeding neighbor's magnitude, then their magnitudes are swapped. This process continues, over and over again, with the pivot element moving to the element just previous to its current value.

7 7 Analysis of Bubble Sort Bubble Sort makes at most (N-1) passes over the data. The first pass compares (N-1) pairs of elements and moves the largest element to the largest array position. The second pass works on the remaining (N- 1)-element array and makes (N-2) comparisons. Thus, in the worst case this method makes (N- 1) + (N-2) +... + 2 + 1 = N*(N-1)/2 = O(N 2 ) Thus Bubble Sort is an O(N 2 ) algorithm.

8 8 Smart Bubble Sort // List is an integer array of size == n swapped = true; // boolean variable i = n-1; while (swapped && (i >= 1)) { swapped = false; for (j = 0; j <= (i-1); j++) if (List[j] > List[j+1]) { swap(List[j], List[j+1]); swapped = true; // swapping happened } i--; } Bubble Sort (Smart)

9 9 Smart Bubble Sort This is a more efficient version. If, for a particular pass through the list, the sorter can detect that no swaps have been made, then it concludes the sorting is done and terminates. The first version keeps repeating the passes over and over again until the pivot element moves to the front of the list, and does not recognize that sorting is done if no swaps are made in the most recent pass.

10 10 Selection Sort // List is an array of size == n for (i = 0; i < (n - 1); i++) { min_index = i; for (j = (i + 1); j < n; j++) if (List[j] <= List[min_index]) min_index = j; swap(List[i], List[min_index]); } Selection Sort

11 11 Selection Sort The algorithm starts with element 0 It scans through the rest of the elements of the list, and it keeps track of the current minimum element. Once this first scan through the array is complete, element 0 has the proper (least) element The pivot element now moves to element 1 and the scan is repeated from elements 1 through n-1 to find the current minimum element. This process happens repeatedly until the pivot element moves to the end of the list, and the last pivot element has its proper value (magnitude). At this point, the list is sorted.

12 12 Analysis of Selection Sort Selection makes N-1 passes over the data. In the first pass it finds the smallest among all the N elements of the array and swaps it with the smallest index position (N-1 Comparisons). In the j th pass it finds the smallest of a (N-j+1) element array (i.e. N-j comparisons). Thus the worst case complexity is O(N 2 ). Number of exchanges is at most N. Selection is efficient in terms of data moves.

13 13 Insertion Sort // List is an array of size n for (i = 1; i < n; i++) { Inserted = false; // boolean variable j = i; while ((j >= 1) && (Inserted == false)) { if (List[j] < List[j-1]) swap(List[j], List[j-1]) else Inserted = true; j--; } Insertion Sort

14 14 Insertion Sort This algorithm works by first considering element 0 It proceeds to the next element (element 1) and considers its magnitude vis-a-vis element 0. If the magnitude is smaller, it swaps it with element 0. Now, elements 0 and 1 are sorted, but only with respect to each other. Element 2 is now considered, and is moved to its correct position with respect to elements 0 and 1 Each element is sought to placed in its “proper” position vis-a-vis the part of the list already sorted. Note that each element under consideration is swapped with its immediately preceding neighbor as it moves towards the front of the list.

15 15 Analysis of Insertion Sort There are N -1 insert operations. The j th insert operation inserts a new element into a sorted array of j elements and hence takes at most j comparisons. The total number of comparisons is 1 + 2 + 3 +... + (N-1) = N*(N-1)/2 = O(N 2 ) Insertion Sort is linear for “almost sorted” files

16 16 Exchanges Selection Sort uses N 2 /2 comparisons and N exchanges. Insertion Sort uses N 2 /2 comparisons and exchanges in the worst case (reversed), N 2 /4 exchanges and comparisons on average, and about half the averages or N 2 /8 exchanges the “almost sorted” case. Insertion Sort best case: about N comparisons, 0 moves, (input already sorted) Bubble Sort uses N 2 /2 comparisons and exchanges in the best and worst case.


Download ppt "Elementary Sorting 30 January 2003. 2 Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j <= n; j++) if (List[i] > List[j])"

Similar presentations


Ads by Google