1 / 17

Sorting

Sorting. contents. 3 kinds of sorting methods Selection, exchange , and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements Simple selection, bubble (exchange), and insertion sorts heaps , for efficient selection sort heapsort priority queues using heaps

dianne
Télécharger la présentation

Sorting

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. Sorting

  2. contents • 3 kinds of sorting methods • Selection, exchange, and insertion • O(n2) sorts – VERY inefficient, but OK for ≈ 10 elements • Simple selection, bubble (exchange), and insertion sorts • heaps, for efficient selection sort • heapsort • priority queues using heaps • quicksort - example of divide-and-conquer strategy for efficient exchange sort • mergesort - for sequential files • radix sort - a non-comparison-based sort • no such thing as a universally good sorting scheme • Results may depend just how bad (out of order) the list is

  3. Selection Sort Algorithm • Set i=0 • Start at element i • Search for smallest item < current item • Swap them • Advance i to i+1 • Repeat from step 2 until done

  4. Selection sort • Make passes through a list • Each time correctly reposition one element • Restart at current (new) element and try again • Always moves the smallest item toward 0 5, 1, 12, -5, 16, 2, 12, 14 No smaller items so -5 gets swapped 5, 1, 12, -5, 16, 2, 12, 14

  5. Selection Sort Results (red="to be swapped") • 5, 1,12,-5,16,2,12,14 (original array) • -5,1,12,5,16,2,12,14, • -5,1,2,5,16,12,12,14, • -5,1,2,5,12,16,12,14, • -5,1,2,5,12,12,16,14, • -5,1,2,5,12,12,14,16 done swapping, but not done • -5,1,2,5,12,12,14,16 done!! • 5 swaps

  6. Selection sort code n=sizeof(myarray)/sizeof(*myarray); // #elements in array for(i = 0; i < n - 1; i++) // n is size of array (NOT max subs) { minloc= i; for (j = i + 1; j < n; j++) if (myarray[j] < myarray[minloc]) // smaller value? minloc= j; // if yes, new minloc if(minloc!= i) // if not itself { // swap the 2 elements temp = myarray[i]; // save new smaller one array[i] = myarray[minloc]; // move bigger one myarray[minloc] = temp; // insert smaller one       }       }

  7. Exchange Sort Algorithm • n=#elements of the array • start at item n • Search for FIRST smaller item • Swap as soon as one is found • Repeat from step 3 until end of array • // above 2 steps move largest item toward end • Increase n • Repeat from step 2

  8. Exchange sort results (red="to be swapped") • 5,1,12,-5,16,2,12,14 (original array) • 1,5,12,-5,16,2,12,14, • -5,5,12,1,16,2,12,14 • -5,1,12,5,16,2,12,14, • -5,1,5,12,16,2,12,14, • -5,1,2,12,16,5,12,14, • -5,1,2,5,16,12,12,14, • -5,1,2,5,12,16,12,14, • -5,1,2,5,12,12,16,14, • -5,1,2,5,12,12,14,16 done! • Nine swaps!

  9. Exchange Sort code n=sizeof(myarray)/sizeof(*myarray); for (i=0; i<n-1; i++) for (j=i+1; j<n; j++) if (myarray[i] > myarray[j]) { //swap values temp = myarray[i]; // save new smaller one array[i] = myarray[j]; // move bigger one myarray[j] = temp; // insert smaller one }

  10. Bubble Sort Algorithm (an exchange sort) • Repeat • hasChanged := false • decrementitemCount • repeat (index from 1 toitemCount) • if(item at index) > (item at (index + 1)) swap (item at index) with (item at (index + 1)) • hasChanged := true • untilhasChanged = false • A swap occurs at every value of itemcount

  11. Bubble Sort Results (red="to be swapped") • 5, 1,12,-5,16,2,12,14 (original array) • 1,5,12,-5,16,2,12,14, • 1,5,-5,12,16,2,12,14, • 1,5,-5,12,2,16,12,14, • 1,5,-5,12,2,12,16,14, • 1,5,-5,12,2,12,14,16, • 1,-5,5,12,2,12,14,16, • 1,-5,5,2,12,12,14,16, • -5,1,5,2,12,12,14,16, • -5,1,2,5,12,12,14,16, done!! • 9 swaps!!

  12. Bubble Sort Code asize=sizeof(myarray)/sizeof(*myarray); for(x=0; x<asize; x++) // start with one item {for(y=0; y<asize-1; y++) // for all following pairs in array {if(myarray[y]>myarray[y+1]) {// swap a pair as needed temp = myarray[y+1]; myarray[y+1] = myarray[y]; myarray[y] = temp; printarray(); } } // end y loop. Start again. 2nd item is part of a pair } // end x loop

  13. Insertion Sort • Repeatedly insert a new element into an already sorted list • Great for a linked list

  14. Algorithm for Linear Insertion Sort • Consider array as having 2 parts: • Sorted: 1st element (even if not in right place) • Unsorted: remaining elements • Look at first item in the UNsorted part • Move it to left or right of sorted part • Copy it to a temp • Locate where to put the item • Starting there, shift all elements, to the "right" • Makes a hole for the item • Insert the item • Define new beginning of Unsorted part • Repeat all of above until done

  15. Re-stating the Algorithm for Linear Insertion Sort //similar to selection sort • Get a list of unsorted numbers • Set a "marker" for the sorted section after the first number in the list • Repeat steps 4 through 6 until the unsorted section is empty •    Select the first unsorted number •    Swap this number to the left until it arrives at the correct sorted position •    Advance the marker to the right by one position • Stop

  16. Insertion Sort Results (red ="to be moved") • 5, 1, 12, -5, 16, 2, 12, 14 original array • Remove the 1 & slide the 5 to the right • ---, 5, 12, -5, 16, 2, 12, 14 now insert the 1 • 1, 5, 12, -5, 16, 2, 12, 14 the 12 is OK, move the -5 • -5, 1, 5, 12, 16, 2, 12, 14 need to move the 2 • -5, 1, ---, 5, 12, 16, 12, 14 • -5, 1, 2, 5, 12, 16, 12, 14 now swap 2,-5, 1 • -5, 1, 2, 5, 12, 16, 12, 14 insert the 2nd 12 • -5, 1, 2, 5, 12, 12, 16, 14 insert the 14 • -5, 1, 2, 5, 12, 12, 14, 16

  17. Insertion Sort Code asize=sizeof(myarray)/sizeof(*myarray); for (i = 1; i < asize; i++) { j = i; while (j > 0 && myarray [j - 1] > myarray [j]) { tmp= myarray[j]; myarray [j] = myarray[j - 1]; myarray[j - 1] = tmp; j--; } }

More Related