Download presentation
Presentation is loading. Please wait.
Published byNigel Jefferson Modified over 9 years ago
1
Sorting
2
2 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 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 1.Set i=0 2.Start at element i 3.Search for smallest item < current item 4.Swap them 5.Advance i to i+1 6.Repeat from step 2 until done
4
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 1.n=#elements of the array 2.start at item n 3.Search for FIRST smaller item 4.Swap as soon as one is found 5.Repeat from step 3 until end of array 6.// above 2 steps move largest item toward end 7.Increase n 8.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
10 Bubble Sort Algorithm (an exchange sort) 1.Repeat a. hasChanged := false b.decrement itemCount c.repeat (index from 1 to itemCount ) i.if (item at index) > (item at (index + 1)) swap (item at index) with (item at (index + 1)) ii.hasChanged := true 2.until hasChanged = false 3.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. 2 nd item is part of a pair } // end x loop
13
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: 1 st 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
15 Re-stating the Algorithm for Linear Insertion Sort //similar to selection sort 1.Get a list of unsorted numbers 2.Set a "marker" for the sorted section after the first number in the list 3.Repeat steps 4 through 6 until the unsorted section is empty 4. Select the first unsorted number 5. Swap this number to the left until it arrives at the correct sorted position 6. Advance the marker to the right by one position 7.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--; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.