Lecture8sortAndSearch_4up

Lecture8sortAndSearch_4up - Sorting CMPT 126: Lecture 8...

Info iconThis 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-deFned 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 Fnal, sorted positions. Consider a list of numeric values that should be in ascending (increasing) order: 1. Scan the entire list and Fnd the smallest value. 2. Exchange that value with the value in the Frst position of the list. 3. Scan the list, omitting the Frst value, and Fnd the smallest value. 4. Exchange that value with the value in the second position of the list. 5. Scan the list, omitting the Frst two values, and Fnd 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
Background image of page 1

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

View Full DocumentRight 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 Frst 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 Frst two (sorted) values.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 8

Lecture8sortAndSearch_4up - Sorting CMPT 126: Lecture 8...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online