Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 11-2 Chapter Objectives Examine the linear search and.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
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
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.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
© 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 11 Sorting.
Algorithm Efficiency and Sorting
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Chapter 18 Searching and Sorting
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
© 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.
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.
Chapter 19 Searching, Sorting and Big O
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 8: Sorting and Searching Java Software Structures: Designing.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 9 Searching and Sorting
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Searching and Sorting Chapter 18 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
© 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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Sorting: why?  We do it A LOT!  Makes finding the largest and smallest value easier  Makes finding how many of a certain value are in a list easier.
Chapter 19 Searching and Sorting
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Algorithms
Chapter 9 Polymorphism.
Algorithm Efficiency and Sorting
Chapter 20 Searching and Sorting
Chapter 13: Searching and Sorting
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Sorts.
Data Structures and Algorithms
24 Searching and Sorting.
Chapter 4.
Algorithm Efficiency and Sorting
Sorting and Searching -- Introduction
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

Chapter 11 Sorting and Searching

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and binary search algorithms Examine several sorting algorithms, including: – selection sort – insertion sort – bubble sort – quick sort – merge sort Discuss the complexity of these algorithms

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Searching Searching is the process of finding a target element among a group of items (the search pool), or determining that it isn't there This requires repetitively comparing the target to candidates in the search pool An efficient sort performs no more comparisons than it has to The size of the search pool is a factor

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The Comparable Interface We want to define the algorithms such that they can search any set of objects Therefore we will search objects that implement the Comparable interface It contains one method, compareTo, which is designed to return an integer that specifies the relationship between two objects: obj1.compareTo(obj2) This call returns a number less than, equal to, or greater than 0 if obj1 is less than, equal to, or greater than obj2, respectively

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The Comparable Interface All of the methods presented in this chapter are part of the SortingandSearching class This class makes use of the generic type T For this class, we make the further distinction that T extends Comparable public class SortingandSearching

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Linear Search A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted This approach does not assume the items in the search pool are in any particular order We just need to be able to examine each element in turn (in a linear fashion) It's fairly easy to understand, but not very efficient

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 11.1 A linear search

Copyright © 2005 Pearson Addison-Wesley. All rights reserved linearSearch

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Binary Search If the search pool is sorted, then we can be more efficient than a linear search A binary search eliminates large parts of the search pool with each comparison Instead of starting the search at one end, we begin in the middle If the target isn't found, we know that if it is in the pool at all, it is in one half or the other We can then jump to the middle of that half, and continue similarly

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 11.2 A binary search

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Binary Search For example, find the number 29 in the following sorted list of numbers: Compare the target to the middle value 54 We now know that if 29 is in the list, it is in the front half of the list With one comparison, we've eliminated half of the data Then compare to 22, eliminating another quarter of the data, etc.

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Binary Search A binary search algorithm is often implemented recursively Each recursive call searches a smaller portion of the search pool The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool At any point there may be two "middle" values, in which case either could be used

Copyright © 2005 Pearson Addison-Wesley. All rights reserved binarySearch

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Comparing Search Algorithms On average, a linear search would examine n/2 elements before finding the target Therefore, a linear search is O(n) The worst case for a binary search is (log 2 n) comparisons A binary search is a logarithmic algorithm It has a time complexity of O(log 2 n) But keep in mind that the search pool must be sorted For large n, a binary search is much faster

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Sorting Sorting is the process of arranging a group of items into a defined order based on particular criteria Many sorting algorithms have been designed Sequential sorts require approximately n 2 comparisons to sort n elements Logarithmic sorts typically require nlog 2 n comparisons to sort n elements

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Sorting Let's define a generic sorting problem that any of our sorting algorithms could help solve As with searching, we must be able to compare one element to another

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 11.1

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 11.1 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 11.2

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 11.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Selection Sort Selection sort orders a list of values by repetitively putting a particular value into its final position More specifically: – find the smallest value in the list – switch it with the value in the first position – find the next smallest value in the list – switch it with the value in the second position – repeat until all values are in their proper places

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 11.3 Illustration of selection sort processing

Copyright © 2005 Pearson Addison-Wesley. All rights reserved selectionSort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Insertion Sort Insertion sort orders a list of values by repetitively inserting a particular value into a sorted subset of the list More specifically: – consider the first item to be a sorted sublist of length 1 – insert the second item into the sorted sublist, shifting the first item if needed – insert the third item into the sorted sublist, shifting the other items as needed – repeat until all values have been inserted into their proper positions

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 11.4 Illustration of insertion sort processing

Copyright © 2005 Pearson Addison-Wesley. All rights reserved insertionSort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Bubble Sort Bubble sort orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary More specifically: – scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top – scan the list again, bubbling up the second highest value – repeat until all elements have been placed in their proper order

Copyright © 2005 Pearson Addison-Wesley. All rights reserved bubbleSort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Comparing Sorts We've seen three sorts so far: – selection sort – insertion sort – bubble sort They all use nested loops and perform approximately n 2 comparisons They are relatively inefficient Now we will turn our attention to more efficient sorts

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Quick Sort Quick sort orders a list of values by partitioning the list around one element, then sorting each partition More specifically: – choose one element in the list to be the partition element – organize the elements so that all elements less than the partition element are to the left and all greater are to the right – apply the quick sort algorithm (recursively) to both partitions

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Quick Sort The choice of the partition element is arbitrary For efficiency, it would be nice if the partition element divided the list roughly in half The algorithm will work in any case, however We will divide the work into two methods: – quickSort – performs the recursive algorithm – findPartition – rearranges the elements into two partitions

Copyright © 2005 Pearson Addison-Wesley. All rights reserved quickSort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved findPartition

Copyright © 2005 Pearson Addison-Wesley. All rights reserved findPartition

Copyright © 2005 Pearson Addison-Wesley. All rights reserved findPartition

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Merge Sort Merge sort orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining More specifically: – divide the list into two roughly equal parts – recursively divide each part in half, continuing until a part contains only one element – merge the two parts into one sorted list – continue to merge parts as the recursion unfolds

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 11.5 The decomposition of merge sort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 11.6 The merge portion of the merge sort algorithm

Copyright © 2005 Pearson Addison-Wesley. All rights reserved mergeSort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved mergeSort

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Efficiency of quickSort and mergeSort Both quickSort and mergeSort use a recursive structure that takes log 2 n processing steps to decompose the original list into lists of length one At each step, both algorithms either compare or merge all n elements Thus both algorithms are O(nlogn)