Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Simple Sorting Algorithms
1 Algorithm Efficiency and Sorting (Walls & Mirrors - Remainder of Chapter 9)
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
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.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 8 ARRAYS Continued
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Applications of Arrays (Searching and Sorting) and Strings
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
CSC 211 Data Structures Lecture 13
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CS 367 Introduction to Data Structures Lecture 11.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
Sorting Mr. Jacobs.
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Chapter 8 Search and Sort
Searching and Sorting Arrays
Searching and Sorting 1-D Arrays
Data Structures Sorted Arrays
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort Section 5 - Merge Sort 1 Go

Searches and Sorts There are two search algorithms and three sort algorithms that you need to know for the AP Exam: Sequential Search also called Linear Search Binary Search Selection Sort Insertion Sort Merge Sort We will discuss each and note some important facts about each one. 2

Chapter 14 - Section 1 Sequential or Linear Search 3

Sequential Search or Linear Search Sequential Search also called Linear Search can be performed on a sorted or unsorted array. As the name indicates, the search process begins at the start of the list (usually a standard array) and checks the first element to see if it is the target value. If it is not, the process continues with each element being checked until the value is found or the end of the list is reached. When it is found, the position of the target value (it’s index) is returned by the method. If the target value is NOT found, then the value -1 is returned. 4

Sequential Search of an Array of ints Only one loop is necessary for Sequential Search. The code is usually contained in a method and a target value being searched for is passed to it. public static int sequentialSearchInts (int [ ] arr, int searchValue) { for (int i = 0; i < arr.length; i++) { if (arr [ i ] == searchValue) { return i; // if found return the index of the target value } return -1; // if target value NOT found return -1 } 5

Sequential Search of an Array of ints The variable checks can be added to see how many checks are needed to find the target value. public static int sequentialSearchInts (int [ ] arr, int target) { checks = 0; for (int i = 0; i < arr.length; i++) { checks++; // increment as a value is checked if (arr [ i ] == target ) { System.out.println(”Found the value after " + checks + " checks!"); return i; } System.out.println("The value was NOT found by Linear Search!"); return -1; } 6

Sequential Search of a 2D Array of ints A two-dimensional array can be searched also, but two loops are necessary. public static int TwoDArraySearchInts (int [ ] [ ] arr, int target) { for (int row = 0; row < arr.length; row ++) { for (int col = 0; col < arr[row].length; col++) { if (arr [ row ][col] == target ) { return i; // if found return the index of the target value } return -1; // if target value NOT found return -1 } 7

Sequential Search of an Array of Strings The code is almost the same for a list of objects, except the equals method is used for the type of object. Here the array contains String values: public static int sequentialSearchStrings (String [ ] arr, String target) { for (int i = 0; i < arr.length; i++) { if ( arr [ i ]. equals (target) ) { return i; // if found return the index of the target value } return -1; // if target value NOT found return -1 } 8

Sequential Search of an Array of Students The code is almost the same for a list of objects, except the equals method is used for the type of object. Here the array contains String values: public static int linearSearchStudents (Student [ ] arr, Student target) { for (int i = 0; i < arr.length; i++) { if ( arr [ i ]. equals (target) ) { return i; // if found return the index of the target value } return -1; // if target value NOT found return -1 } Notice the code is not any different from the code for searching strings because the Student class has an equals method. 9

Sequential Search of an ArrayList of Students The code is almost the same for an ArrayList of Student objects. We only need to replace [ ] with a call to the get method: public static int search (ArrayList arr, Student target) { for (int i = 0; i < arr.size(); i++) { if ( arr.get(i). equals (target) ) { return i; // if found return the index of the target value } return -1; // if target value NOT found return -1 } Notice the code is almost the same as searching a standard array of Student objects. 10

Chapter 14 - Section 2 Binary Search 11

Binary Search Binary Search must be performed on a sorted array. If you try to use it on an unsorted list, you will get erroneous results. You may be told a value was not found when it was actually in the list. Binary Search is a divide and conquer algorithm. The search process begins at the middle of the list. The midpoint is calculated by using the index values of the first array location and the last array location. The code has two variables left and right that refer to those index values. If the value at the midpoint index is the target value, then the search is over and the index value of the midpoint is returned. If the target value is not stored at the midpoint index, then the process continues using a comparison of the target value and the value at the midpoint to see which half of the array will be excluded from the remainder of the search process. 12

Binary Search Code public static int binarySearchInts (int [] arr, int target) { checks = 0; int left = 0; int right = arr.length - 1; while (left <= right) { checks++; int midpoint = (left + right) / 2; if (arr[midpoint] == target) { System.out.println(”Found after " + checks + " checks!"); return midpoint; } else if (arr[midpoint] < target) left = midpoint + 1; else right = midpoint - 1; } System.out.println("The value was NOT found by Binary Search!"); return -1; // if target not found return -1 } 13

Binary Search The Binary Search algorithm uses the following code: midpoint = (left + right) / 2; It is extremely important that you are aware that midpoint, left, and right are int variables, because if the array has 8 elements, then when the search starts, the value of left is 0 and the value of right is 7. (0 + 7) / 2 is equal to 3 NOT 3.5 because this is integer division which truncates the remainder. So the first midpoint value would be the index 3. If the target value is found, then the variables left and right get new values and a new midpoint is checked. 14

Binary Search Because the Binary Search algorithm successively divides the array in half during each check for the target value, its name is representative of what is happening. Mathematically, we can express the number of checks Binary Search takes to find a target value as log 2 n. That is “the log base 2 of n”. So how many checks need to be done to find a target value in an array of size 8? log 2 8 which is equal to log 2 (2 3 ) which is equal to 3. Actually because of the integer division truncation of the statement midpoint = (left + right) / 2; it takes one more (4) when there are eight if the target value is in the last index 7. So we will summarize this for you on the next slide: 15

Binary Search’s Efficiency is log 2 n + 1 If Binary Search looks for a target value in an array of size 7, it takes at most 3 checks to see if the target value is in the list. The numbers in red indicate the 1 st, 2 nd, or 3 rd checks that would be made in finding any of the values in the array or one not in it. [0] [1] [2] [3] [4] [5] [6] However, notice the numbers when there are 8 values in the list: [0] [1] [2] [3] [4] [5] [6] [7]

The Number of Checks for Binary Search So here is the rule on how to find the number of checks it takes to find a target value in an array: You take the log base 2 of the next highest power of 2. value of n 0 – 7 elements takes … log 2 (8) or log 2 (2 3 ) = 3 checks 8 – 15 elements takes … log 2 (16) or log 2 (2 4 ) = 4 checks 16 – 31 elements takes … log 2 (32) or log 2 (2 5 ) = 5 checks elements takes … log 2 (64) or log 2 (2 6 ) = 6 checks elements takes … log 2 (128) or log 2 (2 7 ) = 7 checks This can be summarized in general as:log 2 n + 1 So you can see when n is 8 it takes 4 checks: log = = 4 17

Recursive Binary Search 18

Recursive Binary Search Code public int binarySearch (int[] arr, int target) { int pos = recurBinarySearch (arr, target, 0, arr.length - 1); return pos; } private int recurBinarySearch (int[] arr, int target, int left, int right) { if (left > right) return -1; else { int midpoint = (left + right) / 2; if (arr[midpoint] == target) return midpoint; else if (arr[midpoint] < target) return recurBinarySearch (arr, target, midpoint + 1, right); else return recurBinarySearch (arr, target, left, midpoint - 1); } 19

Efficiency of the Searches The efficiency of Sequential Search is pretty good for small sized arrays up to size Computer processors are fast enough that it would not matter if you are performing a Sequential Search or a Binary Search. However, if the array is much larger, above size 1000, then Binary Search will always be faster than Sequential Search. Here is a table that shows the Best, Worst, and Average case efficiencies of Sequential and Binary Search. Mathematically log 2 n is better than n. Graphing y = log n and y = n shows you why. As n increases log n doesn’t increase as fast as n. Search of n elements Best CaseWorst CaseAverage Case Sequential1 checkn checksn / 2 checks Binary1 checklog 2 n checks 20

Chapter 14 - Section 3 Selection Sort 21

Selection Sort Selection Sort gets its name from the fact that during the first “pass”, the smallest element in the array is “selected” and swapped to the first position in the array. It does this by keeping track of the index where the current smallest element is located using the variable minIndex. As it makes a pass it continually checks all values in order to see if they are smaller than the current smallest element. On the second pass, the next to the smallest element is swapped into the second position in the array and so on until the largest element is placed in the last position in the array. Using this approach, the elements end up in “ascending order” in the array (smallest to largest). The algorithm can also be written to place the largest element in the first position in the array, the next to the largest element in the second position and so on until the elements are in reverse or “descending order”. (largest to smallest ). 22

Selection Sort The main efficiency of Selection Sort is that there is only one swap per pass. In computer time, swaps take longer to process than just checking something. So a lot of swaps can be bad for efficiency. The code can also be written to swap the largest element to the last location of the array, the next to the largest element into the next to the last location in the array and so on. In this case the elements will be in ascending order. Its just that we are manipulating the largest item during each pass instead of the smallest. The code can also be written to swap the smalles element to the last location of the array, the next to the smallest element into the next to the last location in the array and so on. In this case the elements will be in descending order. Its just that we are manipulating the smallest item during each pass instead of the smallest. 23

Selection Sort Let’s say that an array has the following values stored in it with the element 18 stored in index 0: Selection Sort finds that the smallest element is in index 2, so it swaps 6 with the first element 18 stored in index First pass over! 6 is out of the sorting process. Selection Sort finds that the next smallest element is in index 4, so it swaps 8 with the first element in the part of the array still being sorted, Second pass over! 8 is out of the sorting process. Selection Sort finds that the next smallest element is in index 4, so it swaps 13 with the first element in the part of the array still being sorted, Third pass over! 13 is out of the sorting process. Selection Sort finds that the next smallest element is in index 4, so it swaps 18 with the first element in the part of the array still being sorted, Fourth pass over! 18 is out of the sorting process. On the last pass 23 is already in order so no swap is made. 24

Selection Sort Ascending Order for ints public static void selectionSortInts(int [ ] arr) { swaps = 0; int minIndex; for (int i = 0; i < arr.length - 1; i++) { minIndex = i; for (int index = i + 1; index < arr.length; index++) if (arr[index] < arr[minIndex]) minIndex = index; int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex]= temp; swaps++; } System.out.println("It took " + swaps + " swaps using Selection Sort!"); } 25

Selection Sort Descending Order for ints public static void selectionSortInts(int [ ] arr) { swaps = 0; int maxIndex; for (int i = 0; i < arr.length - 1; i++) { maxIndex = i; for (int index = i + 1; index < arr.length; index++) if (arr[index] > arr[maxIndex]) maxIndex= index; int temp = arr[i]; arr[i] = arr[maxIndex]; arr[maxIndex]= temp; swaps++; } System.out.println("It took " + swaps + " swaps using Selection Sort!"); } 26

Using compareTo for Sorting To be able to sort String values, we need to use the compareTo method of the String class. If we need to sort values of another kind of object, then we need to make sure there is a compareTo method of that class. Let’s review the information on the compareTo method of the String class originally given you in the Chapter 7 PowerPoint on Strings. 27

Sect 6 - int compareTo (String other) Consider the code: String s1 = reader.nextLine(); String s2 = reader.nextLine(); In the call of the method compareTo below, s1 is this and s2 is other ….. if (s1.compareTo(s2) == 0) and the compareTo method returns one of the following values when it is made: a negative integer if this is lexicographically less than other 0 if this is equal to other a positive integer if this is lexicographically greater than other One thing the compareTo() method is valuable for is to help us sort String values in a list. We will eventually do this. 28

Sect 6 - int compareTo (String other) The following code compares the string values in word1 and word2 to identify the lexicographical order of word1 and word2. String word1 = reader.nextLine(); String word2 = reader.nextLine(); if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); 29 lexicographical indicates alphabetical order

Sect 6 - int compareTo (String other) If word1 comes before word2, then a negative int value is returned by word1.compareTo (word2 ). If word1 comes after word2, then a positive int value is returned. If word1 exactly equals word2, then zero is returned. if (word1.compareTo (word2 ) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2 ) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2 ) == 0) System.out.println(word1 + “ is equal to ” + word2); 30

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “a”; String word2 = “b”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “a comes before b” Note: “a”.compareTo(“b”) returns -1 based on the Unicode (ASCII) values for “a” and “b” … = Note that word1 is calling the method.

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “a”; String word2 = “c”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “a comes before b” Note: “a”.compareTo(“c”) returns -2 based on the Unicode values for “a” and “c” … = -2 32

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “apple”; String word2 = “banana”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “apple comes before banana” Note: “apple”.compareTo(“banana”) returns -1 based on the Unicode values for “a” and “b” … = -1. Only the first two characters are compared, because they are different. 33

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “b”; String word2 = “a”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “b comes after a” Note: “b”.compareTo(“a”) returns +1 based on the Unicode values for “b” and “a” … = 1 34

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “d”; String word2 = “a”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “b comes after a” Note: “d”.compareTo(“a”) returns +3 based on the Unicode values for “d” and “a” … = 3 35

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “bear”; String word2 = “bearcats”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “banana comes after apple” Note: “bear”.compareTo(“bearcats”) returns -4 based on the fact that the words are the same but the second one has 4 additional characters. “bearcats”.compareTo(“bear”) returns

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “bearclaw”; String word2 = “bearcats”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “banana comes after apple” Note: “bearclaw”.compareTo(“bearcats”) returns +11 based on the Unicode values for “l” and “a” … = 11. Only the first two characters that are different are compared. 37

Sect 6 - int compareTo (String other) In this code, word1 is the receiver object this and word2 is other. String word1 = “bearcats”; String word2 = “bearclaw”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “banana comes after apple” Note: “bearclaw”.compareTo(“bearcats”) returns -11 based on the Unicode values for “a” and “l” … = -11. Only the first two characters that are different are compared. 38

Writing a Student Class compareTo Method You will also write a compareTo Method in the Student class. It can be used to sort a list of Student objects. A class like Student or Person can only properly include a compareTo method if it implements the Comparable interface using templating in the class definition line: public class Person implements Comparable Let’s look at what a compareTo method of the Person class would look like on the next slide. 39

CompareTo Method for the Person class public int compareTo(Person other) // note the parameter is Person not Object { if (! (other instanceof Person )) throw new IllegalArgumentException("Parameter must be a Person."); if( this.name.compareTo(other.name) == 0) { if ( this.address.equals(other.address) ) return 0; else if ( this.address.compareTo(other.address) < 0 ) return -1; else // if ( this.address.compareTo(other.address) > 0) return 1; } else if ( this.name.compareTo(other.name) < 0 ) return -1; else // if ( this.name.compareTo(other.name) > 0 ) return 1; } 40

The Selection Sort Code for Strings public static void selectionSortStrings(String [ ] arr) { swaps = 0; int minIndex; for (int i = 0; i < arr.length - 1; i++) { minIndex = i; for (int index = i + 1; index < arr.length; index++) if (arr[index].compareTo(arr[minIndex]) < 0) minIndex = index; int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex]= temp; swaps++; } System.out.println("It took " + swaps + " swaps using Selection Sort!"); } 41

The Selection Sort Code for Students public static void selectionSortStudents(Student [ ] arr) { swaps = 0; int minIndex; for (int i = 0; i < arr.length - 1; i++) { minIndex = i; for (int index = i + 1; index < arr.length; index++) if (arr[index].compareTo(arr[minIndex]) < 0) minIndex = index; int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex]= temp; swaps++; } System.out.println("It took " + swaps + " swaps using Selection Sort!"); } 42

Selection Sort Note that all versions of the algorithm have a nested loop structure. If there are n elements in the array, the outer loop runs approximately n times and the inner for loop runs approximately n times. This fact causes this algorithm to have an efficiency on the order of n * n or n 2. Actually for this version of Selection Sort, the outside for loop runs n - 1 times. The inside loop runs a number of times based on the outside loop lcv: During first iteration of outside loop, the inner loop runs n times. During second iteration of outside loop, the inner loop runs n - 1 times. During third iteration of outside loop, the inner loop runs n - 2 times. During fourth iteration of outside loop, the inner loop runs n - 3 times. and so on. 43

Chapter 14 - Section 4 Insertion Sort 44

Insertion Sort Insertion Sort gets its name from the fact that during each “pass”, the element being sorted is being “inserted” into its proper position in the array. The position is temporary if other elements when they are sorted later move the element to a new position. This is similar to placing cards in your hand in order, as they are dealt to you one at a time. The algorithm begins with the second element in the array. If it is smaller than the first element, then the two elements are swapped. Next, the third element in the array is tested to see if it is smaller than the second element. If it is, then the two are swapped. If the third element was swapped with the second, it is then tested to see if it is smaller than the first element. If it is, then they are swapped. The next slide shows an example. 45

Insertion Sort Let’s say that an array has the following values stored in it with the element 18 stored in index 0: Since Insertion Sort always begins with the second element it starts with 13 to see is it is smaller than 18. If it is then it swaps.13 and The first pass is over! As the second pass begins, Insertion Sort checks to see if 6 (above) is less than 18. If it is then it swaps them, so temporarily we have … But the second pass is NOT over! Then Insertion Sort checks to see if 6 is less than 13. If it is then it swaps them and we have … The second pass is over! As the third pass begins, Insertion Sort checks to see if 23 is less than 18. It isn’t so no swap is made and the third pass is over! 46

Insertion Sort public static void insertionSortInts(int[] arr) { swaps = 0; int itemToInsert, j; boolean stillLooking; for (int k = 1; k < arr.length; k++) { itemToInsert = arr[k]; j = k - 1; stillLooking = true; while ((j >= 0) && stillLooking) { if (itemToInsert < arr[j]) { arr[j + 1] = arr[j]; j--; } else stillLooking = false; } arr[j + 1] = itemToInsert; swaps++; } System.out.println("It took " + swaps + " swaps to sort this array!"); } 47

Insertion Sort with String Values public static void insertionSortStrings (String [ ] arr) { swaps = 0; int itemToInsert, j; boolean stillLooking; for (int k = 1; k < arr.length; k++) { itemToInsert = arr[k]; j = k - 1; stillLooking = true; while ((j >= 0) && stillLooking) { if (itemToInsert.compareTo(arr[j]) < 0) { arr[j + 1] = arr[j]; j--; } else stillLooking = false; } arr[j + 1] = itemToInsert; swaps++; } System.out.println("It took " + swaps + " swaps to sort this array!"); } 48

Insertion Sort Note that the algorithm has a nested for loop structure. If there are n elements in the array, the outer loop runs approximately n times and the inner for loop runs approximately n times. This fact causes this algorithm to have an efficiency on the order of n * n or n 2. Actually for this version of Insertion Sort, the outside for loop runs n - 1 times. The inside loop runs a number of times based on the outside loop lcv: During first iteration of outside loop, the inner loop runs 1 time. During second iteration of outside loop, the inner loop runs 2 times. During third iteration of outside loop, the inner loop runs 3 times. During fourth iteration of outside loop, the inner loop runs 4 times. and so on up to n-1 times on the last iteration of the outside loop. 49

Chapter 14 - Section 5 Merge Sort 50

Merge Sort The Merge Sort algorithm is a divide and conquer algorithm. It recursively divides the original array into two equal parts It does this by copying the left half into one temporary array and the right half into another temporary array. If the size of either array is larger than two, then it divides them in half again copying the left most part into another temporary array and the right half into another temporary array. When the left and right arrays contain only one element each, then the Merge part of the algorithm places the two elements back into an array in the correct order. This happens at all levels of recursion and produces a totally sorted array of elements. This will be demonstrated for you in class with sorting cups, but it can be show here on the next slide. 51

Merge Sort Let’s say that an array has the following values stored in it with the element 18 stored in index 0. The underlined elements show how they are split in half and how they are merged back together arrays are split in half arrays split in half again elements merged in order elements merged in order elements merged in order With n elements, there are log 2 n subdivisions. This gives an efficiency of n*log 2 n which is better than n*n or n 2. This means that Merge Sort is more efficient than Selection Sort or Insertion Sort which are both n 2 sorts. 52

Merge Method Code For the AP Exam, you only need to be familiar with the Merge part of the Merge Sort algorithm, so we will show you only the Merge method here: (this is the first part) private static void merge (int [] a, int[] temp, int from, int middle, int to) { int i = from, j = middle + 1, k = from; // While both arrays have elements left unprocessed: while (i <= middle && j <= to) { if (a[i] < a[j]) // compare the first one in each of the two "subarrays" { temp[k] = a[i]; i++; } else { temp[k] = a[j]; j++; } k++; } 53

Merge Method Code For the AP Exam, you only need to be familiar with the Merge part of the Merge Sort algorithm, so we will show you only the Merge method here: (this is the second part) // Copy the tail of the first half, if any, into temp: while (i <= middle) { temp[k] = a[i]; // Or simply temp[k++] = a[i++] i++; k++; } // Copy the tail of the second half, if any, into temp: while (j <= to) { temp[k] = a[j]; // Or simply temp[k++] = a[j++] j++; k++; } // Copy temp back into a for (k = from; k <= to; k++) a[k] = temp[k]; } // end merge method 54

Merge Method Code For the AP Exam, you only need to be familiar with the Merge part of the Merge Sort algorithm, so we will show you only the Merge method here: public static void merge (int[ ] a, int[ ] copyBuffer, int low, int middle, int high) { // initialize i1 and i2 to the first items in each subarray int i1 = low, i2 = middle + 1; // Interleave items from the subarrays into the copyBuffer in such a way // that order is maintained. for (int i = low; i <= high; i++) { if (i1 > middle) copyBuffer[i] = a[i2++];// first subarray exhausted else if (i2 > high) copyBuffer[i] = a[i1++];// second subarray exhausted else if (a[i1] < a[i2]) copyBuffer[i] = a[i1++];// item in first subarray is less else copyBuffer[i] = a[i2++];// item in second subarray is less } for (int i = low; i <= high; i++) // copy sorted items back into a[i] = copyBuffer[i];// proper position in a } // end merge method 55

Search & Sort Summary AlgorithmHow it Works Sequential Search Begins at the first of the array and searches sequentially in order to find the target value. The array can be sorted or unsorted. On average the target value will be found in n/2 checks. Binary Search Begins at the middle of the array and checks for the target value, returning it if it is found. Otherwise, it excludes the half of the array the target can’t be in and goes to the midpoint of the half of the array it could be in. This continues until the target value is found or not found taking less than or equal to log2n + 1 checks. The array must be sorted. It is more efficient that Sequential Search for large arrays. It is a divide and conquer algorithm. Selection Sort On the first pass, swaps the smallest element into the first index location. On the second pass, swaps the next to smallest element into the second index location, etc. Makes only one swap per pass unless the smallest element for that pass is already in its correct final position. No particular worse case scenario. Insertion Sort Begins by placing the second element in order with the first element. On the second pass, places the third element in order with respect to the first two elements, and so on. Similar to placing cards in your hand in order while they are being dealt. Worse case scenario is when the array is in reverse order. Merge Sort It is a divide and conquer algorithm. It successively divides an array in half, keeping track of each half along the way until each part contains only one element. It then merges the half arrays back together with the elements in order. It is more efficient than Selection Sort or Insertion Sort. 56