CHAPTER 18 - PowerPoint PPT Presentation

chapter 18 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CHAPTER 18 PowerPoint Presentation
play fullscreen
1 / 84
CHAPTER 18
152 Views
Download Presentation
avent
Download Presentation

CHAPTER 18

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CHAPTER 18 SORTING AND SEARCHING

  2. CHAPTER GOALS • To study the several searching and sorting algorithms • To appreciate that algorithms for the same task can differ widely in performance • To understand 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

  3. Sorting an Array of Integers • The selection sort algorithm repeatedly finds the smallest element in the unsorted tail region of the array and moves it to the front

  4. Sorting an Array of Integers • Array in original order • Find the smallest and swap it with the first element

  5. Sorting an Array of Integers • Find the next smallest. It is already in the correct place • Find the next smallest and swap it with the first element of unsorted portion

  6. Sorting an Array of Integers • Repeat • When the unsorted portion is of length 1, we are done

  7. File SelectionSorter.java 01: /** 02: This class sorts an array, using the selection sort 03: algorithm 04: */ 05: public class SelectionSorter 06: { 07: /** 08: Constructs a selection sorter. 09: @param anArray the array to sort. 10: */ 11: public SelectionSorter(int[] anArray) 12: { 13: a = anArray; 14: } 15: 16: /** 17: Sorts the array managed by this selection sorter.

  8. 18: */ 19: public void sort() 20: { 21: for (int i = 0; i < a.length - 1; i++) 22: { 23: int minPos = minimumPosition(i); 24: swap(minPos, i); 25: } 26: } 27: 28: /** 29: Finds the smallest element in a tail range of the array 30: @param from the first position in a to compare 31: @return the position of the smallest element in the 32: range a[from]...a[a.length - 1] 33: */ 34: private int minimumPosition(int from) 35: { 36: int minPos = from; 37: for (int i = from + 1; i < a.length; i++)

  9. 38: if (a[i] < a[minPos]) minPos = i; 39: return minPos; 40: } 41: 42: /** 43: Swaps two entries of the array. 44: @param i the first position to swap 45: @param j the second position to swap 46: */ 47: private void swap(int i, int j) 48: { 49: int temp = a[i]; 50: a[i] = a[j]; 51: a[j] = temp; 52: } 53: 54: private int[] a; 55: }

  10. File SelectionSortTest.java 01: /** 02: This program tests the selection sort algorithm by 03: sorting an array that is filled with random numbers. 04: */ 05: public class SelectionSortTest 06: { 07: public static void main(String[] args) 08: { 09: int[] a = ArrayUtil.randomIntArray(20, 100); 10: ArrayUtil.print(a); 11: 12: SelectionSorter sorter = new SelectionSorter(a); 13: sorter.sort(); 14: 15: ArrayUtil.print(a); 16: } 17: }

  11. File ArrayUtil.java 01: import java.util.Random; 02: 03: /** 04: This class contains utility methods for array 05: manipulation. 06: */ 07: public class ArrayUtil 08: { 09: /** 10: Creates an array filled with random values. 11: @param length the length of the array 12: @param n the number of possible random values 13: @return an array filled with length numbers between 14: 0 and n-1 15: */ 16: public static int[] randomIntArray(int length, int n) 17: { int[] a = new int[length];

  12. 18: Random generator = new Random(); 19: 20: for (int i = 0; i < a.length; i++) 21: a[i] = generator.nextInt(n); 22: 23: return a; 24: } 25: 26: /** 27: Prints all elements in an array. 28: @param a the array to print 29: */ 30: public static void print(int[] a) 31: { 32: for (int i = 0; i < a.length; i++) 33: System.out.print(a[i] + " "); 34: System.out.println(); 35: } 36: }

  13. Profiling the Selection Sort Algorithm • We want to measure the time the algorithm takes to execute • Exclude the time the program takes to load • Exclude output time

  14. Profiling the Selection Sort Algorithm • Create a StopWatch class to measure execution time of an algorithm • It can start, stop and give elapsed time • Use System.currentTimeMillis method

  15. Profiling the Selection Sort Algorithm • Create a Stopwatch object • Start the stopwatch just before the sort • Stop the stopwatch just after the sort • Read the elapsed time

  16. File StopWatch.java 01: /** 02: A stopwatch accumulates time when it is running. You can 03: repeatedly start and stop the stopwatch. You can use a 04: stopwatch to measure the running time of a program. 05: */ 06: public class StopWatch 07: { 08: /** 09: Constructs a stopwatch that is in the stopped state 10: and has no time accumulated. 11: */ 12: public StopWatch() 13: { 14: reset(); 15: } 16: 17: /**

  17. 18: Starts the stopwatch. Time starts accumulating now. 19: */ 20: public void start() 21: { 22: if (isRunning) return; 23: isRunning = true; 24: startTime = System.currentTimeMillis(); 25: } 26: 27: /** 28: Stops the stopwatch. Time stops accumulating and is 29: is added to the elapsed time. 30: */ 31: public void stop() 32: { 33: if (!isRunning) return; 34: isRunning = false; 35: long endTime = System.currentTimeMillis(); 36: elapsedTime = elapsedTime + endTime - startTime; 37: }

  18. 38: 39: /** 40: Returns the total elapsed time. 41: @return the total elapsed time 42: */ 43: public long getElapsedTime() 44: { 45: if (isRunning) 46: { 47: long endTime = System.currentTimeMillis(); 48: elapsedTime = elapsedTime + endTime - startTime; 49: startTime = endTime; 50: } 51: return elapsedTime; 52: } 53: 54: /** 55: Stops the watch and resets the elapsed time to 0. 56: */ 57: public void reset()

  19. 58: { 59: elapsedTime = 0; 60: isRunning = false; 61: } 62: 63: private long elapsedTime; 64: private long startTime; 65: private boolean isRunning; 66: }

  20. File SelectionSortTimer 01: import javax.swing.JOptionPane; 02: 03: /** 04: This program measures how long it takes to sort an 05: array of a user-specified size with the selection 06: sort algorithm. 07: */ 08: public class SelectionSortTimer 09: { 10: public static void main(String[] args) 11: { 12: String input = JOptionPane.showInputDialog( 13: "Enter array size:"); 14: int n = Integer.parseInt(input); 15: 16: // construct random array 17:

  21. 18: int[] a = ArrayUtil.randomIntArray(n, 100); 19: SelectionSorter sorter = new SelectionSorter(a); 20: 21: // use stopwatch to time selection sort 22: 23: StopWatch timer = new StopWatch(); 24: 25: timer.start(); 26: sorter.sort(); 27: timer.stop(); 28: 29: System.out.println("Elapsed time: " 30: + timer.getElapsedTime() + " milliseconds"); 31: System.exit(0); 32: } 33: }

  22. Selection Sort on Various Size Arrays Time for array size n

  23. Selection Sort on Various Size Arrays Time vs array size

  24. Selection Sort on Various Size Arrays • Doubling the size of the array more than doubles the time needed to sort it

  25. Analyzing the Selection Sort Algorithm • In an array of size n, count how many times an array element is visited • To find the smallest, visit n elements + 2 visits for the swap • To find the next smallest, visit (n-1) elements + 2 visits for the swap • The last term is 2 elements visited to find the smallest + 2 visits for the swap

  26. Analyzing the Selection Sort Algorithm • The number of visits: • n + 2 + (n-1) + 2 + (n-2) +2 + . . .+ 2 + 2 • This can be simplified to n2 /2  +  n/2  - 3 • n/2 - 3 is small compared to n2 /2 - so let's ignore it • also ignore the 1/2 - it divides out when comparing ratios

  27. Analyzing the Selection Sort Algorithm • The number of visits is of the order n2 • Using big-Oh notation: The number of visits is O(n2) • Multiplying the number of elements in an array by 2 multiplies the processing time by 4

  28. Merge Sort • Divide an array in half and sort each half

  29. Merge Sort • Merge the two sorted arrays into a single sorted array

  30. Merge Sort • Dramatically faster than the selection sort.

  31. File MergeSorter.java 01: /** 02: This class sorts an array, using the merge sort 03: algorithm 04: */ 05: public class MergeSorter 06: { 07: /** 08: Constructs a merge sorter. 09: @param anArray the array to sort 10: */ 11: public MergeSorter(int[] anArray) 12: { 13: a = anArray; 14: } 15: 16: /** 17: Sorts the array managed by this merge sorter

  32. 18: */ 19: public void sort() 20: { 21: if (a.length <= 1) return; 22: int[] first = new int[a.length / 2]; 23: int[] second = new int[a.length - first.length]; 24: System.arraycopy(a, 0, first, 0, first.length); 25: System.arraycopy(a, first.length, second, 0, second.length); 26: MergeSorter firstSorter = new MergeSorter(first); 27: MergeSorter secondSorter = new MergeSorter(second); 28: firstSorter.sort(); 29: secondSorter.sort(); 30: merge(first, second); 31: } 32: 33: /** 34: Merges two sorted arrays into the array to be sorted by this 35: merge sorter. 36: @param first the first sorted array 37: @param second the second sorted array

  33. 38: */ 39: private void merge(int[] first, int[] second) 40: { 41: // merge both halves into the temporary array 42: 43: int iFirst = 0; 44: // next element to consider in the first array 45: int iSecond = 0; 46: // next element to consider in the second array 47: int j = 0; 48: // next open position in a 49: 50: // as long as neither i1 nor i2 past the end, move 51: // the smaller element into a 52: while (iFirst < first.length && iSecond < second.length) 53: { 54: if (first[iFirst] < second[iSecond]) 55: { 56: a[j] = first[iFirst]; 57: iFirst++;

  34. 58: } 59: else 60: { 61: a[j] = second[iSecond]; 62: iSecond++; 63: } 64: j++; 65: } 66: 67: // note that only one of the two while loops 68: // below is executed 69: 70: // copy any remaining entries of the first array 71: System.arraycopy(first, iFirst, a, j, first.length - iFirst); 72: 73: // copy any remaining entries of the second half 74: System.arraycopy(second, iSecond, a, j, second.length - iSecond); 75: } 76: 77: private int[] a; 78: }

  35. File MergeSortTest.java 01: /** 02: This program tests the merge sort algorithm by 03: sorting an array that is filled with random numbers. 04: */ 05: public class MergeSortTest 06: { 07: public static void main(String[] args) 08: { 09: int[] a = ArrayUtil.randomIntArray(20, 100); 10: ArrayUtil.print(a); 11: MergeSorter sorter = new MergeSorter(a); 12: sorter.sort(); 13: ArrayUtil.print(a); 14: } 15: }

  36. Merge Sort vs Selection Sort

  37. Merge Sort Timing Vs Selection Sort Merge sort – rectangles Selection sort - circles

  38. Analyzing Merge Sort Algorithm • In an array of size n, count how many times an array element is visited • Assume n is a power of 2:    n = 2m • Calculate the number of visits to create the two sub-arrays and then merge the two sorted arrays • 3 visits to merge each element or 3n visits • 2n visits to create the two sub-arrays • total of 5n visits

  39. Analyzing Merge Sort Algorithm • Let T(n) denote the number of visits to sort an array of n elements then • T(n) = T(n/2) + T(n/2) + 5n or • T(n) = 2*T(n/2) + 5n

  40. Analyzing Merge Sort Algorithm • The visits for an array of size n/2 is    T(n/2) = 2*T(n/4) + 5n/2 • So T(n) = 2 * 2*T(n/4) +5n + 5n • The visits for an array of size n/4 is     T(n/4) = 2*T(n/8) + 5n/4 • So T(n) = 2 * 2 * 2*T(n/8) + 5n + 5n + 5n

  41. Analyzing Merge Sort Algorithm • Repeating the process k times:    T(n) = 2kT(n/2k) +5nk • since n = 2m, when k = m:    T(n) = 2mT(n/2m) +5nm • T(n) = nT(1) +5nm • T(n) = n + 5nlog2(n)

  42. Analyzing Merge Sort Algorithm • To establish growth order • Drop the lower-order term n • Drop the constant factor 5 • Drop the base of the logarithm since all logarithms are related by a common factor • We are left with n log(n)

  43. Analyzing Merge Sort Algorithm • Using big-Oh notation: The number of visits is O( nlog(n) )

  44. Merge Sort Vs Selection Sort • Selection sort is an O( n2 ) algorithm • Merge sort is an O( nlog(n) ) algorithm • The nlog(n) function grows much more slowly than n2

  45. Sorting in a Java Program • The Arrays class contains static sort methods • To sort an array of integers int[] intArray = . . . ; Arrays.sort(intArray);

  46. Linear Search • Also called sequential search • Examines all values in an array until it finds a match or reaches the end • Number of visits for a linear search of an array of n elements: • The average search visits n/2 elements • The maximum visits is n • A linear search is an O(n) algorithm

  47. File LinearSearcher.java 01: /** 02: A class for executing linear searches through an array. 03: */ 04: public class LinearSearcher 05: { 06: /** 07: Constructs the LinearSearcher. 08: @param anArray an array of integers 09: */ 10: public LinearSearcher(int[] anArray) 11: { 12: a = anArray; 13: } 14: 15: /** 16: Finds a value in an array, using the linear search 17: algorithm.

  48. 18: @param v the value to search 19: @return the index at which the value occurs, or -1 20: if it does not occur in the array 21: */ 22: public int search(int v) 23: { 24: for (int i = 0; i < a.length; i++) 25: { 26: if (a[i] == v) 27: return i; 28: } 29: return -1; 30: } 31: 32: private int[] a; 33: }

  49. File LinearSearchTest.java 01: import javax.swing.JOptionPane; 02: 03: /** 04: This program tests the linear search algorithm. 05: */ 06: public class LinearSearchTest 07: { 08: public static void main(String[] args) 09: { 10: // construct random array 11: 12: int[] a = ArrayUtil.randomIntArray(20, 100); 13: ArrayUtil.print(a); 14: LinearSearcher searcher = new LinearSearcher(a); 15: 16: boolean done = false; 17: while (!done)

  50. 18: { 19: String input = JOptionPane.showInputDialog( 20: "Enter number to search for, Cancel to quit:"); 21: if (input == null) 22: done = true; 23: else 24: { 25: int n = Integer.parseInt(input); 26: int pos = searcher.search(n); 27: System.out.println("Found in position " + pos); 28: } 29: } 30: System.exit(0); 31: } 32: }