Chapter 9: Searching, Sorting, and Algorithm Analysis

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Algorithm Efficiency and Sorting
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 19 Searching, Sorting and Big O
Analysis of Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Arrays
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Chapter 9: Searching, Sorting, and Algorithm Analysis
Introduction to Analysis of Algorithms
Introduction to complexity
Introduction to Analysis of Algorithms
Introduction to Algorithms
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Chapter 14: Recursion Starting Out with C++ Early Objects
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Introduction to Search Algorithms
Chapter 14: Recursion Starting Out with C++ Early Objects
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
24 Searching and Sorting.
Principles of Computing – UFCFA3-30-1
Basics of Recursion Programming with Recursion
Searching and Sorting Arrays
Search,Sort,Recursion.
Algorithm Efficiency and Sorting
Searching and Sorting Arrays
Introduction to Sorting Algorithms
Principles of Computing – UFCFA3-30-1
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Chapter 9: Searching, Sorting, and Algorithm Analysis Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

Topics 9.1 Introduction to Search Algorithms 9.2 Searching an Array of Objects 9.3 Introduction to Sorting Algorithms 9.4 Sorting an Array of Objects 9.5 Sorting and Searching Vectors 9.6 Introduction to Analysis of Algorithms

9.1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, etc.) of information Two algorithms (methods) considered here: Linear search Binary search

Algorithm: Linear Search(position) Given: search_key, N-element array List Answers the question: WHERE IS IT? for (int pos = 0; pos< N; pos++) if ( List [pos] == search_key ) return pos; return -1; Return position

Algorithm: Linear Search(found) Given: search_key, N-element array List Answers the question: IS IS THERE? for (int pos = 0; pos<N; pos++) if (List [pos] == search_key ) return true; return false;

Linear Search Example Array numlist contains Searching for the value 11, linear search examines 17, 23, 5, and 11 Searching for the key value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3, i.e., ALL values! 17 23 5 11 2 29 3 See pr9-01.cpp

Linear Search Tradeoffs Benefits Easy algorithm to understand Array can be in any order Disadvantage Inefficient (slow): for array of N elements, examines N/2 elements on average for value that is found, N elements for value not found 1 million elements, expect 500,000 to be examined.

Binary Search Algorithm (e.g., finding name in phone book) Divide a sorted array into three sections: middle element elements before the middle element elements after of the middle element If the middle element is the search key, done. Otherwise, go to step 1, using only the half of the array that may contain the correct value. Continue steps 1 and 2 until either the value is found or there are no more elements to examine.

Binary Search (BS) Example Array A contains [0] [1] [2] [3] [4] [5] [6] for key 11, BS examines the middle, 11 (A[3]), and then stops (success) for key 3, BS examines 11(A[3]), then 3 (A[1]), then stops. (success) for key 20, BS examines 11(A[3]), then 23 (A[5]), then 17 (A[4]), then stops (not success). 2 3 5 11 17 23 29 See pr9-02.cpp

Binary Search Function – Skeleton A Recursive Algorithm) bool BinSearch(int key, int A[], int lo, int hi) { //If no more elements to search, answer is false. if ( lo > hi ) return false; // BRAKES!! //Divide array into 3 parts: middle, [lo, mid-1], [mid+1, hi] int mid = (lo + hi) / 2; // Check middle element for match. if ( A[mid] == key ) return true; // Search the half of array that can contain the key. if ( key < A[mid] ) return BinSearch(key, A, lo, mid-1); else return BinSearch(key, A, mid+1, hi); }

Binary Search Tradeoffs Benefit Much more efficient than linear search (For array of N elements, performs at most log2N comparisons) Disadvantage Requires that array elements be sorted N 2 4 64 1024 4096 1,000,000 log2N 1 6 10 12 20

Binary Search Exercise Take the test_binarySearch.cpp program from the public repository, and make the following changes to the code. Then observe what happens, learn the rules about sortedness and BRAKES to stop recursion. Comment out the first if statement. Compile and run the program. If it hangs, hit CTR-C. Observe the output. Change the array in the main so that is it NOT SORTED. Compile and run and describe what you observed.

Binary Search Exercise2 In the test_binarySearch.cpp program, revise the binSearch function so that it works for an array sorted in DESCENDING order. Use the test program to demonstrate that the revised function works correctly. > OSsubmit VIDEOalgm test_binarySearch.cpp

9.2 Searching an Array of Objects Search algorithms are not limited to arrays of integers When searching an array of objects or structures, the value being searched for is a member variable, not the entire object or structure Member in object/structure: key field Value used in search: search key See pr9-03.cpp

9.3 Introduction to Sorting Algorithms Sort: arrange values into an order Ascending – smallest value first Descending – smallest value last Two algorithms considered here Selection sort Bubble sort

Selection Sort Algorithm (multiple passes required) Locate smallest element in array positions 0 to last, and swap (exchange) it with element in position 0. Locate smallest element in array positions 1 to last, and exchange it with element in position 1. Continue positions 2 to (last-1), until all elements are in order. Easiest comparison based sort (to me)!! See pr9-05.cpp

Selection Sort Example (pass 1) Array List contains [0] [1] [2] [3] Smallest element, 2, at position 1; Swap List[1] and List[0]. 11 2 29 3 Now in order 2 11 29 3

Selection Sort – Example (pass 2) Smallest element in List[1:3] is 3, at position 3. Swap List[1] and List[3] Now in order 2 11 29 3 Now in order 2 3 29 11

Selection Sort – Example (pass 3) Smallest element in List[2:3] is 11, at position 3. Swap List[2] and List[3] Now in order 2 3 29 11 Now in order 2 3 11 20

Selection Sort Tradeoffs Benefit- more efficient than Bubble Sort (later), due to fewer swaps/exchanges Disadvantage – some consider it harder than Bubble Sort to understand (not me!!) This is how YOU would sort a stack of numbered cards

Algorithm Skeleton / Analysis Properties of Selection Sort: Nested loop: outer controls passes: for (int pass=0; pass<N-1; pass++) inner finds position of smallest/largest in range [pass, N-1] Need 2 functions: void Swap( type &, type &); int posMin(int A[ ], int lo, int hi); // subscript of smallest value in A[ lo:hi ]. Algorithm: for (pass=0; pass<N-1; pass++) { pos = posMin(A, pass, N-1); // Essentially the inner loop. Swap(A[pos], A[pass]); }

Selection Sort Exercise Take the test_binarySearch.cpp program, and convert it to test_selectionSort.cpp that implements the SelectionSort function and tests that it works. The main program must display the array BEFORE and AFTER sorting. > OSsubmit VIDEOalgm test_selectionSort.cpp

Bubble Sort Algorithm Compare 1st two elements and exchange (swap) them if they are out of order. Move down one element and compare 2nd and 3rd elements. Exchange if necessary. Continue until end of array. Pass through array again, repeating process and exchanging as necessary. Repeat until a pass is made with no exchanges. See pr9-04.cpp

Bubble Sort Example (first pass) Array List contains Compare values 17 and 23. In correct order, so no swap (exchange). Compare values 23 and 11. Not in correct order, so swap (exchange) them. 17 23 5 11 Compare values 23 and 5. Out of order, so swap (exchange) them. 11 23 5 23

Bubble Sort Example (pass 2) After first pass, array List contains 5 11 17 23 In order from previous pass Compare values 17 and 5. Not in correct order, so exchange them. 17 5 11 11. Not in correct order, 23

Bubble Sort Example (pass 3) After second pass, array numlist3 contains Compare values 5 and 11. In correct order, so no swap. 5 11 17 23 In order from previous passes

Bubble Sort Tradeoffs Benefit Disadvantage MASTER IT!! Easy to understand and implement Disadvantage Inefficiency makes it slow for large arrays MASTER IT!!

Algorithm Skeleton / Analysis Properties of Bubble Sort (MAX): Nested loop: outer controls passes: for (int pass=1; pass<N-1; pass++) inner compares neighbors, swapping if out of order Need 1 function: void Swap( type &, type &); Algorithm: for (pass=1; pass<N-1; pass++) { for (int k=0; k<N-pass; k++) if (A[k] > A[k+1]) Swap(A[k], A[k+1]); }

Bubble Sort Exercise Take your test_selectionSort program, and convert it to test_bubbleSort.cpp that implements the Bubble Sort function and tests that it works. The main program must display the array BEFORE and AFTER sorting. > OSsubmit VIDEOalgm test_bubbleSort.cpp

9.4 Sorting an Array of Objects As with searching, arrays to be sorted can contain objects or structures The key field determines how the structures or objects will be ordered When exchanging contents of array elements, entire structures or objects must be exchanged, not just the key fields in the structures or objects See pr9-06.cpp

9.5 Sorting and Searching Vectors (LATER) Sorting and searching algorithms can be applied to vectors as well as to arrays Need slight modifications to functions to use vector arguments vector <type> & used in prototype No need to indicate vector size as functions can use size member function to calculate See pr9-07.cpp

9.6 Introduction to Analysis of Algorithms Given two algorithms to solve a problem, what makes one better than the other? Efficiency of an algorithm is measured by space (computer memory used) time (how long to execute the algorithm) Analysis of algorithms is a more effective way to find efficiency than by using empirical data

Analysis of Algorithms: Terminology Computational Problem: problem solved by an algorithm Basic step: operation in the algorithm that executes in a constant amount of time Examples of basic steps: swap/exchange the contents of two variables compare two values

Analysis of Algorithms: Terminology Complexity of an algorithm: the number of basic steps required to execute the algorithm as a function of the input size N (N input values) Worst-case complexity of an algorithm: number of basic steps for input of size N that requires the most work Average case complexity function: the complexity for typical, average inputs of size N

Comparison of Algorithmic Complexity Given algorithms F and G with complexity functions f(n) and g(n) for input of size n If the ratio approaches a constant value as n gets large, F and G have equivalent efficiency If the ratio gets larger as n gets large, algorithm G is more efficient than algorithm F If the ratio approaches 0 as n gets large, algorithm F is more efficient than algorithm G

"Big O" Notation Algorithm F is O(g(n)) ("F is big O of g") for some mathematical function g(n) if the ratio approaches a positive constant as n gets large O(g(n)) defines a complexity class for the algorithm F Increasing complexity class means faster rate of growth, less efficient algorithm

Chapter 9: Searching, Sorting, and Algorithm Analysis Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda