Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.

Slides:



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

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.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CS0007: Introduction to Computer Programming Array Algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Programming Logic and Design Fourth Edition, Comprehensive
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
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.
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.
Chapter 8 ARRAYS Continued
Big Oh Algorithm Analysis with
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.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
10-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
Introduction to Search Algorithms
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The 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
Chapter 10 Vectors Computing Fundamentals with C++ 3rd Edition
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
24 Searching and Sorting.
Presentation transcript:

Chapter 8 Search and Sort ©Rick Mercer

Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement and call static methods Declare arrays with two and three subscripts Process data stored in rows and columns

Binary Search Binary search serves the same service as sequential search, but faster. – Especially when there are many array elements You employ a similar algorithm when you look up a name in a phonebook. while the element is not found and it still may be in the array { if the element in the middle of the array is the matches store the reference and signal that the element was found else eliminate correct half of the array from further search }

Binary Search We'll see that binary search can be a more efficient algorithm for searching. It works only on sorted arrays like this – Compare the element in the middle – if that's the target, quit and report success – if the key is smaller, search the array to the left – otherwise search the array to the right This process repeats until we find the target or there is nothing left to search

Some preconditions For Binary Search to work – The array must be sorted – The indexes referencing the first and last elements must represent the entire range of meaningful elements in the array

Binary search a small array  Here is the array that will be searched int n = 9; String[] a = new String[n]; // Insert elements in natural order (according to // the compareTo method of the String class a[0] = "Bob"; a[1] = "Carl"; a[2] = "Debbie"; a[3] = "Evan"; a[4] = "Froggie"; a[5] = "Gene"; a[6] = "Harry"; a[7] = "Igor"; a[8] = "Jose"; // The array is filled to capacity in this example

Initialize other variables Several assignments to get things going int first = 0; int last = n - 1; String searchString = "Harry"; // –1 means the element has not yet been found int indexInArray = -1; The first element to be compared is the one in the middle. int mid = ( first + last ) / 2; If the middle element matches searchString, stop Otherwise move low above mid or high below mid – This will eliminate half of the elements from the search The next slide shows the binary search algorithm

Binary Search Algorithm while indexInArray is -1 and there are more elements { if searchString is equal to name[mid] then let indexInArray = mid // array element equaled searchString else if searchString alphabetically precedes name[mid] eliminate mid... last elements from the search else eliminate first... mid elements from the search mid = ( first + last ) / 2; // Compute a new mid for the next iteration } – Next slide shows mid goes from 4 to (5+8)/2=6

Binary Search Harry Bob Carl Froggie Gene Harry Igor Debbie Evan a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] first mid last first mid found last Jose Datareference pass 1 pass 2

Binary Search Alice not in array Bob Carl Froggie Gene Harry Igor Debbie Evan a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] first mid last first mid last Jose data reference pass 1 pass 2 pass 3 first mid last pass5 At pass4 (not shown) all integers are 0 to consider the first in the array. However at pass5, last becomes -1. At pass5, indexInArray is still -1, however last is < first to indicate the element was not found. The loop test: while(indexInArray == -1 && first <= last) last first mid ?

Binary Search Code while(indexInArray == -1 && (first <= last)) { if( searchString.equals(a[mid] ) ) indexInArray = mid; // found else if( searchString.compareTo(a[mid] ) < 0 ) last = mid-1 ; // may be in 1st half else first= mid+1; // may be in second mid = ( first + last ) / 2; } System.out.println("Index of " + searchString + ": " + indexInArray);

How fast is Binary Search? Best case: 1 Worst case: when target is not in the array At each pass, the "live" portion of the array is narrowed to half the previous size. The series proceeds like this: – n, n/2, n/4, n/8,... Each term in the series is 1 comparison How long does it take to get to 1? – This will be the number of comparisons

Graph Illustrating Relative growth logarithmic and linear n f(n) searching with sequential search searching with binary search

Comparing sequential search to binary search Rates of growth and logarithmic functions

Sorting The process of arranging array elements into ascending or descending order – Ascending (where x is an array object): x[0] <= x[1] <= x[2] <=... <= x[n-2] <= x[n-1] – Descending: x[0] >= x[1] >= x[2] >=... >= x[n-2] >= x[n-1] – Here's the data used in the next few slides:

Swap smallest with the first // Swap the smallest element with the first top = 0 smallestIndex = top for i ranging from top+1 through n – 1 { if test[ i ] < test[ smallestIndex ] then smallestIndex = i } // Question: What is smallestIndex now __________? swap test[ smallestIndex ] with test[ top ]

Selection sort algorithm Now we can sort the entire array by changing top from 0 to n-2 with this loop for (top = 0; top < n-1; top++) for each subarray, move the smallest to the top The top moves down one array position each time the smallest is placed on top Eventually, the array is sorted

Selection sort runtimes Actual observed data on a slow pc in thousands