1 / 15

COMP 14: Sorting

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?

damon
Download Presentation

COMP 14: 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. COMP 14: Sorting June 14, 2000 Nick Vallidis

  2. Announcements • P4 is due today!

  3. Homework • Read 6.3 • P5 is due next Tuesday (June 20, 2000)

  4. 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?

  5. Sorting • Put elements of an array in some order • alphabetize names • order grades lowest to highest • Two simple sorting algorithms • selection sort • insertion sort

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

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

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

  9. 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; }

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

  11. Selection Sort • Sorts in ascending order • Can be changed to sort in descending order • look for max instead of min

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

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

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

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

More Related