cpts121-9-1 - 1 C ptS 121 L9-1 – Pro f C hris Hundha us e...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 C ptS 121 L9-1 – 10/19/09 Pro f. C hris Hundha us e n CptS 121 Fall ‘09 Lecture 9-1 HK Chapter 8: Arrays Lecture Outline I. Array Sorting II. Array Searching 2 C ptS 121 L9-1 – 10/19/09 Pro f. C hris Hundha us e n Array Sorting Lots of motivating problems- Print out a class list in alphabetical order- Order finish times in a race to determine who placed- Make look-up easier (preparation for binary search) 3 C ptS 121 L9-1 – 10/19/09 Pro f. C hris Hundha us e n Array Sorting (cont.) Selection Sort Input: a list of numbers Output: a list of the same numbers in ascending order Intuition: Build up a sorted list by successively finding smallest elements in unsorted portion of array, and placing those values at end of sorted portion of list Method: - Set marker that divides “sorted” and “unsorted” sections of list to the beginning of the list- While the unsorted section of the list is larger than one element – Call upon Find_Smallest helper function to find index of smallest value in “unsorted” section of list – Swap value indexed by marker in with smallest value in “unsorted” section of list – Move "sorted/unsorted" marker right one position 4 CptS 121 L9-1 – 10/19/09 Prof. Chris Hundhausen Array Sorting (cont.) C adaptation of selection sort (compiled and tested!) void selection_sort(int values, int num_values) { int i, index_of_smallest, temp; for (i = 0; i < num_values - 1; ++i) { /* Find the index of the smallest element in unsorted list... */ index_of_smallest = find_smallest(values,i,num_values-1); /* Swap the smallest value in the subarray i+1 .. num_values - 1 with the value i, thereby putting into place the ith element. */ temp = values[i]; values[i] = values[index_of_smallest]; values[index_of_smallest] = temp; } } int find_smallest(int values, int low, int high) { int smallest_index, i; smallest_index = low; for (i = low + 1; i <= high; i++) if (values[i] < values[smallest_index]) smallest_index = i; return smallest_index; } 5 C ptS 121 L9-1 – 10/19/09 Pro f. C hris Hundha us e n Array Sorting (cont.) Insertion Sort Input: a list of numbers Output: a list of the same numbers in ascending order Intuition: Successively insert first item in unsorted portion of list into sorted portion of list Method: - Set marker that divides “sorted” and “unsorted” sections of...
View Full Document

This note was uploaded on 01/25/2011 for the course CPTS 121 taught by Professor Hundhaus during the Spring '09 term at Washington State University .

Page1 / 20

cpts121-9-1 - 1 C ptS 121 L9-1 – Pro f C hris Hundha us e...

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

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