1 / 37

Searching and Sorting Algorithms: Introduction, Linear Search, Binary Search, Selection Sort, Bubble Sort

This chapter introduces different search and sorting algorithms in C++. Topics covered include linear search, binary search, selection sort, and bubble sort.

dsandoval
Download Presentation

Searching and Sorting Algorithms: Introduction, Linear Search, Binary Search, Selection Sort, Bubble Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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

  5. 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;

  6. 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!

  7. 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.

  8. Binary Search Algorithm(e.g., finding name in phone book) • Divide a sorted array into three sections: • middle element • elements beforethe middle element • elements afterof 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.

  9. 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).

  10. Binary Search Function – SkeletonA 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); }

  11. 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

  12. 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.

  13. 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

  14. 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

  15. 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

  16. 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)!!

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

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

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

  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

  21. 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 &); • intposMin(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]); }

  22. 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

  23. 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.

  24. 17 23 5 11 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. Compare values 23 and 5. Out of order, so swap (exchange) them. Bubble Sort Example(first pass) 11 23 5 23 Array List contains

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

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

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

  28. 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]); }

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. "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

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

More Related