Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.

Slides:



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

CSE Lecture 3 – Algorithms I
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Polymorphism Polymorphism (having many forms) is an object- oriented concept.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 11 Sorting.
Chapter Day 22. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 22 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21 (right.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Algorithm Efficiency and Sorting
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 10 Sorting and Searching.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
Chapter 9 Searching and Sorting
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
CS1101: Programming Methodology Aaron Tan.
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.
CS1101: Programming Methodology
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.
ELC 310 Day 19. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
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,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Sequential (Linear) Binary Selection** Insertion** Merge.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
Chapter 12 Recursion.
Chapter 9 Polymorphism.
Warmup What is an abstract class?
Chapter 13: Searching and Sorting
Sorts.
Data Structures and Algorithms
Outline Late Binding Polymorphism via Inheritance
Sorting and Searching -- Introduction
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Arrays.
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Searching Searching is the process of finding a target element within a group of items called the search pool The target may or may not be in the search pool We want to perform the search efficiently, minimizing the number of comparisons Let's look at two classic searching approaches: linear search and binary search

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polymorphism in Searching Recall that an class that implements the Comparable interface defines a compareTo method to determine the relative order of its objects We can use polymorphism to develop a generic search for any set of Comparable objects The searching method accepts as a parameter an array of Comparable objects That way, one method can be used to search target from a group of People, or Books, or whatever

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Linear Search A linear search begins at one end of a list and examines each element in turn Eventually, either the item is found or the end of the list is encountered See PhoneList2.java PhoneList2.java See Searching.java, specifically the linearSearch method Searching.java

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Search A binary search assumes the list of items in the search pool is sorted It eliminates a large part of the search pool with a single comparison A binary search first examines the middle element of the list -- if it matches the target, the search is over If it doesn't, only one half of the remaining elements need be searched Since they are sorted, the target can only be in one half of the other number

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Successful Search #1 high low lowhighmid mid 4 search( 44 ) < 44 low = mid+1 = 5

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Successful Search - 2 search( 44 ) lowhighmid 0808 #1 highlow 5858 # < 77 high = mid-1=5 mid 6 4

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Successful Search - 3 search( 44 ) lowhighmid 0808 # #2 highlow5 # == 44 mid 5 Successful Search!! 4 6

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Unsuccessful Search < 45 low = mid+1 = 5 lowhighmid 0808 #1 high low mid search( 45 )

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Unsuccessful Search < 77 high = mid-1=5 highlow 5858 # search( 45 ) lowhighmid 0808 #1 4 mid 6

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Unsuccessful Search < 45 highlow5 #3 low = mid+1 = 6 search( 45 ) lowhighmid 0808 # #2 mid

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sequence of Unsuccessful Search - 4 Unsuccessful Search low > high no more elements to search search( 45 ) lowhighmid 0808 # #2 65 #3 5 highlow 6565 #4

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Search Routine public int binarySearch ( int[] number, int searchValue ) { int low = 0, high = number.length - 1, mid= (low + high) / 2; while ( low <= high && number[mid] != searchValue ) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if ( low > high) { mid = NOT_FOUND; } return mid; }

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Search Performance Successful Search –Best Case – 1 comparison –Worst Case – log 2 N comparisons Unsuccessful Search –Best Case =Worst Case – log 2 N comparisons Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comparing N and log 2 N Performance Array SizeLinear – NBinary – log 2 N

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Search Another Polymorphism Implementation of Binary Search See PhoneList2.java PhoneList2.java See Searching.java, specifically the binarySearch method Searching.java

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 scores in ascending numeric order –sorting a list of people alphabetically by last name There are many algorithms, which vary in efficiency, for sorting a list of items We will examine two specific algorithms: –Selection Sort –Insertion Sort

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Selection Sort The approach of Selection Sort: –select a value and put it in its final place into the list –repeat for all other values In more detail: –find the smallest value in the list –switch it with the value in the first position –find the next smallest value in the list –switch it with the value in the second position –repeat until all values are in their proper places

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Selection Sort An example: original: smallest is 1: smallest is 2: smallest is 3: smallest is 6: Each time, the smallest remaining value is found and exchanged with the element in the "next" position to be filled

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Swapping The processing of the selection sort algorithm includes the swapping of two values Swapping requires three assignment statements and a temporary storage location: temp = first; first = second; second = temp;

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Selection Sort As we did with searching, we'll implement the sorting with polymorphic Comparable parameters The sorting method doesn't "care" what it is sorting, it just needs to be able to call the compareTo method That is guaranteed by using Comparable as the parameter type Also, this way each class decides for itself what it means for one object to be less than another See Sorting.java, specifically the selectionSort method Sorting.java See PhoneList.java Contact.java PhoneList.java Contact.java

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Insertion Sort The approach of Insertion Sort: –pick any item and insert it into its proper place in a sorted sublist –repeat until all items have been inserted In more detail: –consider the first item to be a sorted sublist (of one item) –insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition –insert the third item into the sorted sublist (of two items), shifting items as necessary –repeat until all values are inserted into their proper positions

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Insertion Sort An example: original: insert 9: insert 6: insert 1: insert 2: See Sorting.java, specifically the insertionSort method Sorting.java

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comparing Sorts The Selection and Insertion sort algorithms are similar in efficiency They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list Approximately n 2 number of comparisons are made to sort a list of size n We therefore say that these sorts are of order n 2 Other sorts are more efficient: order n log 2 n

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exercise Pp9.4 Rewrite the Sorting class so that both sorting algorithms put the values in descending order. Create a driver class with a main method to exercise the modifications.