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.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

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.
CMPS1371 Introduction to Computing for Engineers SORTING.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick.
Sorting Algorithms: Merge and Quick. Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement.
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Sorting Algorithms: Selection, Insertion and Bubble.
Analysis of Algorithm.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fourteen: Sorting and Searching.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Sorting and Searching.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Recursion A recursive computation solves a problem by using the solution of the same problem with simpler values The same computation occurs repeatedly.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
Chapter 19 Searching, Sorting and Big O
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
CHAPTER 18 SORTING AND SEARCHING. CHAPTER GOALS To study the several searching and sorting algorithms To appreciate that algorithms for the same task.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Chapter 15: Algorithms 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 15 Algorithms.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Comparison-Based Sorting & Analysis Smt Genap
Sort Algorithms.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 14 Sorting and Searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Fibonacci Sequence Fibonacci sequence is a sequence of numbers defined by f 1 = 1 f 2 = 1 f n = f n-1 + f n-2 First ten terms – 1, 1, 2, 3, 5, 8, 13, 21,
Searching and Sorting Searching algorithms with simple arrays
Using recursion for Searching and Sorting
Sorting Chapter 14.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Sorting Mr. Jacobs.
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 19 Sorting and Searching
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
14 SORTING AND SEARCHING CHAPTER
Sub-Quadratic Sorting Algorithms
Chapter 14 – Sorting and Searching
Review of Searching and Sorting Algorithms
CSE 373 Data Structures and Algorithms
Searching/Sorting/Searching
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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:  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

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 (n - 1) (n - 2) This can be simplified to n 2 /2 + 5n/ n/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

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.

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, ….,  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(n 2 ) 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(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; }

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

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

Copyright © 2014 by John Wiley & Sons. All rights reserved.27 section_4/MergeSorter.javaMergeSorter.java 30 /** 31 Merges 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 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 // 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

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 }

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)); MergeSorter.sort(a); 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]

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.

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: cs-applications 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:

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

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 }

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

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==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:

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

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 }

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

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.

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