Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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?

2 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.

3 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

4 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

5 Copyright © 2014 by John Wiley & Sons. All rights reserved.5  Selection sort pseudocode:  swap(A(i), A(iMin)): temp = A(i) A(i) = A(iMin) A(iMin) = temp 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

6 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

7 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)); }

8 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)); }

9 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

10 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 n 2 /2 + 5n/2 - 3 5n/2 - 3 is small compared to n 2 /2 – so let's ignore it Also ignore the 1/2 – it cancels out when comparing ratios

11 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 n 2.  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(n 2 ).  To convert to big-Oh notation: locate fastest-growing term, and ignore constant coefficient.

12 Copyright © 2014 by John Wiley & Sons. All rights reserved.12 Homework: 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) );

13 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(n 2 ) pattern

14 Copyright © 2014 by John Wiley & Sons. All rights reserved.14 Common Big-Oh Growth Rates Growth rate increases downwards

15 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….

16 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]

17 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] }

18 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

19 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)); }

20 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)); } }

21 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(n 2 ) 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; }

22 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.

23 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

24 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.  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) public static void sort(int[] a)

25 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); }

26 Copyright © 2014 by John Wiley & Sons. All rights reserved.26 section_4/MergeSorter.javaMergeSorter.java 1 /** 2 The sort method of this class sorts an array, using the merge 3 sort algorithm. 4 */ 5 public class MergeSorter 6 { 7 /** 8 Sorts an array, using merge sort. 9 @param a the array to sort 10 */ 11 public static void sort(int[] a) 12 { 13 if (a.length <= 1) { return; } 14 int[] first = new int[a.length / 2]; 15 int[] second = new int[a.length - first.length]; 16 // Copy the first half of a into first, the second half into second 17 for (int i = 0; i < first.length; i++) 18 { 19 first[i] = a[i]; 20 } 21 for (int i = 0; i < second.length; i++) 22 { 23 second[i] = a[first.length + i]; 24 } 25 sort(first); 26 sort(second); 27 merge(first, second, a); 28 } 29 Continued

27 Copyright © 2014 by John Wiley & Sons. All rights reserved.27 section_4/MergeSorter.javaMergeSorter.java 30 /** 31 Merges two sorted arrays into an array 32 @param first the first sorted array 33 @param second the second sorted array 34 @param a the array into which to merge first and second 35 */ 36 private static void merge(int[] first, int[] second, int[] a) 37 { 38 int iFirst = 0; // Next element to consider in the first array 39 int iSecond = 0; // Next element to consider in the second array 40 int j = 0; // Next open position in a 41 42 // As long as neither iFirst nor iSecond is past the end, move 43 // the smaller element into a 44 while (iFirst < first.length && iSecond < second.length) 45 { 46 if (first[iFirst] < second[iSecond]) 47 { 48 a[j] = first[iFirst]; 49 iFirst++; 50 } 51 else 52 { 53 a[j] = second[iSecond]; 54 iSecond++; 55 } 56 j++; 57 } 58 Continued

28 Copyright © 2014 by John Wiley & Sons. All rights reserved.28 section_4/MergeSorter.javaMergeSorter.java 59 // Note that only one of the two loops below copies entries 60 // Copy any remaining entries of the first array 61 while (iFirst < first.length) 62 { 63 a[j] = first[iFirst]; 64 iFirst++; j++; 65 } 66 // Copy any remaining entries of the second half 67 while (iSecond < second.length) 68 { 69 a[j] = second[iSecond]; 70 iSecond++; j++; 71 } 72 } 73 }

29 Copyright © 2014 by John Wiley & Sons. All rights reserved.29 section_4/MergeSortDemo.javaMergeSortDemo.java 1 import java.util.Arrays; 2 3 /** 4 This program demonstrates the merge sort algorithm by 5 sorting an array that is filled with random numbers. 6 */ 7 public class MergeSortDemo 8 { 9 public static void main(String[] args) 10 { 11 int[] a = ArrayUtil.randomIntArray(20, 100); 12 System.out.println(Arrays.toString(a)); 13 14 MergeSorter.sort(a); 15 16 System.out.println(Arrays.toString(a)); 17 } 18 } 19 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]

30 Copyright © 2014 by John Wiley & Sons. All rights reserved.30 Merge Sort Vs Selection Sort  Selection sort is an O(n 2 ) algorithm.  Merge sort is an O(n log(n)) algorithm.  The n log(n) function grows much more slowly than n 2.

31 Copyright © 2014 by John Wiley & Sons. All rights reserved.31 Merge Sort Timing vs. Selection Sort

32 Copyright © 2014 by John Wiley & Sons. All rights reserved.32  Sort Animator: http://www.csbsju.edu/computer-science/useful-links/launch- cs-applications http://www.csbsju.edu/computer-science/useful-links/launch- cs-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/

33 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

34 Copyright © 2014 by John Wiley & Sons. All rights reserved.34 Programming Question  Implement the class LinearSearchDemo. Implement the method search that performs linear search. Method should return index of search key if found or -1 otherwise  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 Find program template in next slide

35 Copyright © 2014 by John Wiley & Sons. All rights reserved.35 import java.util.Arrays; import java.util.Scanner; 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 = search(a, n); System.out.println("Found in position " + pos); } public static int search(int[] a, int value) { //TODO }

36 Copyright © 2014 by John Wiley & Sons. All rights reserved.36 Answer 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 = search(a, n); System.out.println("Found in position " + pos); } public static int search(int[] a, int value) { for (int i = 0; i < a.length; i++) { if (a[i] == value) { return i; } } return -1; } LinearSearch.java

37 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?

38 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==searhckeyreturn 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 http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html

39 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

40 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

41 Copyright © 2014 by John Wiley & Sons. All rights reserved.41 Programming Question  Implement BinarySearchDemo 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 Index range to searchSearch key

42 Copyright © 2014 by John Wiley & Sons. All rights reserved.42 import java.util.Arrays; import java.util.Scanner; /** This program demonstrates the binary search algorithm. */ public class BinarySearchDemo { public static void main(String[] args) { // Construct random array int[] a = ArrayUtil.randomIntArray(20, 100); Arrays.sort(a); 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 = search(a, 0, a.length - 1, n); System.out.println("Found in position " + pos); } public static int search(int[] a, int low, int high, int value) { //TODO }

43 Copyright © 2014 by John Wiley & Sons. All rights reserved.43 Answer import java.util.Arrays; import java.util.Scanner; public class BinarySearchDemo{ public static void main(String[] args) { // Construct random array int[] a = ArrayUtil.randomIntArray(20, 100); Arrays.sort(a); 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 = search(a, 0, a.length - 1, n); System.out.println("Found in position " + pos); } 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; }

44 Copyright © 2014 by John Wiley & Sons. All rights reserved.44 Binary Search  Assume n is a power of 2, n = 2 m where m = log 2 (n)  Then: T(n) = 1 + log 2 (n)  A binary search locates a value in a sorted array in O(log(n)) steps.

45 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?

46 Copyright © 2014 by John Wiley & Sons. All rights reserved.46 Answer Answer: On average, you’d make 500,000 comparisons.

47 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?

48 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.)

49 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 names =...; Collections.sort(names); Uses merge sort algorithm

50 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


Download ppt "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."

Similar presentations


Ads by Google