560 likes | 577 Views
16. Searching and Sorting. 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.
E N D
16 • Searching and Sorting
With sobs and tears he sorted outThose 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
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.
16.1Introduction • 16.2Searching Algorithms • 16.2.1Linear Search • 16.2.2Binary Search • 16.3Sorting Algorithms • 16.3.1Selection Sort • 16.3.2Insertion Sort • 16.3.3Merge Sort • 16.4Invariants • 16.5Wrap-up
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
16.2 Searching Algorithms • Examples of searching • Looking up a phone number • Accessing a Web site • Checking a word in the dictionary
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
Iterate through array Test each element sequentially Return index containing search key
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
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(n2) • Grows proportional to the square of the size of the array
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
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.
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
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 element If search element is less than middle element Eliminate higher half Else, eliminate lower half
Update middle of array Return location of element
Efficiency of Binary Search • Binary search • Each comparison halves the size of the remaining array • Results in O(log n) • Called logarithmic run time
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
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
Variable to store index of smallest element Loop length–1 times
Loop over remaining elements Locate smallest remaining element Swap smallest element with first unsorted element
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(n2)
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
Declare variable to store element to be inserted Iterate over length–1 elements Store value to insert Search for location to place inserted element Move one element to the right Decrement location to insert element Insert element into sorted place
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(n2)
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
Compute middle of array Compute element one spot to right of middle Recursively sort second half of array Test for base case Recursively sort first half of array Merge the two sorted subarrays
Index of element in left array Index of element in right array Index to place element in combined array Array to hold sorted elements Loop until reach end of either array Determine smaller of two elements Place smaller element in combined array
If left array is empty Fill with elements of right array If right array is empty Fill with elements of left array Copy values back to original array