Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Introduction to Algorithms Quicksort
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
© 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.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Cmpt-225 Algorithm Efficiency.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Data Structures Review Session 1
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Chapter 11 Arrays Continued
 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.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CSC 211 Data Structures Lecture 13
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Data Structure Introduction.
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.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Chapter 7 Analysis of Algorithms © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
Chapter 7 Analysis of Algorithms © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Chapter 5 Ordered List.
Presentation transcript:

Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview ● Searching – 8.1 ● Linear search – 8.2 ● Binary search ● Sorting – 8.3 ● Presents the insertion sort algorithm.

Overview ● 8.4 – Introduce an interface that allows us to search and sort arrays of other things. ● 8.5 – Sorting linked lists

Linear Search ● Linear search – Start at the beginning and examine each element in turn. – It takes linear time in both the worst and average cases.

Linear Search ● Algorithm is more efficient if we know in advance that the array is sorted. – The number we encounter during a search increases as we move from left to right across the array. – If we ever encounter a number which is larger than the target, we can stop searching. ● The target can't possibly appear later in the array.

Linear Search

● Worst case – No faster than the old version. – Average still in Θ (n) reduced by a factor of 2. – Let event i be the event that the target, if it were present, would belong right before element i.

Linear Search ● Assume that the n + 1 events are equally likely. ● On average look at between n/2 and (n/2) + 1 elements.

Binary Search ● Starting our search in the middle with a sorted array. – A constant amount of work allows us to divide the data in half.

Binary Search

● The running time of binary search is proportional to the number of times the loop runs. – The number of times we have to divide the data in half before we run out of data. – In the worst case, we always have to look in the larger piece. – The number of passes through the loop is p + 1, where 2 p = n.

Binary Search ● If n = 8, we have one pass where there are 8 candidate elements, one where there are 4, one where there are 2, and one where there is 1. – This is four passes. – Notice that 2³ = 8. – If n were 2 4 = 16, we would need 5 passes. – 1 + log 2 n Θ (log n)

Binary Search

● We do not generally expect n to be a power of two, but for most running-time functions this shortcut will not change the order of our result. – Theorem: Let f(n) and g(n) be two monotonically nondecreasing functions. If f(n) = g(n) for exact powers of two, and cg(n) > g(2n) for some constant c, then f(n) Θ (g(n)) in general. – cg(n) > g(2n) indicates that this theorem does not work for very quickly growing functions like 2 n. – It does work for any function in a polynomial or lower order.

Binary Search

● cg(2) > g(4), cg(4) > g(8) ● cg(n) is always above the boundary boxes, and therefore greater than f(n) – f(n) O(cg(n)) = O(g(n)) – f(n) Ω (g(n)) – f(n) Θ (g(n)) Binary Search

● Binary search – Case where n is not a power of 2. – The number of passes is. – Since log 2 n is an integer for exact powers of 2 assuming that n is a power of 2 allows us to ignore floors. Binary Search

Insertion Sort ● There are many algorithms for getting an array into this sorted state. – Insertion sort: ● One of the simplest sorting algorithms.

Insertion Sort

● “in use” portion of the input array is always exactly the same size as the “not in use”. – We can use the same array for both purposes.

Insertion Sort ● Element 0 would always be inserted at position 0. It's already there. – It will move if any smaller numbers are later inserted.

Insertion Sort

● Running time of insertion sort depends on the running time of this inner loop. – Best case for inner loop:

Insertion Sort ● Worst case data was in reverse order.

Insertion Sort ● Average case

Insertion Sort ● Since the average-case time can't be worse- than the worst-case time, the average-case time must also be in O(n 2 ), and therefore in Θ (n 2 ).

The Comparable Interface ● We could use polymorphism to write methods to search and sort structures containing Integers, Cards, or anything else. – “greater than” and “less than” don't make sense for all classes. – It only makes sense to sort things which are comparable. – Java provides a built-in interface Comparable. – All of the wrapper classes, as well as String, implement Comparable.

The Comparable Interface

● Comparable is generic because we can't compare instances of different subclasses. – The type parameter specifies the class in question. – If we want a generic class to hold only of comparable classes ● > – “some class E that implements Comparable. – In this context, Java does not distinguish between implementing an interface and extending a class; the keyword extends covers both cases.

The Comparable Interface

● compareTo() – Given two objects a and b, a.compareTo(b) returns a negative number if a is less than b, zero if a equals b, and a positive number if a is greater than b.

The Comparable Interface

● compareTo() method in the String class uses lexicographical order. – All upper-case letters to be earlier than all lower- case ones.

The Comparable Interface

● If we want to make one of our own classes Comparable, we have to declare that it implements Comparable and provide the compareTo() method. – In some classes, this amounts to a simple subtraction.

The Comparable Interface

● The compareTo() method should be consistent with the equals() method. – a.equals(b) should be true for exactly those values of a and b for which a.compareTo(b) returns 0.

Sorting Linked Lists ● Binary search algorithm depends on random access. – Most sorting algorithms also depend on random access, some of them can be adapted to work with linked lists. – For our insertion sort to run efficiently, we must be careful to avoid the methods get(), set(), and size(), all take linear time on linked lists.

Sorting Linked Lists

Summary ● Two useful searching algorithms are linear search and binary search. ● The insertion sort algorithm inserts each element in turn into a sequence of already sorted elements.

Summary