cpts121-9-1

# cpts121-9-1 - CptS 121 Fall 09 Lecture 9-1 HK Chapter 8...

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

1 C ptS  121 L9-1 – 10/19/09 Pro f. C hris  Hundhaus e n CptS 121 Fall ‘09 Lecture 9-1 HK Chapter 8: Arrays Lecture Outline I. Array Sorting II. Array Searching

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

View Full Document
2 C ptS  121 L9-1 – 10/19/09 Pro f. C hris  Hundhaus 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  Hundhaus 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

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

View Full Document
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  Hundhaus 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: -

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

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

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

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

Dana University of Pennsylvania ‘17, Course Hero Intern

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

Jill Tulane University ‘16, Course Hero Intern