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

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
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.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution 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 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
 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.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
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.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
 2007 Pearson Education, Inc. All rights reserved Sorting: A Deeper Look.
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.
 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 Algorithms Searching and Sorting Algorithm Efficiency.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Chapter 19 Searching and Sorting
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
16 Searching and Sorting.
19 Searching and Sorting.
Searching – Linear and Binary Searches
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 20 Searching and Sorting
Teach A level Computing: Algorithms and Data Structures
Big-Oh and Execution Time: A Review
Algorithm design and Analysis
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 4.
Algorithm Efficiency and Sorting
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

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

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

 2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To search for a given value in a vector using binary search.  To sort a vector using the recursive merge sort algorithm.  To determine the efficiency of searching and sorting algorithms.

 2006 Pearson Education, Inc. All rights reserved Introduction 20.2 Searching Algorithms Efficiency of Linear Search Binary Search 20.3 Sorting Algorithms Efficiency of Selection Sort Efficiency of Insertion Sort Merge Sort (A Recursive Implementation) 20.4 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved Introduction Searching data – Determine whether a value (the search key) is present in the data If so, find its location – Algorithms Linear search – Simple Binary search – Faster but more complex

 2006 Pearson Education, Inc. All rights reserved Introduction (Cont.) Sorting data – Place data in order Typically ascending or descending Based on one or more sort keys – Algorithms Insertion sort Selection sort Merge sort – More efficient, but more complex

 2006 Pearson Education, Inc. All rights reserved Introduction (Cont.) Big O notation – Estimates worst-case runtime for an algorithm How hard an algorithm must work to solve a problem

 2006 Pearson Education, Inc. All rights reserved. 8 Fig | Searching and sorting algorithms in this text. (Part 1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 9 Fig | Searching and sorting algorithms in this text. (Part 2 of 2)

 2006 Pearson Education, Inc. All rights reserved Searching Algorithms Searching algorithms – Find element that matches a given search key If such an element does exist – Major difference between search algorithms Amount of effort they require to complete search – Particularly dependent on number of data elements – Can be described with Big O notation

 2006 Pearson Education, Inc. All rights reserved Efficiency of Linear Search Big O notation – Measures runtime growth of an algorithm relative to number of items processed Highlights dominant terms Ignores terms that become unimportant as n grows Ignores constant factors

 2006 Pearson Education, Inc. All rights reserved Efficiency of Linear Search (Cont.) Big O notation (Cont.) – Constant runtime Number of operations performed by algorithm is constant – Does not grow as number of items increases Represented in Big O notation as O(1) – Pronounced “on the order of 1” or “order 1” Example – Test if the first element of an n-vector is equal to the second element Always takes one comparison, no matter how large the vector

 2006 Pearson Education, Inc. All rights reserved Efficiency of Linear Search (Cont.) Big O notation (Cont.) – Linear runtime Number of operations performed by algorithm grows linearly with number of items Represented in Big O notation as O(n) – Pronounced “on the order of n” or “order n” Example – Test if the first element of an n-vector is equal to any other element Takes n - 1 comparisons n term dominates, -1 is ignored

 2006 Pearson Education, Inc. All rights reserved Efficiency of Linear Search (Cont.) Big O notation (Cont.) – Quadratic runtime Number of operations performed by algorithm grows as the square of the number of items Represented in Big O notation as O(n 2 ) – Pronounced “on the order of n 2 ” or “order n 2 ” Example – Test if any element of an n-vector is equal to any other element Takes n 2 /2 – n/2 comparisons n 2 term dominates, constant 1/2 is ignored, -n/2 is ignored

 2006 Pearson Education, Inc. All rights reserved Efficiency of Linear Search (Cont.) Efficiency of linear search – Linear search runs in O(n) time Worst case: every element must be checked If size of the vector doubles, number of comparisons also doubles

 2006 Pearson Education, Inc. All rights reserved. 16 Performance Tip 20.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.

 2006 Pearson Education, Inc. All rights reserved Binary Search Binary search algorithm – Requires that the vector first be sorted Can be performed by Standard Library function sort – Takes two random-access iterators – Sorts elements in ascending order – First iteration (assuming sorted ascending order) Test the middle element in the vector – If it matches search key, the algorithm ends – If it is greater than search key, continue with only the first half of the vector – If it is less than search key, continue with only the second half of the vector

 2006 Pearson Education, Inc. All rights reserved Binary Search (Cont.) Binary search algorithm (Cont.) – Subsequent iterations Test the middle element in the remaining subvector – If it matches search key, the algorithm ends – If not, eliminate half of the subvector and continue – Terminates when Element matching search key is found Current subvector is reduced to zero size – Can conclude that search key is not in vector

 2006 Pearson Education, Inc. All rights reserved. 19 Outline BinarySearch.h (1 of 1)

 2006 Pearson Education, Inc. All rights reserved. 20 Outline BinarySearch.cpp (1 of 3) Initialize the vector with random int s from Sort the elements in vector data in ascending order

 2006 Pearson Education, Inc. All rights reserved. 21 Outline BinarySearch.cpp (2 of 3) Calculate the low end index, high end index and middle index of the portion of the vector being searched Initialize the location of the found element to -1, indicating that the search key has not (yet) been found Loop until the subvector is of zero size or the search key is located Test if the middle element is equal to searchElement Eliminate half of the remaining values in the vector

 2006 Pearson Education, Inc. All rights reserved. 22 Outline BinarySearch.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 23 Outline Fig20_04.cpp (1 of 3) Perform a binary search on the data

 2006 Pearson Education, Inc. All rights reserved. 24 Outline Fig20_04.cpp (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 25 Outline Fig20_04.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved Binary Search (Cont.) Efficiency of binary search – Logarithmic runtime Number of operations performed by algorithm grows logarithmically as number of items increases Represented in Big O notation as O(log n) – Pronounced “on the order of log n” or “order log n” Example – Binary searching a sorted vector of 1023 elements takes at most 10 comparisons ( 10 = log 2 ( ) ) Repeatedly dividing 1023 by 2 and rounding down results in 0 after 10 iterations

 2006 Pearson Education, Inc. All rights reserved Sorting Algorithms Sorting algorithms – Placing data into some particular order Such as ascending or descending – One of the most important computing applications – End result, a sorted vector, will be the same no matter which algorithm is used Choice of algorithm affects only runtime and memory use

 2006 Pearson Education, Inc. All rights reserved Efficiency of Selection Sort Selection sort – At ith iteration Swaps the ith smallest element with element i – After ith iteration Smallest i elements are sorted in increasing order in first i positions – Requires a total of (n 2 – n)/2 comparisons Iterates n - 1 times – In ith iteration, locating ith smallest element requires n – i comparisons Has Big O of O(n 2 )

 2006 Pearson Education, Inc. All rights reserved Efficiency of Insertion Sort Insertion sort – At ith iteration Insert (i + 1)th element into correct position with respect to first i elements – After ith iteration First i elements are sorted – Requires a worst-case of n 2 inner-loop iterations Outer loop iterates n - 1 times – Inner loop requires n – 1iterations in worst case For determining Big O, nested statements mean multiply the number of iterations Has Big O of O(n 2 )

 2006 Pearson Education, Inc. All rights reserved Merge Sort (A Recursive Implementation) Merge sort – Sorts vector by Splitting it into two equal-size subvectors – If vector size is odd, one subvector will be one element larger than the other Sorting each subvector Merging them into one larger, sorted vector – Repeatedly compare smallest elements in the two subvectors – The smaller element is removed and placed into the larger, combined vector

 2006 Pearson Education, Inc. All rights reserved Merge Sort (A Recursive Implementation) (Cont.) Merge sort (Cont.) – Our recursive implementation Base case – A vector with one element is already sorted Recursion step – Split the vector (of ≥ 2 elements) into two equal halves If vector size is odd, one subvector will be one element larger than the other – Recursively sort each subvector – Merge them into one larger, sorted vector

 2006 Pearson Education, Inc. All rights reserved Merge Sort (A Recursive Implementation) (Cont.) Merge sort (Cont.) – Sample merging step Smaller, sorted vectors – A: – B: Compare smallest element in A to smallest element in B – 4 (A) is less than 5 (B) 4 becomes first element in merged vector – 5 (B) is less than 10 (A) 5 becomes second element in merged vector – 10 (A) is less than 30 (B) 10 becomes third element in merged vector – Etc.

 2006 Pearson Education, Inc. All rights reserved. 33 Outline MergeSort.h (1 of 1)

 2006 Pearson Education, Inc. All rights reserved. 34 Outline MergeSort.cpp (1 of 5)

 2006 Pearson Education, Inc. All rights reserved. 35 Outline MergeSort.cpp (2 of 5) Call function sortSubVector with 0 and size – 1 as the beginning and ending indices Test the base case Split the vector in two Recursively call function sortSubVector on the two subvectors

 2006 Pearson Education, Inc. All rights reserved. 36 Outline MergeSort.cpp (3 of 5) Combine the two sorted vectors into one larger, sorted vector Loop until the end of either subvector is reached Test which element at the beginning of the vectors is smaller Place the smaller element in the combined vector

 2006 Pearson Education, Inc. All rights reserved. 37 Outline MergeSort.cpp (4 of 5) Fill the combined vector with the remaining elements of the right vector or … … else fill the combined vector with the remaining elements of the left vector Copy the combined vector into the original vector

 2006 Pearson Education, Inc. All rights reserved. 38 Outline MergeSort.cpp (5 of 5)

 2006 Pearson Education, Inc. All rights reserved. 39 Outline Fig20_07.cpp (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 40 Outline Fig20_07.cpp (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 41 Outline Fig20_07.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved Merge Sort (A Recursive Implementation) (Cont.) Efficiency of merge sort – n log n runtime Halving vectors means log 2 n levels to reach base case – Doubling size of vector requires one more level – Quadrupling size of vector requires two more levels O(n) comparisons are required at each level – Calling sortSubVector with a size-n vector results in Two sortSubVector calls with size-n/2 subvectors A merge operation with n – 1 (order n) comparisons – So, always order n total comparisons at each level Represented in Big O notation as O(n log n) – Pronounced “on the order of n log n” or “order n log n”

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

 2006 Pearson Education, Inc. All rights reserved. 44 Fig | Approximate number of comparisons for common Big O notations.