Download presentation
Presentation is loading. Please wait.
1
16 Searching and Sorting
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
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.
4
16.1 Introduction 16.2 Searching Algorithms Linear Search Binary Search 16.3 Sorting Algorithms Selection Sort Insertion Sort Merge Sort 16.4 Invariants 16.5 Wrap-up
5
16.1 Introduction Searching Sorting
Determining whether a search key is present in data Sorting Places data in order based on one or more sort keys
6
Fig. 16.1 | Searching and sorting algorithms in this text.
7
16.2 Searching Algorithms Examples of searching
Looking up a phone number Accessing a Web site Checking a word in the dictionary
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
Outline LinearArray.java (1 of 2)
10
Outline Iterate through array Test each element sequentially (2 of 2)
LinearArray.java (2 of 2) Test each element sequentially Return index containing search key
11
Outline LinearSearchTest .java (1 of 2)
12
Outline LinearSearchTest .java (2 of 2)
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
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
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
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
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
Outline BinaryArray.java (1 of 3)
19
Outline Store high, low and middle of remaining array to search
BinaryArray.java (2 of 3) 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
20
Return location of element
Update middle of array Outline Return location of element BinaryArray.java (3 of 3)
21
Outline BinarySearchTest .java (1 of 3)
22
Outline BinarySearchTest .java (2 of 3)
23
Outline BinarySearchTest .java (3 of 3)
24
Efficiency of Binary Search
Each comparison halves the size of the remaining array Results in O(log n) Called logarithmic run time
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
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
Variable to store index of smallest element
Outline SelectionSort.java (1 of 3) Variable to store index of smallest element Loop length – 1 times
28
Outline Loop over remaining elements Locate smallest remaining element
SelectionSort.java (2 of 3) Swap smallest element with first unsorted element
29
Outline SelectionSort.java (3 of 3)
30
Outline SelectionSortTest .java (1 of 2)
31
Outline SelectionSortTest .java (2 of 2)
32
Efficiency of Selection Sort
Outer for loop iterates over n – 1 elements Inner for loop iterates over remaining elements in the array Results in O(n2)
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
Outline InsertionSort.java (1 of 4)
35
Outline Declare variable to store element to be inserted
Iterate over length – 1 elements InsertionSort.java (2 of 4) 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
36
Outline InsertionSort.java (3 of 4)
37
Outline InsertionSort.java (4 of 4)
38
Outline InsertionSortTest .java (1 of 2)
39
Outline InsertionSortTest .java (2 of 2)
40
Efficiency of Insertion Sort
Outer for loop iterates over n – 1 elements Inner while loop iterates over preceding elements of array Results in O(n2)
41
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
Call recursive helper method
Outline MergeSort.java (1 of 5) Call recursive helper method
43
Outline Test for base case Compute middle of array (2 of 5)
MergeSort.java (2 of 5) Compute element one spot to right of middle Recursively sort first half of array Recursively sort second half of array Merge the two sorted subarrays
44
Outline Index of element in left array Index of element in right array
MergeSort.java (3 of 5) 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
45
Outline If left array is empty Fill with elements of right array
If right array is empty MergeSort.java (4 of 5) Fill with elements of left array Copy values back to original array
46
Outline MergeSort.java (5 of 5)
47
Outline MergeSortTest.java (1 of 4)
48
Outline MergeSortTest.java (2 of 4)
49
Outline MergeSortTest.java (3 of 4)
50
Outline MergeSortTest.java (3 of 4)
51
Efficiency of 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
Fig. 16.12 | Searching and sorting algorithms with Big O values.
53
Fig. 16.13 | Number of comparisons for common Big O notations.
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
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
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
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.