150 likes | 232 Views
COMP 14: Sorting. June 14, 2000 Nick Vallidis. Announcements. P4 is due today!. Homework. Read 6.3 P5 is due next Tuesday (June 20, 2000). Review. How do we declare and instantiate an array? How can we tell how many values there are in an array?
E N D
COMP 14: Sorting June 14, 2000 Nick Vallidis
Announcements • P4 is due today!
Homework • Read 6.3 • P5 is due next Tuesday (June 20, 2000)
Review • How do we declare and instantiate an array? • How can we tell how many values there are in an array? • What's the Java control statement that's super useful with arrays? • So how are Strings and arrays related?
Sorting • Put elements of an array in some order • alphabetize names • order grades lowest to highest • Two simple sorting algorithms • selection sort • insertion sort
Selection Sort • Sorts by putting values directly into their final, sorted position • For each value in the list, the selection sort finds the value that belongs in that position and puts it there
Selection SortGeneral Algorithm • Scan the list to find the smallest value • Exchange that value with the value in the first position in the list • Scan rest of list for the next smallest value • Exchange that value with the value in the second position in the list • And so on, until you get to the end of the list
Selection SortAt Work… 98 68 83 74 93 68 98 83 74 93 5 items 4 swaps 68 74 83 98 93 68 74 83 98 93 68 74 83 9398
code for selection sort (done in class) int tmp; int[] myArray = new int[10000]; for (int j=0; j<myArray.length; j++) { int indexMin=j; for (int i=j; i<myArray.length; i++) { if (myArray[i] < myArray[indexMin]) indexMin = i; } tmp = myArray[j]; myArray[j] = myArray[indexMin]; myArray[indexMin] = tmp; }
Selection Sort • Outer loop controls the index in the array where the next chosen element goes • Inner loop searches for the minimum of the rest of the array • Each iteration of the outer loop adds one more value to the sorted subset of the list, until the entire list is sorted
Selection Sort • Sorts in ascending order • Can be changed to sort in descending order • look for max instead of min
Insertion Sort • Like we’d actually sort things • Insert each new item into an already sorted list • Each unsorted element is inserted at the appropriate spot in the sorted subset until the list is ordered
Insertion SortGeneral Algorithm • Sort the first two values (swap, if necessary) • Insert list’s third value into the appropriate position relative to the first two (which are already sorted) • Insert fourth into the appropriate position relative to the three sorted • Each time insertion made, number of values in the sorted subset increases by one • Repeat until all values in list are sorted • Other values in array shift to make room for inserted elements
Insertion SortAt Work… 98 68 83 74 93 6898 83 74 93 68 83 9874 93 68 74 83 9893 68 74 83 93 98
Insertion Sort • Outer loop controls the index in the array of the next value to be inserted • Inner loop compares the current insert value with values stored at lower indexes • Each iteration of the outer loop adds one more value to the sorted subset of the list, until the entire list is sorted