2.09k likes | 2.13k Views
Sorting and Searching. "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter and the Sorcerer's Stone. Sorting and Searching. Fundamental problems in computer science and programming.
E N D
Sorting and Searching "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter and the Sorcerer's Stone
Sorting and Searching • Fundamental problems in computer science and programming. • Sorting done to make searching easier. • Multiple algorithms to solve the same problem. • How do we know which algorithm is "better"? • Look at searching first. • Examples will use arrays of ints to illustrate algorithms. CS 221 - Computer Science II
Searching • Given a list of data, find the location of a particular value or report that value is not present. • Linear Search • Intuitive approach: • Start at first item. • Is it the one I am looking for? • If not, go to next item. • Repeat until found or all items checked. • If items not sorted or unsortable, this approach is necessary. CS 221 - Computer Science II
Linear Search /* return the index of the first occurrence of target in list, or -1 if target not present in list */ public int linearSearch(int list[], int target) { int i = 0; while(i < list.length && list[i] != target) i++; if(i >= list.length) return -1; else return i; } CS 221 - Computer Science II
Question 1 • What is the average case Big-O of linear search in an array with n items? • O(n) • O(n2) • O(1) • O(log(n)) • O(n log(n)) CS 221 - Computer Science II
Question 1 • What is the average case Big-O of linear search in an array with n items? • O(n) • O(n2) • O(1) • O(log(n)) • O(n log(n)) CS 221 - Computer Science II
Searching in a Sorted List • If items are sorted, we can divide and conquer. • Divide your work in half with each step. • Generally a good thing. • Uses recursion. • Binary Search: • List in sorted in ascending order. • Start at middle of list. • Is that the item? Done. • If not, • is it less than item? • Look in second half of list. • is it greater than? • Look in first half of list. • Repeat until found, or sub-list size = 0. CS 221 - Computer Science II
Recursive Binary Search public static int search(int list[], int target) { return b-search(list, target, 0, list.length – 1); } public static int b-search(int list[], int target, int lo, int hi) { if( lo <= hi ){ int mid = (hi + lo) / 2; if( list[mid] == target ) return mid; else if( list[mid] > target ) return b-search(list, target, lo, mid – 1); else return b-search(list, target, mid + 1, hi); } return -1; } CS 221 - Computer Science II
Binary Search I • Given target and sorted array list[], find index i such that list[i] = target, or report that no such index exists. • Example 1: Search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi CS 221 - Computer Science II
Binary Search I • 33 < 53, so look in lower half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi CS 221 - Computer Science II
Binary Search I 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi CS 221 - Computer Science II
Binary Search I • 25 < 33, so look in upper half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi CS 221 - Computer Science II
Binary Search I 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi CS 221 - Computer Science II
Binary Search I • 43 < 33, so look in lower half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi CS 221 - Computer Science II
Binary Search I 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohi CS 221 - Computer Science II
Binary Search I • 33 = 33, so found value. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid CS 221 - Computer Science II
Binary Search I • Done. 6 13 14 25 43 51 53 64 72 84 93 95 96 97 33 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid CS 221 - Computer Science II
Binary Search II • Example 2: Search for 90. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi CS 221 - Computer Science II
Binary Search II • 90 > 53, so look in upper half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi CS 221 - Computer Science II
Binary Search II 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi CS 221 - Computer Science II
Binary Search II • 90 < 93, so look in lower half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi CS 221 - Computer Science II
Binary Search II 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi CS 221 - Computer Science II
Binary Search II • 90 > 72, so look in upper half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi CS 221 - Computer Science II
Binary Search II 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohi CS 221 - Computer Science II
Binary Search II • 90 > 84, so look in upper half? 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid CS 221 - Computer Science II
Binary Search II • lo > hi, so no list left to search. • Done. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 hi lo CS 221 - Computer Science II
Question 2 What is the worst case Big O of binary search in an array with n items, if an item is present? O(n) O(n2) O(1) O(log(n)) O(n log(n)) CS 221 - Computer Science II
Question 2 What is the worst case Big O of binary search in an array with n items, if an item is present? O(n) O(n2) O(1) O(log(n)) O(n log(n)) CS 221 - Computer Science II
Other Searching Algorithms • Interpolation Search • More like what people really do. • Binary Search Trees • Hash Table Searching • Best-First • A* CS 221 - Computer Science II
Sorting • A fundamental application for computers. • Makes finding data (searching) faster. • Many different algorithms for sorting. • The "simple" sorts run in quadratic time O(n2). • Selection Sort • Insertion Sort • Bubble Sort CS 221 - Computer Science II
Selection Sort CS 221 - Computer Science II
Selection Sort • Given an array of length n, • Search elements0 throughn-1, select smallest. • Swap it with the element at location 0. • Search elements1 through n-1, select smallest. • Swap it with the element at location 1. • Search elements 2 through n-1, select smallest. • Swap it with the element at location 2. • Search elements 3 through n-1, select smallest. • Swap it with the element at location 3. • Continue in this fashion until there’s nothing left to search. CS 221 - Computer Science II
Example: An array of integers, sort from smallest to largest. Sorting an Array of Integers [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Repeatedly select the smallest element, and move this element to the front. [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Swap the smallest entry with the first entry. [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Part of the array is now sorted. Sorted side Unsorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Find the smallest element in the unsorted side. Sorted side Unsorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Swap with the first element of unsorted side. Sorted side Unsorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice We have increased the size of the sorted side by one element. Sorted side Unsorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice The process continues... Sorted side Unsorted side Smallest from unsorted [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Sorted side Unsorted side • The process continues... Swap with front [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice The process continues... Sorted side is bigger Sorted side Unsorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Keep adding one more number to the sorted side. Sorted side Unsorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice Stop when the unsorted side has just one number, since that number must be the largest number. Unsorted side Sorted side [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort in Practice The array is now sorted. [0][1] [2] [3] [4] [5] CS 221 - Computer Science II
Selection Sort Algorithm public static void selectionSort(int list[]) { int min; int temp; for(inti = 0; i < list.length - 1; i++) { min = i; for(int j = i + 1; j < list.length; j++) { if( list[j] < list[min] ) min = j; } temp = list[i]; list[i] = list[min]; list[min] = temp; } } • Big O? CS 221 - Computer Science II
Insertion Sort CS 221 - Computer Science II
Insertion Sort • Another of the O(n2) sorts: • Start with first item, assume it’s sorted. • Compare the second item to the first. • If it’s smaller, swap. • Compare the third item to the second. • If smaller, swap. • Compare again with first, if smaller swap again. • And so forth… CS 221 - Computer Science II