1 / 17

The Sorting Game

The Sorting Game. How many items are you sorting? How many times are you sorting? How is the data (pre) organized? Almost sorted? Large number of identical keys? Elementary methods are usually O(n 2 ), usually OK for small n, but to be avoided for large, randomly arranged n. Some Rules.

farrah
Download Presentation

The Sorting Game

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Sorting Game • How many items are you sorting? • How many times are you sorting? • How is the data (pre) organized? • Almost sorted? • Large number of identical keys? • Elementary methods are usually O(n2), usually OK for small n, but to be avoided for large, randomly arranged n.

  2. Some Rules • We aim to sort files of records containing keys. The key, a small part of each record, is used to control the sort. • Internal sort: data fits in memory • External sort: data does not fit in memory. • Important factors: • Run time. Limit is O(NlgN). • Memory consumption • Sort-in-place (no extra memory needed) • Linked list (need mem for ptrs to keys) • Need copy of entire array to be sorted • Stability - relative order of equal keys

  3. Selection Sort • A very simple sort: • Find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and so on. • Method called selection sort because it selects the smallest remaining element.

  4. Selection Sort Algorithm int i, j, min, t; for (i=0; i<N-1; i++) { /* find min */ min = i; for (j=i+1; j<N; j++) if (a[j] < a[min]) min = j; /* exchange */ t = a[min]; a[min] = a[i]; a[i] = t; }

  5. Tracing Selection Sort 10 9 7 8 6 5 4 3 1 2 1 9 7 8 6 5 4 3 10 2 1 2 7 8 6 5 4 3 10 9 1 2 3 8 6 5 4 7 10 9 1 2 3 4 6 5 8 7 10 9 1 2 3 4 5 6 8 7 10 9 1 2 3 4 5 6 8 7 10 9 1 2 3 4 5 6 7 8 10 9 1 2 3 4 5 6 7 8 10 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

  6. Tracing Selection Sort

  7. Selection Sort Properties • Inner loop runs at most ~ N times • Elements are moved only once, making this sort method a choice method for large records.

  8. Selection Sort Performance Selection sort uses about N2/2 comparisons and N exchanges Looking at the figure on the previous slide, we see an NxN table, in which each letter corresponds to a comparison. The N-1 elements on the diagonal (not the last) each correspond to an exchange. More precisely: for each i from 0 to N-2, there is one exchange and N-i comparisons, so there is a total of N-1 exchanges and (N-1) + (N-2) + … + 2 + 1 = N(N-1)/2 comparisons, regardless of the input data. The only part that does depend on the input is the assignment to min, which could be quadratic but on the average takes about NlgN. We can expect the running time of selection sort to be quite insensitive to input data.

  9. Insertion Sort • Like sorting a hand of cards. • Consider elements one at a time, inserting each in its proper place among those already considered, keeping them sorted. Move larger elements one position to the right and then insert the element into the vacated position.

  10. Insertion Sort Algorithm int i, j, v; for (i=1; i<N; i++) { /* define current subset */ v = a[i]; j = i; /* rearrange current subset */ while (j && (a[j-1] > v)) { a[j] = a[j-1]; j--; } a[j] = v; }

  11. Tracing Insertion Sort 10 9 7 8 6 5 4 3 1 2 9 10 7 8 6 5 4 3 1 2 7 9 10 8 6 5 4 3 1 2 7 8 9 10 6 5 4 3 1 2 6 7 8 9 10 5 4 3 1 2 5 6 7 8 9 10 4 3 1 2 4 5 6 7 8 9 10 3 1 2 3 4 5 6 7 8 9 10 1 2 1 3 4 5 6 7 8 9 10 2 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

  12. Tracing Insertion Sort

  13. Insertion Sort Performance Insertion sort uses about N2/4 comparisons and N2/8 exchanges on the average, and twice as many in the worst case. Looking at the figure on the previous slide, we see an NxN table. We can consider all the elements in the lower diagonal in the worst case. For random input, we expect each element to go about halfway back, on the average, thus we can count half of the elements in the lower diagonal.

  14. Bubble Sort: a digression int i, j, t; for (i=N-1; i>=0; i--) for (j=1; j<=i; j++) if (a[j-1] > a[j]) { t = a[j-1]; a[j-1] = a[j]; a[j] = t; }

  15. Tracing Bubble Sort 10 9 7 8 6 5 4 3 1 2 9 10 7 8 6 5 4 3 1 2 9 7 10 8 6 5 4 3 1 2 9 7 8 10 6 5 4 3 1 2 9 7 8 6 10 5 4 3 1 2 9 7 8 6 5 10 4 3 1 2 9 7 8 6 5 4 10 3 1 2 9 7 8 6 5 4 3 10 1 2 9 7 8 6 5 4 3 1 10 2 9 7 8 6 5 4 3 1 2 10 7 9 8 6 5 4 3 1 2 10 7 8 9 6 5 4 3 1 2 10 7 8 6 9 5 4 3 1 2 10 7 8 6 5 9 4 3 1 2 10 7 8 6 5 4 9 3 1 2 10 7 8 6 5 4 3 9 1 2 10 7 8 6 5 4 3 1 9 2 10 7 8 6 5 4 3 1 2 9 10 7 6 8 5 4 3 1 2 9 10 7 6 5 8 4 3 1 2 9 10 7 6 5 4 8 3 1 2 9 10 7 6 5 4 3 8 1 2 9 10 7 6 5 4 3 1 8 2 9 10 7 6 5 4 3 1 2 8 9 10 6 7 5 4 3 1 2 8 9 10 6 5 7 4 3 1 2 8 9 10 6 5 4 7 3 1 2 8 9 10 6 5 4 3 7 1 2 8 9 10 6 5 4 3 1 7 2 8 9 10 6 5 4 3 1 2 7 8 9 10 5 6 4 3 1 2 7 8 9 10 5 4 6 3 1 2 7 8 9 10 5 4 3 6 1 2 7 8 9 10 5 4 3 1 6 2 7 8 9 10 5 4 3 1 2 6 7 8 9 10 4 5 3 1 2 6 7 8 9 10 4 3 5 1 2 6 7 8 9 10 4 3 1 5 2 6 7 8 9 10 4 3 1 2 5 6 7 8 9 10 3 4 1 2 5 6 7 8 9 10 3 1 4 2 5 6 7 8 9 10 3 1 2 4 5 6 7 8 9 10 1 3 2 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 Considerably more work!

  16. Bubble Sort Performance Bubble sort uses about N2/2 comparisons and N2/2 exchanges on the average and in the worst case. In the worst case (file is in reverse order), it is clear that the ith pass requires N-i comparisons and exchanges, and the proof is like that of selection sort, but the running time of bubble sort does depend on the input. For example, note that only one pass is required if the file is already in sorted order. (It turns out that the average-case performance is not significantly better than the worst case. Analysis is rather difficult).

  17. Tracing sorts graphically Selection Sort. Moves left to right, putting elements in their final position without looking back. Bubble Sort. "Selects" maximum remaining elements at each stage, but wastes effort putting some order into the unsorted part of the array. Insertion Sort. Moves left to right, inserts newly encountered elements into position without looking any further forward. Note how the left part of the array is continually changing.

More Related