C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.

Slides:



Advertisements
Similar presentations
Chapter 10: Applications of Arrays and the class vector
Advertisements

C++ Programming:. From Problem Analysis
Introduction to Algorithms Quicksort
MATH 224 – Discrete Mathematics
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Data Structures Using C++ 2E
Data Structures Using C++ 2E
CPS120: Introduction to Computer Science Searching and Sorting.
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
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
Algorithm Efficiency and Sorting
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Chapter 16: Searching, Sorting, and the vector Type.
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.
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.
Applications of Arrays (Searching and Sorting) and Strings
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.
Chapter 14: Searching and Sorting
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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.
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.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
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.
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.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 18: Searching and Sorting Algorithms
Data Structures Using C++
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms

C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will: Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential and binary search algorithms perform Become aware of the lower bound on comparison-based search algorithms

C++ Programming: Program Design Including Data Structures, Fourth Edition3 Objectives (continued) Learn the various sorting algorithms Explore how to implement the bubble, selection, insertion, quick, and merge sorting algorithms Discover how the sorting algorithms discussed in this chapter perform

C++ Programming: Program Design Including Data Structures, Fourth Edition4 Searching and Sorting Algorithms The most important operation that can be performed on a list is the search algorithm Using a search algorithm, you can: −Determine whether a particular item is in the 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

C++ Programming: Program Design Including Data Structures, Fourth Edition5 Searching and Sorting Algorithms (continued) Because searching and sorting require comparisons of data, the algorithms should work on the type of data that provide appropriate functions to compare data items Data can be organized with the help of an array or a linked list − unorderedLinkedList − unorderedArrayListType

C++ Programming: Program Design Including Data Structures, Fourth Edition6 Search Algorithms Associated with each item in a data set is a special member that uniquely identifies the item in the data set −Called the key of the item Key comparison: comparing the key of the search item with the key of an item in the list −Can be counted: number of key comparisons

C++ Programming: Program Design Including Data Structures, Fourth Edition7 Sequential Search

C++ Programming: Program Design Including Data Structures, Fourth Edition8 Sequential Search Analysis The statements before and after the loop are executed only once, and hence require very little computer time The statements in the for loop are the ones that are 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

C++ Programming: Program Design Including Data Structures, Fourth Edition9 Sequential Search Analysis (continued) L: a list of length n If search item is not in the list: n comparisons If the search item is in the list: −If search item is the first element of L  one key comparison (best case) −If search item is the last element of L  n comparisons (worst case) −Average number of comparisons:

C++ Programming: Program Design Including Data Structures, Fourth Edition10 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 search the upper half of the list

C++ Programming: Program Design Including Data Structures, Fourth Edition13 Performance of Binary Search Every iteration cuts size of search list in half If list L has 1000 items −At most 11 iterations needed to find x Every iteration makes two key comparisons −In this case, at most 22 key comparisons Sequential search would make 500 key comparisons (average) if x is in L

C++ Programming: Program Design Including Data Structures, Fourth Edition14 Binary Search Algorithm and the class orderedArrayListType

C++ Programming: Program Design Including Data Structures, Fourth Edition15 Asymptotic Notation: Big-O Notation After an algorithm is designed it should be analyzed There are various ways to design a particular algorithm −Certain algorithms take very little computer time to execute; others take a considerable amount of time

Lines 1 to 6 each have one operation, > Line 7 has one operation, >= Either Line 8 or Line 9 executes; each has one operation There are three operations, <<, in Line 11 The total number of operations executed in this code is = 11

C++ Programming: Program Design Including Data Structures, Fourth Edition18 Asymptotic Notation: Big-O Notation (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition20 Asymptotic Notation: Big-O Notation (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition21 Asymptotic Notation: Big-O Notation (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition24 Asymptotic Notation: Big-O Notation (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition25 Asymptotic Notation: Big-O Notation (continued) We can use Big-O notation to compare the sequential and binary search algorithms:

C++ Programming: Program Design Including Data Structures, Fourth Edition26 Lower Bound on Comparison- Based Search Algorithms Comparison-based search algorithm: search the list by comparing the target element with the list elements

C++ Programming: Program Design Including Data Structures, Fourth Edition27 Sorting Algorithms There are several sorting algorithms in the literature We discuss some of the commonly used sorting algorithms To compare their performance, we provide some analysis of these algorithms These sorting algorithms can be applied to either array-based lists or linked lists

C++ Programming: Program Design Including Data Structures, Fourth Edition28 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

C++ Programming: Program Design Including Data Structures, Fourth Edition31 Sorting a List: Bubble Sort (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition32 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 Comparisons: Assignments (worst case):

C++ Programming: Program Design Including Data Structures, Fourth Edition33 Bubble Sort Algorithm and the class unorderedArrayListType Calls bubbleSort

C++ Programming: Program Design Including Data Structures, Fourth Edition34 Selection Sort: Array-Based Lists Selection sort: 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

C++ Programming: Program Design Including Data Structures, Fourth Edition35 Selection Sort (continued) On successive passes, locate the smallest item in the list starting from the next element

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

C++ Programming: Program Design Including Data Structures, Fourth Edition39 Insertion Sort: Array-Based Lists The insertion sort algorithm sorts the list by moving each element to its proper place

C++ Programming: Program Design Including Data Structures, Fourth Edition42 Insertion Sort (continued) Pseudocode algorithm:

C++ Programming: Program Design Including Data Structures, Fourth Edition44 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 )

C++ Programming: Program Design Including Data Structures, Fourth Edition46 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] −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 the final ordering of the nodes

C++ Programming: Program Design Including Data Structures, Fourth Edition47 Lower Bound on Comparison- Based Sort Algorithms (continued) root branch path

C++ Programming: Program Design Including Data Structures, Fourth Edition48 Lower Bound on Comparison- Based Sort Algorithms (continued) Associated with each root-to-leaf path is a unique permutation of the elements of L −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

C++ Programming: Program Design Including Data Structures, Fourth Edition49 Quick Sort: Array-Based Lists 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 so that the combined list is sorted

C++ Programming: Program Design Including Data Structures, Fourth Edition50 Quick Sort: Array-Based Lists (continued) To partition the list into two sublists, first we choose an element of the list called pivot The pivot divides the list into: lowerSublist and upperSublist −The elements in lowerSublist are < pivot −The elements in upperSublist are ≥ pivot

C++ Programming: Program Design Including Data Structures, Fourth Edition51 Quick Sort: Array-Based Lists (continued) Partition algorithm (we assume that pivot is chosen as the middle element of the list): −Determine pivot ; swap it with the first element of the list −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

C++ Programming: Program Design Including Data Structures, Fourth Edition52 Quick Sort: Array-Based Lists (continued) Step 1 determines the pivot and moves pivot to the first array position During the execution of Step 2, the list elements get arranged

C++ Programming: Program Design Including Data Structures, Fourth Edition55 Quick Sort: Array-Based Lists (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition58 Analysis: Quick Sort

C++ Programming: Program Design Including Data Structures, Fourth Edition59 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

C++ Programming: Program Design Including Data Structures, Fourth Edition61 Merge Sort: Linked List-Based Lists (continued) General algorithm: We next describe the necessary algorithm to: −Divide the list into sublists of nearly equal size −Merge sort both sublists −Merge the sorted sublists

C++ Programming: Program Design Including Data Structures, Fourth Edition62 Divide

C++ Programming: Program Design Including Data Structures, Fourth Edition63 Divide (continued) Every time we advance middle by one node, we advance current by one node After advancing current by one node, if it is not NULL, we again advance it by one node −Eventually, current becomes NULL and middle points to the last node of first sublist

C++ Programming: Program Design Including Data Structures, Fourth Edition65 Merge Sorted sublists are merged into a sorted list by comparing the elements of the sublists and then adjusting the pointers of the nodes with the smaller info

C++ Programming: Program Design Including Data Structures, Fourth Edition70 Analysis: Merge Sort Suppose that L is a list of n elements, where n > 0 Suppose that n is a power of 2; that is, n = 2 m for some nonnegative integer m, so that we can divide the list into two sublists, each of size: −m is the number of recursion levels

C++ Programming: Program Design Including Data Structures, Fourth Edition71 Analysis: Merge Sort (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition72 Analysis: Merge Sort (continued) To merge a sorted list of size s with a sorted list of size t, the maximum number of comparisons is s + t  1 The function mergeList merges two sorted lists into a sorted list −This is where the actual work (comparisons and assignments) is done −Max. # of comparisons at level k of recursion:

C++ Programming: Program Design Including Data Structures, Fourth Edition73 The maximum number of comparisons at each level of the recursion is O(n) −The maximum number of comparisons is O(nm), where m is the number of levels of the recursion; since n = 2 m  m = log 2 n −Thus, O(nm)  O(n log 2 n) W(n): # of key comparisons in the worst case A(n): # of key comparisons in average case Analysis: Merge Sort (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition74 Programming Example: Election Results The presidential election for the student council of your university is about to be held You have to write a program to analyze the data and report the winner The university has four major divisions (labeled region 1 – 4), and each division has several departments Each department in each division handles its own voting and reports the votes received by each candidate to the election committee

C++ Programming: Program Design Including Data Structures, Fourth Edition75 Programming Example: Election Results (continued) The voting is reported in the following form: firstName lastName regionNumber numberOfVotes

C++ Programming: Program Design Including Data Structures, Fourth Edition76 Programming Example: Election Results (continued) The input file containing the voting data looks like the following: The main program component is a candidate − class candidateType

C++ Programming: Program Design Including Data Structures, Fourth Edition77 personType

C++ Programming: Program Design Including Data Structures, Fourth Edition79 Candidate

C++ Programming: Program Design Including Data Structures, Fourth Edition81 Candidate (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition82 Main Program Read each candidate’s name into candidateList Sort candidateList Process the voting data Calculate the total votes received by each candidate Print the results

C++ Programming: Program Design Including Data Structures, Fourth Edition83 Main Program (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition84 Main Program (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition85 fillNames

C++ Programming: Program Design Including Data Structures, Fourth Edition86 fillNames (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition87 Sort Names

C++ Programming: Program Design Including Data Structures, Fourth Edition88 Process Voting Data

C++ Programming: Program Design Including Data Structures, Fourth Edition89 Process Voting Data (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition90 Process Voting Data (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition91 Add Votes

C++ Programming: Program Design Including Data Structures, Fourth Edition92 Add Votes (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition93 Print Heading and Print Results

C++ Programming: Program Design Including Data Structures, Fourth Edition94 Print Heading and Print Results (continued)

C++ Programming: Program Design Including Data Structures, Fourth Edition95 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

C++ Programming: Program Design Including Data Structures, Fourth Edition96 Summary (continued) 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 can’t 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

C++ Programming: Program Design Including Data Structures, Fourth Edition97 Summary (continued) 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)