Chapter 19: Searching and Sorting Algorithms

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

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
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Data Structures Using C++ 2E
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
CPS120: Introduction to Computer Science Searching and Sorting.
Searching and Sorting Algorithms Based on D. S
Chapter 19: Searching and Sorting Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
Data Structures Using Java1 Chapter 8 Search Algorithms.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Algorithm Efficiency and Sorting
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Programming Logic and Design Fourth Edition, Comprehensive
Elementary Data Structures and Algorithms
Chapter 3: The Efficiency of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Analysis of Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
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.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
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 9: Sorting and Searching Arrays
Chapter 18: Searching and Sorting Algorithms
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Algorithm Efficiency and Sorting
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

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

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 Edition

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 Edition

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 Edition

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

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 Edition

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

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 Edition

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

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

Lines 1 to 6 each have one operation, << or >> 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 6 + 1 + 1 + 3 = 11

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

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

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

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

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 Edition

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 Edition

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

Analysis: Bubble Sort bubbleSort contains nested loops Comparisons: 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 Edition

Selection Sort 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 Edition

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 Edition

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 Edition

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

Insertion Sort (continued) Pseudocode algorithm: C++ Programming: Program Design Including Data Structures, Fourth 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:1 + 2 + … + (n – 1) = n(n – 1) / 2 = O(n2) Average number of key comparisons and of item assignments: ¼ n2 + O(n) = O(n2) C++ Programming: Program Design Including Data Structures, Fourth 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 2log2n – 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 Edition

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 log2n, it can’t be comparison based Bubble sort: O(n2) key comparisons and item assignments Selection sort: O(n2) key comparisons and O(n) item assignments C++ Programming: Program Design Including Data Structures, Fourth Edition

Summary (continued) Insertion sort: O(n2) key comparisons and item assignments C++ Programming: Program Design Including Data Structures, Fourth Edition