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.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Introduction to Algorithms
CS4413 Divide-and-Conquer
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Chapter 7: Sorting Algorithms
Computer Science 112 Fundamentals of Programming II Finding Faster Algorithms.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
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.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Chapter 19: Searching and Sorting Algorithms
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
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.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
Quicksort
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-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.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
(c) , University of Washington
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 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
CSC 211 Data Structures Lecture 13
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
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 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.
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Computer Science 101 A Survey of Computer Science QuickSort.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
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.
Searching and Sorting Algorithms
Fundamentals of Programming II Finding Faster Algorithms
Algorithm Efficiency and Sorting
Computer Science 101 A Survey of Computer Science
Quicksort
Algorithm Design Methods
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 4: Divide and Conquer
Unit-2 Divide and Conquer
Chapter 4.
Algorithm Efficiency and Sorting
CS 1114: Sorting and selection (part two)
Algorithm Efficiency and Sorting
Design and Analysis of Algorithms
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

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 would like to improve the worst cases, too

Example: Sequential Search If the data items are in random order, then each one must be examined in the worst case Requires N comparisons in the worst case set Current to 1 Set Found to false while Current <= N and not Found do if A(Current) = Target then set Found to true else increment Current if Found then output Current else output 0

Searching a Sorted List When we search a phone book, we don’t begin with the first name and look at each successor We skip over large numbers of names until we find the target or give up

Binary Search Strategy –Have pointers marking left and right ends of the list still to be processed –Compute the position of the midpoint between the two pointers –If the target equals the value at midpoint, quit with the position found –Otherwise, if the target is less than that value, search just the positions to the left of midpoint –Otherwise, search the just the positions to the right of midpoint –Give up when the pointers cross target beginendmid

Binary Search Strategy –Have pointers marking left and right ends of the list still to be processed –Compute the position of the midpoint between the two pointers –If the target equals the value at midpoint, quit with the position found –Otherwise, if the target is less than that value, search just the positions to the left of midpoint –Otherwise, search the just the positions to the right of midpoint –Give up when the pointers cross target beginendmid

The Binary Search Space

The Binary Search Algorithm set Begin to 1 Set End to N Set Found to false while Begin <= End and not Found do compute the midpoint if Target = A(Mid) then set Found to true else if Target < A(Mid) then search to the left of the midpoint else search to the right of the midpoint if Found then output Mid else output 0

The Binary Search Algorithm set Begin to 1 Set End to N Set Found to false while Begin <= End and not Found do set Mid to (Begin + End) / 2 if Target = A(Mid) then set Found to true else if Target < A(Mid) then search to the left of the midpoint else search to the right of the midpoint if Found then output Mid else output 0

The Binary Search Algorithm set Begin to 1 Set End to N Set Found to false while Begin <= End and not Found do set Mid to (Begin + End) / 2 if Target = A(Mid) then set Found to true else if Target < A(Mid) then set End to Mid – 1 else search to the right of the midpoint if Found then output Mid else output 0

The Binary Search Algorithm set Begin to 1 Set End to N Set Found to false while Begin <= End and not Found do set Mid to (Begin + End) / 2 if Target = A(Mid) then set Found to true else if Target < A(Mid) then set End to Mid – 1 else set Begin to Mid + 1 if Found then output Mid else output 0

Analysis of Binary Search On each pass through the loop, ½ of the positions in the list are discarded In the worst case, the number of comparisons equals the number of times the size of the list can be divided by 2 How many comparisons for a list of size N, in the worst case?

Improving on Sorting Several algorithms have been developed to break the (N 2 - N) / 2 barrier for sorting Most of them use a divide-and-conquer strategy Break the list into smaller pieces and apply another algorithm to them

Quicksort Strategy - Divide and Conquer: –Partition list into two parts, with small elements in the first part and large elements in the second part –Sort the first part –Sort the second part Question - How do we sort the sections? Answer - Apply Quicksort to them Recursive algorithm - one which makes use of itself to solve smaller problems of the same type

Quicksort Question - Will this recursive process ever stop? Answer - Yes, when the problem is small enough, we no longer use recursion. Such cases are called base cases

Partitioning a List To partition a list, we choose a pivot element The elements that are less than or equal to the pivot go into the first section The elements larger than the pivot go into the second section

Partition Partitioning a List Pivot is the element at the midpoint Sublist to sort Data are where they should be relative to the pivot

The Quicksort Algorithm if the list to sort has more than 1 element then if the list has exactly two elements then if the elements are out of order then exchange them else perform the Partition Algorithm on the list apply QuickSort to the first section apply QuickSort to the second section

Partitioning: Choosing the Pivot Ideal would be to choose the median element as the pivot, but this would take too long Some versions just choose the first element Our choice - the median of the first three elements

Partition Partitioning a List Pivot is median of first three items The median of the first three elements is a better approximation to the actual median than the element at the midpoint and results in more even splits

The Partition Algorithm exchange the median of the first 3 elements with the first set P to first position of list set L to second position of list set U to last position of list while L <= U while A(L)  A(P) do set L to L + 1 while A(U) > A(P) do set U to U - 1 if L < U then exchange A(L) and A(U) exchange A(P) and A(U) A The list P The position of the pivot element L Probes for elements > pivot U Probes for elements <= pivot

Quicksort: Rough Analysis For simplification, assume that we always get even splits when we partition When we partition the entire list, each element is compared with the pivot - approximately n comparisons Each of the halves is partitioned, each taking about n/2 comparisons, thus about n more comparisons Each of the fourths is partitioned,each taking about n/4 comparisons - n more

Quicksort: Rough Analysis How many levels of about n comparisons do we get? Roughly, we keep splitting until the pieces are about size 1 How many times must we divide n by 2 before we get 1? log(n) times, of course Thus comparisons  n Log(n) in the ideal or best case

Call Tree For a Best Case We select the midpoint element as the pivot. The median element happens to be at the midpoint on each call. But the array was already sorted!

Worst Case What if the value at the midpoint is near the largest value on each call? Or near the smallest value on each call? Then there will be approximately n subdivisions, and the total number of comparisons will degenerate to n 2

Call Tree For a Worst Case We select the first element as the pivot. The smallest element happens to be the first one on each call. n subdivisions!

Other Methods of Selecting the Pivot Element Pick a random element Pick the median of the first three elements Pick the median of the first, middle, and last elements Pick the median element - not!! This is an O(n) algorithm

For Monday Continue Reading in Chapter 3