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

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

View Full Document Right Arrow Icon
Chapter Fourteen: Sorting and Searching
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background 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; }
Background image of page 7

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

View Full DocumentRight 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:
Background 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]
Background image of page 9

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

View Full DocumentRight Arrow Icon
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:
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2009 for the course SC 418262 taught by Professor Chuleerut during the Spring '08 term at Kasetsart University.

Page1 / 79

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

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

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