 1992-2007 Pearson Education, Inc. All rights reserved. 1 16 Searching and Sorting.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

CSE Lecture 3 – Algorithms I
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
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
HST 952 Computing for Biomedical Scientists Lecture 9.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
COMP s1 Computing 2 Complexity
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 19 Searching, Sorting and Big O
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Chapter 19: Searching and Sorting Algorithms
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
1 Searching and Sorting Linear Search Binary Search.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
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,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Chapter 19 Searching and Sorting
16 Searching and Sorting.
19 Searching and Sorting.
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 20 Searching and Sorting
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
MSIS 655 Advanced Business Applications Programming
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

 Pearson Education, Inc. All rights reserved Searching and Sorting

 Pearson Education Inc. All rights reserved. 2 With sobs and tears he sorted out Those of the largest size... — Lewis Carroll Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. — Robert Herrick ‘Tis in my memory lock’d, And you yourself shall keep the key of it. — William Shakespeare It is an immutable law in business that words are words, explanations are explanations, promises are promises — but only performance is reality. — Harold S. Green

 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To search for a given value in an array using linear search and binary search.  To sort arrays using the iterative selection and insertion sort algorithms.  To sort arrays using the recursive merge sort algorithm.  To determine the efficiency of searching and sorting algorithms.  To use loop invariants to help ensure the correctness of your programs.

 Pearson Education, Inc. All rights reserved Introduction 16.2 Searching Algorithms Linear Search Binary Search 16.3 Sorting Algorithms Selection Sort Insertion Sort Merge Sort 16.4 Invariants 16.5 Wrap-up

 Pearson Education, Inc. All rights reserved Introduction Searching – Determining whether a search key is present in data Sorting – Places data in order based on one or more sort keys

 Pearson Education, Inc. All rights reserved. 6 Fig | Searching and sorting algorithms in this text.

 Pearson Education, Inc. All rights reserved Searching Algorithms Examples of searching – Looking up a phone number – Accessing a Web site – Checking a word in the dictionary

 Pearson Education, Inc. All rights reserved Linear Search Linear search – Searches each element sequentially – If search key is not present Tests each element When algorithm reaches end of array, informs user search key is not present – If search key is present Test each element until it finds a match

 Pearson Education, Inc. All rights reserved. 9

10 Iterate through arrayTest each element sequentially Return index containing search key

 Pearson Education, Inc. All rights reserved. 11

 Pearson Education, Inc. All rights reserved. 12

 Pearson Education, Inc. All rights reserved. 13 Efficiency of Linear Search Big O Notation – Indicates the worst-case run time for an algorithm – In other words, how hard an algorithm has to work to solve a problem – Constant run time O(1) Does not grow as the size of the array increases – Linear run time O(n) Grows proportional to the size of the array

 Pearson Education, Inc. All rights reserved. 14 Efficiency of Linear Search Big O Notation – Constant run time O(1) Does not grow as the size of the array increases – Linear run time O(n) Grows proportional to the size of the array – Quadratic run time O(n 2 ) Grows proportional to the square of the size of the array

 Pearson Education, Inc. All rights reserved. 15 Efficiency of Linear Search Linear search algorithm – O(n) – Worst case: algorithm checks every element before being able to determine if the search key is not present – Grows proportional to the size of the array

 Pearson Education, Inc. All rights reserved. 16 Performance Tip 16.1 Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realize maximum performance.

 Pearson Education, Inc. All rights reserved Binary Search Binary search – More efficient than linear search – Requires elements to be sorted – Tests the middle element in an array If it is the search key, algorithm returns Otherwise, if the search key is smaller, eliminates larger half of array If the search key is larger, eliminates smaller half of array – Each iteration eliminates half of the remaining elements

 Pearson Education, Inc. All rights reserved. 18

 Pearson Education, Inc. All rights reserved. 19 Store high, low and middle of remaining array to search Loop until key is found or no elements left to search If search element is the middle element Return middle elementIf search element is less than middle element Eliminate higher halfElse, eliminate lower half

 Pearson Education, Inc. All rights reserved. 20 Update middle of array Return location of element

 Pearson Education, Inc. All rights reserved. 21

 Pearson Education, Inc. All rights reserved. 22

 Pearson Education, Inc. All rights reserved. 23

 Pearson Education, Inc. All rights reserved. 24 Efficiency of Binary Search Binary search – Each comparison halves the size of the remaining array – Results in O(log n) – Called logarithmic run time

 Pearson Education, Inc. All rights reserved Sorting Algorithms Sorting data – Placing data into some particular order A bank sorts checks by account number Telephone companies sort accounts by name – End result is always the same – a sorted array – Choice of algorithm affects how you achieve the result and, most importantly, how fast you achieve the result

 Pearson Education, Inc. All rights reserved Selection Sort Selection sort – Simple, but inefficient sorting algorithm – First iteration selects smallest element in array and swaps it with the first element – Each iteration selects the smallest remaining unsorted element and swaps it with the next element at the front of the array – After i iterations, the smallest i elements will be sorted in the first i elements of the array

 Pearson Education, Inc. All rights reserved. 27 Variable to store index of smallest element Loop length – 1 times

 Pearson Education, Inc. All rights reserved. 28 Loop over remaining elements Locate smallest remaining element Swap smallest element with first unsorted element

 Pearson Education, Inc. All rights reserved. 29

 Pearson Education, Inc. All rights reserved. 30

 Pearson Education, Inc. All rights reserved. 31

 Pearson Education, Inc. All rights reserved. 32 Efficiency of Selection Sort Selection sort – Outer for loop iterates over n – 1 elements – Inner for loop iterates over remaining elements in the array – Results in O(n 2 )

 Pearson Education, Inc. All rights reserved Insertion Sort Insertion sort – Another simple, but inefficient sorting algorithm – First pass takes the second element and inserts it into the correct order with the first element – Each iteration takes the next element in the array and inserts it into the sorted elements at the beginning of the array – After i iterations, the first i elements of the array are in sorted order

 Pearson Education, Inc. All rights reserved. 34

 Pearson Education, Inc. All rights reserved. 35 Declare variable to store element to be inserted Iterate over length – 1 elementsStore value to insertSearch for location to place inserted element Move one element to the rightDecrement location to insert element Insert element into sorted place

 Pearson Education, Inc. All rights reserved. 36

 Pearson Education, Inc. All rights reserved. 37

 Pearson Education, Inc. All rights reserved. 38

 Pearson Education, Inc. All rights reserved. 39

 Pearson Education, Inc. All rights reserved. 40 Efficiency of Insertion Sort Insertion sort – Outer for loop iterates over n – 1 elements – Inner while loop iterates over preceding elements of array – Results in O(n 2 )

 Pearson Education, Inc. All rights reserved Merge Sort Merge sort – More efficient sorting algorithm, but also more complex – Splits array into two approximately equal sized subarrays, sorts each subarray, then merges the subarrays – The following implementation is recursive Base case is a one-element array which cannot be unsorted Recursion step splits an array into two pieces, sorts each piece, then merges the sorted pieces

 Pearson Education, Inc. All rights reserved. 42 Call recursive helper method

 Pearson Education, Inc. All rights reserved. 43 Test for base caseCompute middle of array Compute element one spot to right of middle Recursively sort first half of arrayRecursively sort second half of array Merge the two sorted subarrays

 Pearson Education, Inc. All rights reserved. 44 Index of element in left arrayIndex of element in right arrayIndex to place element in combined array Array to hold sorted elements Loop until reach end of either arrayDetermine smaller of two elements Place smaller element in combined array

 Pearson Education, Inc. All rights reserved. 45 If left array is emptyFill with elements of right arrayIf right array is emptyFill with elements of left array Copy values back to original array

 Pearson Education, Inc. All rights reserved. 46

 Pearson Education, Inc. All rights reserved. 47

 Pearson Education, Inc. All rights reserved. 48

 Pearson Education, Inc. All rights reserved. 49

 Pearson Education, Inc. All rights reserved. 50

 Pearson Education, Inc. All rights reserved. 51 Efficiency of Merge Sort Merge sort – Far more efficient that selection sort or insertion sort – Last merge requires n – 1 comparisons to merge entire array – Each lower level has twice as many calls to method merge, with each call operating on an array half the size which results in O(n) total comparisons – There will be O(log n) levels – Results in O(n log n)

 Pearson Education, Inc. All rights reserved. 52 Fig | Searching and sorting algorithms with Big O values.

 Pearson Education, Inc. All rights reserved. 53 Fig | Number of comparisons for common Big O notations.

 Pearson Education, Inc. All rights reserved Invariants Invariant – Is an assertion that is true before and after a portion of your code executes – Most common type is a loop invariant

 Pearson Education, Inc. All rights reserved Invariants Loop invariant remains true: – Before the execution of the loop – After every iteration of the loop body – When the loop terminates

 Pearson Education, Inc. All rights reserved Invariants Four steps to developing a loop from a loop invariant – Set initial values for any loop control variables – Determine the condition that causes the loop to terminate – Modify the control variable so the loop progresses toward termination – Check that the invariant remains true at the end of each iteration