Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

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,
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Chapter 9: Searching, Sorting, and Algorithm Analysis
Searching and Sorting Algorithms Based on D. S
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
CS4413 Divide-and-Conquer
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Chapter 19: Searching and Sorting Algorithms
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.
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.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
CHAPTER 11 Sorting.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Algorithm Efficiency and Sorting
The Complexity of Algorithms and the Lower Bounds of Problems
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Sorting CS 105 See Chapter 14 of Horstmann text. Sorting Slide 2 The Sorting problem Input: a collection S of n elements that can be ordered Output: the.
Data Structure Introduction.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
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.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
3 – SIMPLE SORTING ALGORITHMS
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
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.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 18: Searching and Sorting Algorithms
Data Structures Using C++
Data Structures Using C++ 2E
Chapter 4.
Presentation transcript:

Chapter 18: Searching and Sorting Algorithms

Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary search algorithms Compare sequential and binary search algorithm performance Become aware of the lower bound on comparison- based search algorithms 2C++ Programming: Program Design Including Data Structures, Sixth Edition

Objectives (cont’d.) Learn the various sorting algorithms Implement bubble, selection, insertion, quick, and merge sorting algorithms Compare sorting algorithm performance 3 C++ Programming: Program Design Including Data Structures, Sixth Edition

Introduction Using a search algorithm, you can: – Determine whether a particular item is in a list – If the data is specially organized (for example, sorted), find the location in the list where a new item can be inserted – Find the location of an item to be deleted 4C++ Programming: Program Design Including Data Structures, Sixth Edition

Searching and Sorting Algorithms Data can be organized with the help of an array or a linked list – unorderedLinkedList – unorderedArrayListType 5C++ Programming: Program Design Including Data Structures, Sixth Edition

Search Algorithms Key of the item – Special member that uniquely identifies the item in the data set Key comparison: comparing the key of the search item with the key of an item in the list – Can count the number of key comparisons 6C++ Programming: Program Design Including Data Structures, Sixth Edition

Sequential Search Sequential search (linear search): – Same for both array-based and linked lists – Starts at first element and examines each element until a match is found Our implementation uses an iterative approach – Can also be implemented with recursion 7C++ Programming: Program Design Including Data Structures, Sixth Edition

Sequential Search Analysis Statements before and after the loop are executed only once – Require very little computer time Statements in the for loop repeated several times – Execution of the other statements in loop is directly related to outcome of key comparison Speed of a computer does not affect the number of key comparisons required 8C++ Programming: Program Design Including Data Structures, Sixth Edition

Sequential Search Analysis (cont’d.) L: a list of length n If search item (target) is not in the list: n comparisons If the search item is in the list: – As first element of L  1 comparison (best case) – As last element of L  n comparisons (worst case) – Average number of comparisons: 9C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Binary search can be applied to sorted lists Uses the “divide and conquer” technique – Compare search item to middle element – If search item is less than middle element, restrict the search to the lower half of the list Otherwise restrict the search to the upper half of the list 10C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search (cont’d.) 11C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search (cont’d.) Search for value of 75: 12C++ Programming: Program Design Including Data Structures, Sixth Edition

Performance of Binary Search Every iteration cuts size of the search list in half If list L has 1024 = 2 10 items – At most 11 iterations needed to find x Every iteration makes two key comparisons – In this case, at most 22 key comparisons – Max # of comparisons = 2log 2 n+2 Sequential search required 512 key comparisons (average) to find if x is in L 13C++ Programming: Program Design Including Data Structures, Sixth Edition

Binary Search Algorithm and the class orderedArrayListType 14 To use binary search algorithm in class orderedArrayListType : – Add binSearch function C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation After an algorithm is designed, it should be analyzed May be various ways to design a particular algorithm – Certain algorithms take very little computer time to execute – Others take a considerable amount of time 15C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) 16C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) 17C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) 18C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) Let f be a function of n Asymptotic: the study of the function f as n becomes larger and larger without bound Let f and g be real-valued, non-negative functions f(n) is Big-O of g(n), written f(n)=O(g(n)) if there are constants c and n 0 such that f(n)≤cg(n) for all n ≥n 0 19C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) 20C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) 21C++ Programming: Program Design Including Data Structures, Sixth Edition

Asymptotic Notation: Big-O Notation (cont’d.) We can use Big-O notation to compare sequential and binary search algorithms: 22C++ Programming: Program Design Including Data Structures, Sixth Edition

Lower Bound on Comparison- Based Search Algorithms Comparison-based search algorithms: – Search a list by comparing the target element with list elements 23C++ Programming: Program Design Including Data Structures, Sixth Edition

Sorting Algorithms To compare the performance of commonly used sorting algorithms – Must provide some analysis of these algorithms These sorting algorithms can be applied to either array-based lists or linked lists 24C++ Programming: Program Design Including Data Structures, Sixth Edition

Sorting a List: Bubble Sort Suppose list[0]...list[n–1] is a list of n elements, indexed 0 to n–1 Bubble sort algorithm: – In a series of n-1 iterations, compare successive elements, list[index] and list[index+1] – If list[index] is greater than list[index+1], then swap them 25C++ Programming: Program Design Including Data Structures, Sixth Edition

Sorting a List: Bubble Sort (cont’d.) 26C++ Programming: Program Design Including Data Structures, Sixth Edition

Sorting a List: Bubble Sort (cont’d.) 27C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Bubble Sort bubbleSort contains nested loops – Outer loop executes n – 1 times – For each iteration of outer loop, inner loop executes a certain number of times Total number of comparisons: Number of assignments (worst case): 28C++ Programming: Program Design Including Data Structures, Sixth Edition

Bubble Sort Algorithm and the class unorderedArrayListType class unorderedArrayListType does not have a sorting algorithm – Must add function sort and call function bubbleSort instead 29C++ Programming: Program Design Including Data Structures, Sixth Edition

Selection Sort: Array-Based Lists Selection sort algorithm: rearrange list by selecting an element and moving it to its proper position Find the smallest (or largest) element and move it to the beginning (end) of the list Can also be applied to linked lists 30C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Selection Sort function swap : does three assignments; executed n−1 times – 3(n − 1) = O(n) function minLocation : – For a list of length k, k−1 key comparisons – Executed n−1 times (by selectionSort ) – Number of key comparisons: 31C++ Programming: Program Design Including Data Structures, Sixth Edition

Insertion Sort: Array-Based Lists Insertion sort algorithm: sorts the list by moving each element to its proper place in the sorted portion of the list 32C++ Programming: Program Design Including Data Structures, Sixth Edition

Insertion Sort: Array-Based Lists (cont’d.) 33C++ Programming: Program Design Including Data Structures, Sixth Edition

Insertion Sort: Array-Based Lists (cont’d.) 34C++ Programming: Program Design Including Data Structures, Sixth Edition

Insertion Sort: Array-Based Lists (cont’d.) 35C++ Programming: Program Design Including Data Structures, Sixth Edition

Insertion Sort: Array-Based Lists (cont’d.) 36C++ Programming: Program Design Including Data Structures, Sixth Edition

Insertion Sort: Array-Based Lists (cont’d.) 37C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Insertion Sort The for loop executes n – 1 times Best case (list is already sorted): – Key comparisons: n – 1 = O(n) Worst case: for each for iteration, if statement evaluates to true – Key comparisons: … + (n – 1) = n(n – 1) / 2 = O(n 2 ) Average number of key comparisons and of item assignments: ¼ n 2 + O(n) = O(n 2 ) 38C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Insertion Sort (cont’d.) 39C++ Programming: Program Design Including Data Structures, Sixth Edition

Lower Bound on Comparison- Based Sort Algorithms Comparison tree: graph used to trace the execution of a comparison-based algorithm – Let L be a list of n distinct elements; n > 0 For any j and k, where 1  j  n, 1  k  n, either L[j] L[k] Binary tree: each comparison has two outcomes 40C++ Programming: Program Design Including Data Structures, Sixth Edition

Lower Bound on Comparison- Based Sort Algorithms (cont’d.) Node: represents a comparison – Labeled as j:k (comparison of L[j] with L[k]) – If L[j] < L[k], follow the left branch; otherwise, follow the right branch Leaf: represents final ordering of the nodes Root: the top node Branch: line that connects two nodes Path: sequence of branches from one node to another 41C++ Programming: Program Design Including Data Structures, Sixth Edition

Lower Bound on Comparison- Based Sort Algorithms (cont’d.) 42C++ Programming: Program Design Including Data Structures, Sixth Edition

Lower Bound on Comparison- Based Sort Algorithms (cont’d.) A unique permutation of the elements of L is associated with each root-to-leaf path – Because the sort algorithm only moves the data and makes comparisons For a list of n elements, n > 0, there are n! different permutations – Any of these might be the correct ordering of L Thus, the tree must have at least n! leaves 43C++ Programming: Program Design Including Data Structures, Sixth Edition

Lower Bound on Comparison- Based Sort Algorithms (cont’d.) Theorem: Let L be a list of n distinct elements. Any sorting algorithm that sorts L by comparison of the keys only, in its worst case, makes at least O(nlog 2 n) key comparisons. 44C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists Quick sort: uses the divide-and-conquer technique – The list is partitioned into two sublists – Each sublist is then sorted – Sorted sublists are combined into one list in such a way that the combined list is sorted – All of the sorting work occurs during the partitioning of the list 45C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) pivot element is chosen to divide the list into: lowerSublist and upperSublist – The elements in lowerSublist are < pivot – The elements in upperSublist are ≥ pivot Pivot can be chosen in several ways – Ideally, the pivot divides the list into two sublists of nearly- equal size 46C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) 47C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) Partition algorithm (assumes that pivot is chosen as the middle element of the list): 1.Determine pivot ; swap it with the first element of the list 2.For the remaining elements in the list: If the current element is less than pivot, (1) increment smallIndex, and (2) swap current element with element pointed by smallIndex – Swap the first element ( pivot ), with the array element pointed to by smallIndex 48C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) Step 1 determines the pivot and moves pivot to the first array position During Step 2, list elements are arranged 49C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) 50C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) 51C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) 52C++ Programming: Program Design Including Data Structures, Sixth Edition

Quick Sort: Array-Based Lists (cont’d.) 53C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Quick Sort 54C++ Programming: Program Design Including Data Structures, Sixth Edition

Merge Sort: Linked List-Based Lists Quick sort: O(nlog 2 n) average case; O(n 2 ) worst case Merge sort: always O(nlog 2 n) – Uses the divide-and-conquer technique Partitions the list into two sublists Sorts the sublists Combines the sublists into one sorted list – Differs from quick sort in how list is partitioned Divides list into two sublists of nearly equal size 55C++ Programming: Program Design Including Data Structures, Sixth Edition

Merge Sort: Linked List-Based Lists (cont’d.) 56C++ Programming: Program Design Including Data Structures, Sixth Edition

Merge Sort: Linked List-Based Lists (cont’d.) General algorithm: Uses recursion 57C++ Programming: Program Design Including Data Structures, Sixth Edition

Divide 58C++ Programming: Program Design Including Data Structures, Sixth Edition

Divide (cont’d.) 59C++ Programming: Program Design Including Data Structures, Sixth Edition

Merge Sorted sublists are merged into a sorted list – Compare elements of sublists – Adjust pointers of nodes with smaller info 60C++ Programming: Program Design Including Data Structures, Sixth Edition

Merge (cont’d.) 61C++ Programming: Program Design Including Data Structures, Sixth Edition

Merge (cont’d.) 62C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Merge Sort Suppose that L is a list of n elements, with n > 0 Suppose that n is a power of 2; that is, n = 2 m for some integer m > 0, so that we can divide the list into two sublists, each of size: – m will be the number of recursion levels 63C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Merge Sort (cont’d.) 64C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Merge Sort (cont’d.) To merge two sorted lists of size s and t, the maximum number of comparisons is s + t  1 Function mergeList merges two sorted lists into a sorted list – This is where the actual comparisons and assignments are done Max. # of comparisons at level k of recursion: 65C++ Programming: Program Design Including Data Structures, Sixth Edition

Analysis: Merge Sort (cont’d.) The maximum number of comparisons at each level of the recursion is O(n) – Maximum number of comparisons is O(nm), where m = number of levels of recursion – Thus, O(nm)  O(n log 2 n) W(n): # of key comparisons in worst case A(n): # of key comparisons in average case 66C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary On average, a sequential search searches half the list and makes O(n) comparisons – Not efficient for large lists A binary search requires the list to be sorted – 2log 2 n – 3 key comparisons Let f be a function of n: by asymptotic, we mean the study of the function f as n becomes larger and larger without bound 67C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary (cont’d.) Binary search algorithm is the optimal worst-case algorithm for solving search problems by using the comparison method – To construct a search algorithm of the order less than log 2 n, it cannot be comparison based Bubble sort: O(n 2 ) key comparisons and item assignments Selection sort: O(n 2 ) key comparisons and O(n) item assignments 68C++ Programming: Program Design Including Data Structures, Sixth Edition

Summary (cont’d.) Insertion sort: O(n 2 ) key comparisons and item assignments Both the quick sort and merge sort algorithms sort a list by partitioning it – Quick sort: average number of key comparisons is O(nlog 2 n); worst case number of key comparisons is O(n 2 ) – Merge sort: number of key comparisons is O(nlog 2 n) 69C++ Programming: Program Design Including Data Structures, Sixth Edition