Presentation is loading. Please wait.

Presentation is loading. Please wait.

 1992-2007 Pearson Education, Inc. All rights reserved. 1 16 Searching and Sorting.

Similar presentations


Presentation on theme: " 1992-2007 Pearson Education, Inc. All rights reserved. 1 16 Searching and Sorting."— Presentation transcript:

1  1992-2007 Pearson Education, Inc. All rights reserved. 1 16 Searching and Sorting

2  1992-2007 Pearson Education Inc. All rights reserved. 2 With sobs and tears he sorted out Those of the largest size... — Lewis Carroll Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. — Robert Herrick ‘Tis in my memory lock’d, And you yourself shall keep the key of it. — William Shakespeare It is an immutable law in business that words are words, explanations are explanations, promises are promises — but only performance is reality. — Harold S. Green

3  1992-2007 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To search for a given value in an array using linear search and binary search.  To sort arrays using the iterative selection and insertion sort algorithms.  To sort arrays using the recursive merge sort algorithm.  To determine the efficiency of searching and sorting algorithms.  To use loop invariants to help ensure the correctness of your programs.

4  1992-2007 Pearson Education, Inc. All rights reserved. 4 16.1 Introduction 16.2 Searching Algorithms 16.2.1 Linear Search 16.2.2 Binary Search 16.3 Sorting Algorithms 16.3.1 Selection Sort 16.3.2 Insertion Sort 16.3.3 Merge Sort 16.4 Invariants 16.5 Wrap-up

5  1992-2007 Pearson Education, Inc. All rights reserved. 5 16.1 Introduction Searching – Determining whether a search key is present in data Sorting – Places data in order based on one or more sort keys

6  1992-2007 Pearson Education, Inc. All rights reserved. 6 Fig. 16.1 | Searching and sorting algorithms in this text.

7  1992-2007 Pearson Education, Inc. All rights reserved. 7 16.2 Searching Algorithms Examples of searching – Looking up a phone number – Accessing a Web site – Checking a word in the dictionary

8  1992-2007 Pearson Education, Inc. All rights reserved. 8 16.2.1 Linear Search Linear search – Searches each element sequentially – If search key is not present Tests each element When algorithm reaches end of array, informs user search key is not present – If search key is present Test each element until it finds a match

9  1992-2007 Pearson Education, Inc. All rights reserved. 9

10 10 Iterate through arrayTest each element sequentially Return index containing search key

11  1992-2007 Pearson Education, Inc. All rights reserved. 11

12  1992-2007 Pearson Education, Inc. All rights reserved. 12

13  1992-2007 Pearson Education, Inc. All rights reserved. 13 Efficiency of Linear Search Big O Notation – Indicates the worst-case run time for an algorithm – In other words, how hard an algorithm has to work to solve a problem – Constant run time O(1) Does not grow as the size of the array increases – Linear run time O(n) Grows proportional to the size of the array

14  1992-2007 Pearson Education, Inc. All rights reserved. 14 Efficiency of Linear Search Big O Notation – Constant run time O(1) Does not grow as the size of the array increases – Linear run time O(n) Grows proportional to the size of the array – Quadratic run time O(n 2 ) Grows proportional to the square of the size of the array

15  1992-2007 Pearson Education, Inc. All rights reserved. 15 Efficiency of Linear Search Linear search algorithm – O(n) – Worst case: algorithm checks every element before being able to determine if the search key is not present – Grows proportional to the size of the array

16  1992-2007 Pearson Education, Inc. All rights reserved. 16 Performance Tip 16.1 Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realize maximum performance.

17  1992-2007 Pearson Education, Inc. All rights reserved. 17 16.2.2 Binary Search Binary search – More efficient than linear search – Requires elements to be sorted – Tests the middle element in an array If it is the search key, algorithm returns Otherwise, if the search key is smaller, eliminates larger half of array If the search key is larger, eliminates smaller half of array – Each iteration eliminates half of the remaining elements

18  1992-2007 Pearson Education, Inc. All rights reserved. 18

19  1992-2007 Pearson Education, Inc. All rights reserved. 19 Store high, low and middle of remaining array to search Loop until key is found or no elements left to search If search element is the middle element Return middle elementIf search element is less than middle element Eliminate higher halfElse, eliminate lower half

20  1992-2007 Pearson Education, Inc. All rights reserved. 20 Update middle of array Return location of element

21  1992-2007 Pearson Education, Inc. All rights reserved. 21

22  1992-2007 Pearson Education, Inc. All rights reserved. 22

23  1992-2007 Pearson Education, Inc. All rights reserved. 23

24  1992-2007 Pearson Education, Inc. All rights reserved. 24 Efficiency of Binary Search Binary search – Each comparison halves the size of the remaining array – Results in O(log n) – Called logarithmic run time

25  1992-2007 Pearson Education, Inc. All rights reserved. 25 16.3 Sorting Algorithms Sorting data – Placing data into some particular order A bank sorts checks by account number Telephone companies sort accounts by name – End result is always the same – a sorted array – Choice of algorithm affects how you achieve the result and, most importantly, how fast you achieve the result

26  1992-2007 Pearson Education, Inc. All rights reserved. 26 16.3.1 Selection Sort Selection sort – Simple, but inefficient sorting algorithm – First iteration selects smallest element in array and swaps it with the first element – Each iteration selects the smallest remaining unsorted element and swaps it with the next element at the front of the array – After i iterations, the smallest i elements will be sorted in the first i elements of the array

27  1992-2007 Pearson Education, Inc. All rights reserved. 27 Variable to store index of smallest element Loop length – 1 times

28  1992-2007 Pearson Education, Inc. All rights reserved. 28 Loop over remaining elements Locate smallest remaining element Swap smallest element with first unsorted element

29  1992-2007 Pearson Education, Inc. All rights reserved. 29

30  1992-2007 Pearson Education, Inc. All rights reserved. 30

31  1992-2007 Pearson Education, Inc. All rights reserved. 31

32  1992-2007 Pearson Education, Inc. All rights reserved. 32 Efficiency of Selection Sort Selection sort – Outer for loop iterates over n – 1 elements – Inner for loop iterates over remaining elements in the array – Results in O(n 2 )

33  1992-2007 Pearson Education, Inc. All rights reserved. 33 16.3.2 Insertion Sort Insertion sort – Another simple, but inefficient sorting algorithm – First pass takes the second element and inserts it into the correct order with the first element – Each iteration takes the next element in the array and inserts it into the sorted elements at the beginning of the array – After i iterations, the first i elements of the array are in sorted order

34  1992-2007 Pearson Education, Inc. All rights reserved. 34

35  1992-2007 Pearson Education, Inc. All rights reserved. 35 Declare variable to store element to be inserted Iterate over length – 1 elementsStore value to insertSearch for location to place inserted element Move one element to the rightDecrement location to insert element Insert element into sorted place

36  1992-2007 Pearson Education, Inc. All rights reserved. 36

37  1992-2007 Pearson Education, Inc. All rights reserved. 37

38  1992-2007 Pearson Education, Inc. All rights reserved. 38

39  1992-2007 Pearson Education, Inc. All rights reserved. 39

40  1992-2007 Pearson Education, Inc. All rights reserved. 40 Efficiency of Insertion Sort Insertion sort – Outer for loop iterates over n – 1 elements – Inner while loop iterates over preceding elements of array – Results in O(n 2 )

41  1992-2007 Pearson Education, Inc. All rights reserved. 41 16.3.3 Merge Sort Merge sort – More efficient sorting algorithm, but also more complex – Splits array into two approximately equal sized subarrays, sorts each subarray, then merges the subarrays – The following implementation is recursive Base case is a one-element array which cannot be unsorted Recursion step splits an array into two pieces, sorts each piece, then merges the sorted pieces

42  1992-2007 Pearson Education, Inc. All rights reserved. 42 Call recursive helper method

43  1992-2007 Pearson Education, Inc. All rights reserved. 43 Test for base caseCompute middle of array Compute element one spot to right of middle Recursively sort first half of arrayRecursively sort second half of array Merge the two sorted subarrays

44  1992-2007 Pearson Education, Inc. All rights reserved. 44 Index of element in left arrayIndex of element in right arrayIndex to place element in combined array Array to hold sorted elements Loop until reach end of either arrayDetermine smaller of two elements Place smaller element in combined array

45  1992-2007 Pearson Education, Inc. All rights reserved. 45 If left array is emptyFill with elements of right arrayIf right array is emptyFill with elements of left array Copy values back to original array

46  1992-2007 Pearson Education, Inc. All rights reserved. 46

47  1992-2007 Pearson Education, Inc. All rights reserved. 47

48  1992-2007 Pearson Education, Inc. All rights reserved. 48

49  1992-2007 Pearson Education, Inc. All rights reserved. 49

50  1992-2007 Pearson Education, Inc. All rights reserved. 50

51  1992-2007 Pearson Education, Inc. All rights reserved. 51 Efficiency of Merge Sort Merge sort – Far more efficient that selection sort or insertion sort – Last merge requires n – 1 comparisons to merge entire array – Each lower level has twice as many calls to method merge, with each call operating on an array half the size which results in O(n) total comparisons – There will be O(log n) levels – Results in O(n log n)

52  1992-2007 Pearson Education, Inc. All rights reserved. 52 Fig. 16.12 | Searching and sorting algorithms with Big O values.

53  1992-2007 Pearson Education, Inc. All rights reserved. 53 Fig. 16.13 | Number of comparisons for common Big O notations.

54  1992-2007 Pearson Education, Inc. All rights reserved. 54 16.4 Invariants Invariant – Is an assertion that is true before and after a portion of your code executes – Most common type is a loop invariant

55  1992-2007 Pearson Education, Inc. All rights reserved. 55 16.4 Invariants Loop invariant remains true: – Before the execution of the loop – After every iteration of the loop body – When the loop terminates

56  1992-2007 Pearson Education, Inc. All rights reserved. 56 16.4 Invariants Four steps to developing a loop from a loop invariant – Set initial values for any loop control variables – Determine the condition that causes the loop to terminate – Modify the control variable so the loop progresses toward termination – Check that the invariant remains true at the end of each iteration


Download ppt " 1992-2007 Pearson Education, Inc. All rights reserved. 1 16 Searching and Sorting."

Similar presentations


Ads by Google