Lecture8sortAndSearch_4up

Lecture8sortAndSearch_4up - Sorting CMPT 126 Lecture 8...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPT 126: Lecture 8 Sorting and Searching (9.4 and 9.5) Tamara Smyth, [email protected] School of Computing Science, Simon Fraser University October 16, 2007 1 Sorting Sorting is the process of arranging a list of items in a well-defined order. Eg: alphabetize a list numeric order Here, we look at two sorting algorithms: 1. selection sort 2. insertion sort CMPT 126: Sorting and Searching, Lecture 8 2 Selection Sort The selection sort algorithm sorts a list of values by successively putting values in their final, sorted positions. Consider a list of numeric values that should be in ascending (increasing) order: 1. Scan the entire list and find the smallest value. 2. Exchange that value with the value in the first position of the list. 3. Scan the list, omitting the first value, and find the smallest value. 4. Exchange that value with the value in the second position of the list. 5. Scan the list, omitting the first two values, and find the smallest value. 6. Exchange that value with the value in the third position of the list. 7. Continue this process for all but the last position in the list, which will contain the largest value. CMPT 126: Sorting and Searching, Lecture 8 3 6 9 6 1 2 1 2 6 3 9 9 6 2 3 1 1 2 9 3 3 Figure 1: Selection sort processing. CMPT 126: Sorting and Searching, Lecture 8 4
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
selectionSort Method public static void selectionSort(Comparable[] list) { int min; Comparable temp; for (int i=0; i<list.length-1; i++) { min = i; //index of object with minimum value for (int scan = i+1; scan < list.length; scan++) if (list[scan].compareTo(list[min]) < 0) min = scan; //swap value temp = list[min]; list[min] = list[i]; list[i] = temp; } } The selectionSort method accepts an array of Comparable objects. Comparable is an interface with a single method called compareTo which returns an integer < 0 : if object is less than current object > 0 : if object is greater than current object = 0 : if objects are equal CMPT 126: Sorting and Searching, Lecture 8 5 Insertion Sort The insertion sort algorithm sorts a list of values by repetitively inserting a particular value into a subset of the list that has already been sorted. One at a time, each unsorted element is inserted at the appropriate position in that sorted subset until the entire list is in order. Begin with a “sorted” list containing only one value (the first element of the original list). Insert the second element of the original list into the “sorted” list at the appropriate index. Shift other elements if necessary. Insert the original list’s third value into the “sorted” list the appropriate position relative to the first two (sorted) values. Insert the fourth value of the original list into its proper position in the sorted list relative to the first three values.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern