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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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)
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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; }
Image of page 4
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: -
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
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