Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Sorting and Searching Copyright © 2014 by John Wiley & Sons. All rights reserved. 1 Sorting Sorting places data in ascending or descending order, based on one or more sort keys E.g. • A list of names could be sorted alphabetically, • bank accounts could be sorted by account number, • employee payroll records could be sorted by social security number How do you sort a list/array of data? Copyright © 2014 by John Wiley & Sons. All rights reserved. 2 Popular sorting algorithms: • • • • • Selection sort Insertion sort Merge sort Bubble sort Quick sort The end result (sorted array) is the same no matter which algorithm you use to sort the array. The choice of algorithm affects only the run time and memory use of the program. Copyright © 2014 by John Wiley & Sons. All rights reserved. 3 Selection Sort Simple sorting algorithm First iteration: • selects the smallest element in the array and swaps it with the first element. Second iteration: • selects the second-smallest item and swaps it with the second element. After the ith iteration: • the smallest i items of the array will be sorted into increasing order in the first i elements of the array. Selection sort sorts an array by repeatedly finding the smallest element of the unsorted tail region and moving it to the front. Example: sorting an array of integers Copyright © 2014 by John Wiley & Sons. All rights reserved. 4 Sorting an Array of Integers 1. Find the smallest and swap it with the first element 2. Find the next smallest. It is already in the correct place 3. Find the next smallest and swap it with first element of unsorted portion 4. Repeat 5. When the unsorted portion is of length 1, we are done Copyright © 2014 by John Wiley & Sons. All rights reserved. 5 Selection sort pseudocode: For i = 0 to n-1 do: iMin = i For j = i + 1 to n-1 do: If A(j) < A(iMin ) iMin = j End-If End-For swap(A(i), A(iMin)) End-For swap(A(i), A(iMin)): temp = A(i) A(i) = A(iMin) A(iMin) = temp Copyright © 2014 by John Wiley & Sons. All rights reserved. 6 Programming Question Implement class SelectionSorter . Method sort should accepts an integer array as parameter and perform selection sort on it. Program template in next slide Copyright © 2014 by John Wiley & Sons. All rights reserved. 7 public class SelectionSorter{ public static void sort(int[] a) { //TODO: implement selection sort } public static int[] randomIntArray(int length, int n) Random generator = new Random(); int[] a = new int[length]; for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } { public static void main(String[] args) { int[] a = randomIntArray(20, 100); System.out.println("Before: "+ Arrays.toString(a)); sort(a); System.out.println("After: "+ Arrays.toString(a)); } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 8 Answer SelectionSorter.java import java.util.Arrays; import java.util.Random; public class SelectionSorter{ public static void sort(int[] a) { for (int i = 0; i < a.length - 1; i++) { int minIndex = i; //find index of minimum value element (minIndex) from indices i+1 to n-1 (unsorted section) for (int j = i + 1; j < a.length; j++) if (a[j] < a[minIndex]) minIndex = j; //swap element at indices i and minPos int tmp = a[i]; a[i] = a[minIndex]; a[minIndex] = tmp; } } public static int[] randomIntArray(int length, int n) Random generator = new Random(); int[] a = new int[length]; for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } { public static void main(String[] args) { int[] a = randomIntArray(20, 100); System.out.println("Before: "+ Arrays.toString(a)); sort(a); System.out.println("After: "+ Arrays.toString(a)); } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 9 Analyzing the Performance of 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 For i = 0 to n-1 do: iMin = i For j = i + 1 to n-1 do: If A(j) < A(iMin ) iMin = j End-If End-For swap(A(i), A(iMin)) End-For Copyright © 2014 by John Wiley & Sons. All rights reserved. 10 Analyzing the Performance of 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 + 5n/2 - 3 5n/2 - 3 is small compared to n2 /2 – so let's ignore it Also ignore the 1/2 – it cancels out when comparing ratios Copyright © 2014 by John Wiley & Sons. All rights reserved. 11 Analyzing the Performance of the Selection Sort Algorithm The number of visits is of the order n2 . Computer scientists use the big-Oh notation to describe the growth rate of a function. • Using big-Oh notation: The number of visits is O(n2). To convert to big-Oh notation: locate fastest-growing term, and ignore constant coefficient. Copyright © 2014 by John Wiley & Sons. All rights reserved. 12 Programming Question Experimentally measure time it takes selection sort to sort an array for following array sizes : 1000, 2000, 3000, …., 10000 Then plot time(y axis) against the array size(n) long start = System.currentTimeMillis(); sort(a); long end = System.currentTimeMillis(); System.out.println("tme taken (ms): "+ (end-start) ); Copyright © 2014 by John Wiley & Sons. All rights reserved. 13 Answer public static void main(String[] args) { for (int i=1000;i<=10000;i+=1000) { int[] a = randomIntArray(i, 100); long start = System.currentTimeMillis(); sort(a); long end = System.currentTimeMillis(); System.out.println("tme taken (ms): for size "+i+" = "+ (end-start) ); } } O(n2) pattern Copyright © 2014 by John Wiley & Sons. All rights reserved. 14 Common Big-Oh Growth Rates Growth rate increases downwards Copyright © 2014 by John Wiley & Sons. All rights reserved. 15 Insertion Sort Like selection sort, maintains a sorted portion(left) and a unsorted portion (right)in array Fist iteration starts with second element in array and insert it into correct position in sorted portion in array The second iteration looks at the third element and inserts it into the correct position with respect to the first two, so all three elements are in order. and so on…. Copyright © 2014 by John Wiley & Sons. All rights reserved. 16 Insertion Sort Assume initial sequence a[0] . . . a[k] is sorted (k = 0): Add a[1]; element needs to be inserted before 11 Add a[2] Add a[3] Finally, add a[4] Copyright © 2014 by John Wiley & Sons. All rights reserved. 17 Algorithm: for(i=1;i<a.length;i++)//elements in unsorted section { //shift all elements in sorted section> a[i] one position right to make room for a[i] //insert a[i] } Copyright © 2014 by John Wiley & Sons. All rights reserved. 18 Programming Question Implement InsertionSorter.java. You can write your sorting code in sort method that accepts an int array and call it from main. Program template in next slide Copyright © 2014 by John Wiley & Sons. All rights reserved. 19 public class InsertionSorter{ public static void sort(int[] a) { //TODO: implement insertion sort } public static int[] randomIntArray(int length, int n) Random generator = new Random(); int[] a = new int[length]; for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } { public static void main(String[] args) { int[] a = randomIntArray(20, 100); System.out.println("Before: "+ Arrays.toString(a)); sort(a); System.out.println("After: "+ Arrays.toString(a)); } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 20 Answer import java.util.Arrays; import java.util.Random; public class InsertionSorter{ public static void sort(int[] a) { for (int i = 1; i < a.length; i++) { int temp = a[i]; //element in unsorted section to be inserted in sorted section int j; for(j = i-1;j>=0 && temp<a[j] ;j--) //for each element larger than temp in sorted section { a[j+1]= a[j]; //shift one position right } a[j+1] = temp; //insert temp in right position } } public static int[] randomIntArray(int length, int n) Random generator = new Random(); int[] a = new int[length]; for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } public static void main(String[] args) { { int[] a = randomIntArray(100, 100); System.out.println("Before: "+ Arrays.toString(a)); sort(a); System.out.println("After: "+ Arrays.toString(a)); } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 21 Insertion Sort Insertion sort is the method that many people use to sort playing cards. Pick up one card at a time and insert it so that the cards stay sorted. Insertion sort is an O(n2) algorithm. for (int i = 1; i < a.length; i++) { int temp = a[i]; int j; for(j = i-1;j>=0 && temp<a[j] ;j--) { a[j+1]= a[j]; } a[j+1] = temp; } Copyright © 2014 by John Wiley & Sons. All rights reserved. 22 Merge Sort Sorts an array by • Cutting the array in half • Recursively sorting each half • Merging the sorted halves Dramatically faster than the selection sort In merge sort, one sorts each half, then merges the sorted halves. Copyright © 2014 by John Wiley & Sons. All rights reserved. 23 Merge Sort Example Divide an array in half and sort each half Merge the two sorted arrays into a single sorted array Copyright © 2014 by John Wiley & Sons. All rights reserved. 24 Programming Question Implement MergeSorter.java. You can write your sorting code in sort method that accepts an int array and call it from main. public static void sort(int[] a) Note that the base case is an array with only one element and it simply return the same array as sorted array. Steps: • • • • Break array into halves/two sub arrays first, second Sort first half Sort second half Merge first and second half • Implement a method merge for this. Method should accept 3 parameters (first, second, and array into which you want to merge first and second): private static void merge(int[] first, int[] second, int[] a) Copyright © 2014 by John Wiley & Sons. All rights reserved. 25 Merge Sort public static void sort(int[] a) { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; // Copy the first half of a into first, the second half into second . . . sort(first); sort(second); merge(first, second, a); } Copyright © 2014 by John Wiley & Sons. All rights reserved. 26 section_4/MergeSorter.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /** The sort method of this class sorts an array, using the merge sort algorithm. */ public class MergeSorter { /** Sorts an array, using merge sort. @param a the array to sort */ public static void sort(int[] a) { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; // Copy the first half of a into first, the second half into second for (int i = 0; i < first.length; i++) { first[i] = a[i]; } for (int i = 0; i < second.length; i++) { second[i] = a[first.length + i]; } sort(first); sort(second); merge(first, second, a); } Copyright © 2014 by John Wiley & Sons. All rights reserved. Continued 27 section_4/MergeSorter.java 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 /** Merges @param @param @param two sorted arrays into an array first the first sorted array second the second sorted array a the array into which to merge first and second */ private static void merge(int[] first, int[] second, int[] a) { int iFirst = 0; // Next element to consider in the first array int iSecond = 0; // Next element to consider in the second array int j = 0; // Next open position in a // As long as neither iFirst nor iSecond is past the end, move // the smaller element into a while (iFirst < first.length && iSecond < second.length) { if (first[iFirst] < second[iSecond]) { a[j] = first[iFirst]; iFirst++; } else { a[j] = second[iSecond]; iSecond++; } j++; } Copyright © 2014 by John Wiley & Sons. All rights reserved. Continued 28 section_4/MergeSorter.java 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 // Note that only one of the two loops below copies entries // Copy any remaining entries of the first array while (iFirst < first.length) { a[j] = first[iFirst]; iFirst++; j++; } // Copy any remaining entries of the second half while (iSecond < second.length) { a[j] = second[iSecond]; iSecond++; j++; } } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 29 section_4/MergeSortDemo.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.util.Arrays; /** This program demonstrates the merge sort algorithm by sorting an array that is filled with random numbers. */ public class MergeSortDemo { public static void main(String[] args) { int[] a = ArrayUtil.randomIntArray(20, 100); System.out.println(Arrays.toString(a)); MergeSorter.sort(a); System.out.println(Arrays.toString(a)); } } Typical Program Run: [8, 81, 48, 53, 46, 70, 98, 42, 27, 76, 33, 24, 2, 76, 62, 89, 90, 5, 13, 21] [2, 5, 8, 13, 21, 24, 27, 33, 42, 46, 48, 53, 62, 70, 76, 76, 81, 89, 90, 98] Copyright © 2014 by John Wiley & Sons. All rights reserved. 30 Merge Sort Vs Selection Sort Selection sort is an O(n2) algorithm. Merge sort is an O(n log(n)) algorithm. The n log(n) function grows much more slowly than n2. Copyright © 2014 by John Wiley & Sons. All rights reserved. 31 Merge Sort Timing vs. Selection Sort Copyright © 2014 by John Wiley & Sons. All rights reserved. 32 Sort Animator: • http://www.csbsju.edu/computer-science/useful-links/launchcs-applications • Comparison Sorting • • • • • • • • • • Bubble Sort Selection Sort Insertion Sort Shell Sort Merge Sort Quick Sort Bucket Sort Counting Sort Radix Sort Heap Sort From: http://www.cs.usfca.edu/~galles/visualization/ Copyright © 2014 by John Wiley & Sons. All rights reserved. 33 Searching 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 locates a value in an array in O(n) steps Copyright © 2014 by John Wiley & Sons. All rights reserved. 34 Programming Question Implement the class LinearSearch. Implement the method search that performs linear search. The method should accept as parameters an int array and search value. The method should return the index of the value if found in array or -1 otherwise. Then implement the tester class LinearSearchDemo that test sort method of LinearSearch class. A sample output is shown: Program Run: [46, 99, 45, 57, 64, 95, 81, 69, 11, 97, 6, 85, 61, 88, 29, 65, 83, 88, 45, 88] Enter number to search for, -1 to quit: 12 Found in position -1 Enter number to search for, -1 to quit: -1 Copyright © 2014 by John Wiley & Sons. All rights reserved. 35 Answer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LinearSearch.java /** A class for executing linear searches in an array. */ public class LinearSearcher { /** Finds a value in an array, using the linear search algorithm. @param a the array to search @param value the value to find @return the index at which the value occurs, or -1 if it does not occur in the array */ public static int search(int[] a, int value) { for (int i = 0; i < a.length; i++) { if (a[i] == value) { return i; } } return -1; } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 36 LinearSearchDemo.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.util.Arrays; import java.util.Scanner; /** This program demonstrates the linear search algorithm. */ public class LinearSearchDemo { public static void main(String[] args) { int[] a = ArrayUtil.randomIntArray(20, 100); System.out.println(Arrays.toString(a)); Scanner in = new Scanner(System.in); boolean done = false; while (!done) { System.out.print("Enter number to search for, -1 to quit: "); int n = in.nextInt(); if (n == -1) { done = true; } else { int pos = LinearSearcher.search(a, n); System.out.println("Found in position " + pos); } } } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 37 Question If your array is sorted can you do a faster search than linear search? Copyright © 2014 by John Wiley & Sons. All rights reserved. 38 Binary Search A binary search locates a value in a sorted array by: • Get middle element in array • If middle==searhckey return success • Else • Divide array into two halves. • Determining whether the value occurs in the first or second half • Then repeating the search in one of the halves The size of the search is cut in half with each step. Animator: http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html Copyright © 2014 by John Wiley & Sons. All rights reserved. 39 Binary Search Searching for 15 in this array The last value in the first half is 9 • So look in the second (darker colored) half The last value of the first half of this sequence is 17 • Look in the darker colored sequence Copyright © 2014 by John Wiley & Sons. All rights reserved. 40 Binary Search The last value of the first half of this very short sequence is 12 (<15), • This is smaller than the value that we are searching, • so we must look in the second half 15 ≠ 17: we don't have a match Copyright © 2014 by John Wiley & Sons. All rights reserved. 41 Programming Question Implement BinarySearcher class. The search method should implement binary search. The method should return the index of the value if found in array or -1 otherwise. public static int search(int[] a, int low, int high, int value) Original array Copyright © 2014 by John Wiley & Sons. All rights reserved. Index range to search Search key 42 import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class BinarySearcher{ public static int search(int[] a, int low, int high, int value) //TODO : Implement recursive binary search } public static int[] randomIntArray(int length, int n) Random generator = new Random(); int[] a = new int[length]; for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } { { public static void main(String[] args) { int[] a = randomIntArray(20, 100); Arrays.sort(a); System.out.println(Arrays.toString(a)); Scanner in = new Scanner(System.in); System.out.print ("Enter number to search : "); int n = in.nextInt(); int pos = BinarySearcher.search(a, 0, a.length - 1, n); System.out.println("Found in position " + pos); } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 43 Answer import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class BinarySearcher{ public static int search(int[] a, int low, int high, int value) if (low <= high) { int mid = (low + high) / 2; { if (a[mid] == value) return mid; else if (a[mid] < value ) return search(a, mid + 1, high, value); else return search(a, low, mid - 1, value); } else { return -1; } } public static int[] randomIntArray(int length, int n) Random generator = new Random(); int[] a = new int[length]; for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } //main: omitted { } Copyright © 2014 by John Wiley & Sons. All rights reserved. 44 Binary Search Assume n is a power of 2, n = 2m where m = log2(n) Then: T(n) = 1 + log2(n) A binary search locates a value in a sorted array in O(log(n)) steps. Copyright © 2014 by John Wiley & Sons. All rights reserved. 45 Question Suppose you need to look through 1,000,000 records to find a telephone number. How many records do you expect to search (linear) before finding the number? Copyright © 2014 by John Wiley & Sons. All rights reserved. 46 Answer Answer: On average, you’d make 500,000 comparisons. Copyright © 2014 by John Wiley & Sons. All rights reserved. 47 Question Suppose you need to look through a sorted array with 1,000,000 elements to find a value. Using the binary search algorithm, how many records do you expect to search before finding the value? Copyright © 2014 by John Wiley & Sons. All rights reserved. 48 Answer Answer: You would search about 20. (The binary log of 1,024 is 10.) Copyright © 2014 by John Wiley & Sons. All rights reserved. 49 Sorting and Searching in the Java Library - Sorting You do not need to write sorting and searching algorithms • Use methods in the Arrays and Collections classes The Arrays class contains static sort methods. To sort an array of integers: int[] a = . . . ; Arrays.sort(a); • That sort method uses the Quicksort To sort an ArrayList, use Collections.sort ArrayList<String> names = . . .; Collections.sort(names); • Uses merge sort algorithm Copyright © 2014 by John Wiley & Sons. All rights reserved. 50 Comparing Objects Arrays.sort sorts objects of classes that implement Comparable interface: public interface Comparable { int compareTo(Object otherObject); } The call a.compareTo(b) returns • A negative number if a should come before b • 0 if a and b are the same • A positive number otherwise Copyright © 2014 by John Wiley & Sons. All rights reserved. 51 Comparing Objects Some java library classes implement Comparable. • e.g. String and Date You can implement Comparable interface for your own classes. • The Country class could implement Comparable: public class Country implements Comparable { public int compareTo(Object otherObject) { Country other = (Country) otherObject; if (area < other.area) { return -1; } else if (area == other.area) { return 0; } else { return 1; } } } Copyright © 2014 by John Wiley & Sons. All rights reserved. 52 Comparing Objects You could pass an array of countries to Arrays.sort Country[] countries = new Country[n]; Arrays.sort(countries); // Sorts by increasing area Copyright © 2014 by John Wiley & Sons. All rights reserved. 53