ch14 - Chapter Fourteen Sorting and Searching Chapter Goals...

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

View Full Document Right Arrow Icon
Chapter Fourteen: Sorting and 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
Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can differ widely in performance To understand the big-Oh notation To learn how to estimate and compare the performance of algorithms To learn how to measure the running time of a program
Image of page 2
Selection Sort Sorts an array by repeatedly finding the smallest element of the unsorted tail region and moving it to the front Slow when run on large data sets Example: sorting an array of integers 11 9 17 5 12
Image of page 3

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

View Full Document Right Arrow Icon
Sorting an Array of Integers Find the smallest and swap it with the first element Find the next smallest. It is already in the correct place Find the next smallest and swap it with first element of unsorted portion Repeat When the unsorted portion is of length 1, we are done 5 9 17 11 12 5 9 17 11 12 5 9 11 17 12 5 9 11 12 17 5 9 11 12 17
Image of page 4
ch14/selsort/SelectionSorter.java /** This class sorts an array, using the selection sort algorithm */ public class SelectionSorter { /** Constructs a selection sorter. @param anArray the array to sort */ public SelectionSorter(int[] anArray) { a = anArray; } /** Sorts the array managed by this selection sorter. */ public void sort() { Continued
Image of page 5

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

View Full Document Right Arrow Icon
ch14/selsort/SelectionSorter.java (cont.) for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } /** Finds the smallest element in a tail range of the array. @param from the first position in a to compare @return the position of the smallest element in the range a[from] . . . a[a.length - 1] */ private int minimumPosition(int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) if (a[i] < a[minPos]) minPos = i; return minPos; } Continued
Image of page 6
ch14/selsort/SelectionSorter.java (cont.) /** Swaps two entries of the array. @param i the first position to swap @param j the second position to swap */ private void swap(int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private int[] a; }
Image of page 7

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

View Full Document Right Arrow Icon
ch14/selsort/SelectionSortDemo.java 01: import java.util.Arrays; 02: 03: /** 04: This program demonstrates the selection sort algorithm by 05: sorting an array that is filled with random numbers. 06: */ 07: public class SelectionSortDemo 08: { 09: public static void main(String[] args) 10: { 11: int [] a = ArrayUtil.randomIntArray( 20 , 100 ); 12: System.out.println(Arrays.toString(a)); 13: 14: SelectionSorter sorter = new SelectionSorter(a); 15: sorter.sort(); 16: 17: System.out.println(Arrays.toString(a)); 18: } 19: } 20: 21:
Image of page 8
File ArrayUtil.java Typical Output: [65, 46, 14, 52, 38, 2, 96, 39, 14, 33, 13, 4, 24, 99, 89, 77, 73, 87, 36, 81] [2, 4, 13, 14, 14, 24, 33, 36, 38, 39, 46, 52, 65, 73, 77, 81, 87, 89, 96, 99]
Image of page 9

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

View Full Document Right Arrow Icon
Self Check 14.1 Why do we need the temp variable in the swap method? What would happen if you simply assigned a[i] to a[j] and a[j] to a[i] ? Answer: Dropping the temp variable would not work. Then a[i] and a[j] would end up being the same value.
Image of page 10
Self Check 14.2 What steps does the selection sort algorithm go through to sort the sequence 6 5 4 3 2 1?
Image of page 11

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

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