19 Searching and Sorting.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
 2006 Pearson Education, Inc. All rights reserved Searching 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
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
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.
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.
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.
 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.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
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
Algorithm Analysis 1.
Sort Algorithm.
16 Searching and Sorting.
Growth of Functions & Algorithms
Searching – Linear and Binary Searches
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to complexity
Introduction to Algorithms
Chapter 20 Searching and Sorting
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Efficiency (Chapter 2).
Big-Oh and Execution Time: A Review
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.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
ITEC 2620M Introduction to Data Structures
Chapter 8 Search and Sort
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
CS 201 Fundamental Structures of Computer Science
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.
Divide and Conquer Algorithms Part I
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
Algorithm Efficiency and Sorting
Presentation transcript:

19 Searching and Sorting

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

OBJECTIVES In this chapter you’ll learn: To search for a given value in a vector using binary search. To use Big O notation to express the efficiency of an algorithm and to compare the performance of algorithms. To review the efficiency of the selection sort and insertion sort algorithms. To sort a vector using the recursive merge sort algorithm. To determine the efficiency of various searching and sorting algorithms. To enumerate the searching and sorting algorithms discussed in this text. To understand the nature of algorithms of constant, linear and quadratic runtime.

19.1 Introduction 19.2 Searching Algorithms 19.2.1 Efficiency of Linear Search 19.2.2 Binary Search 19.3 Sorting Algorithms 19.3.1 Efficiency of Selection Sort 19.3.2 Efficiency of Insertion Sort 19.3.3 Merge Sort (A Recursive Implementation) 19.4 Wrap-Up

19.1 Introduction Searching data Determine whether a value (the search key) is present in the data Algorithms Linear search Simple Slow for large sets of data Binary search Fast, even for large sets of data More complex than linear search

19.1 Introduction (Cont.) Sorting data Place data in order Algorithms Typically ascending or descending Based on one or more sort keys Algorithms Insertion sort Selection sort Merge sort More efficient, but more complex

19.1 Introduction (Cont.) Big O notation Estimates worst-case runtime for an algorithm How hard an algorithm must work to solve a problem

Fig. 19. 1 | Searching and sorting algorithms in this text Fig. 19.1 | Searching and sorting algorithms in this text. (Part 1 of 2)

Fig. 19. 1 | Searching and sorting algorithms in this text Fig. 19.1 | Searching and sorting algorithms in this text. (Part 2 of 2)

19.2 Searching Algorithms Searching algorithms Find element that matches a given search key 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

19.2.1 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

19.2.1 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-element vector is equal to the second element Always takes one comparison, no matter how large the vector

19.2.1 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

19.2.1 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(n2) Pronounced “on the order of n2” or “order n2” Example Test if any element of an n-vector is equal to any other element Takes n2/2 – n/2 comparisons n2 term dominates, constant 1/2 is ignored, -n/2 is ignored

19.2.1 Efficiency of Linear Search (Cont.) 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

Performance Tip 19.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.

19.2.2 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

19.2.2 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

Outline BinarySearch.h (1 of 1)

Outline (1 of 3) Initialize the vector with random ints from 10-99 BinarySearch.cpp (1 of 3) Initialize the vector with random ints from 10-99 Sort the elements in vector data in ascending order

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

Outline BinarySearch.cpp (3 of 3)

Outline Fig20_04.cpp (1 of 3) Perform a binary search on the data

Outline Fig19_04.cpp (2 of 3)

Outline Fig19_04.cpp (3 of 3)

19.2.2 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 ( 1023 + 1 ) ) Repeatedly dividing 1023 by 2 and rounding down results in 0 after 10 iterations

19.3 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

19.3.1 Efficiency of 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 (n2 – n)/2 comparisons Iterates n - 1 times In ith iteration, locating ith smallest element requires n – i comparisons Has Big O of O(n2)

19.3.2 Efficiency of 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 n2 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(n2)

19.3.3 Merge Sort (A Recursive Implementation) 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

19.3.3 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

19.3.3 Merge Sort (A Recursive Implementation) (Cont.) Merge sort (Cont.) Sample merging step Smaller, sorted vectors A: 4 10 34 56 77 B: 5 30 51 52 93 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.

Outline MergeSort.h (1 of 1)

Outline MergeSort.cpp (1 of 5)

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

Outline Combine the two sorted vectors into one larger, sorted vector MergeSort.cpp (3 of 5) 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

Outline Fill the combined vector with the remaining elements of the right vector or… MergeSort.cpp (4 of 5) …else fill the combined vector with the remaining elements of the left vector Copy the combined vector into the original vector

Outline MergeSort.cpp (5 of 5)

Outline Fig19_07.cpp (1 of 3)

Outline Fig19_07.cpp (2 of 3)

Outline Fig19_07.cpp (3 of 3)

19.3.3 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”

Fig. 19.8 | Searching and sorting algorithms with Big O values.

Fig. 19.9 | Approximate number of comparisons for common Big O notations.