1 / 209

Sorting and Searching

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.

dlynn
Download Presentation

Sorting and Searching

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

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

  3. Searching

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  31. Sorting

  32. Sorting Fun: Why Not Bubble Sort? CS 221 - Computer Science II

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

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

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

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

  37. Selection Sort in Practice Swap the smallest entry with the first entry. [0][1] [2] [3] [4] [5] CS 221 - Computer Science II

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

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

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

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

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

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

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

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

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

  47. Selection Sort in Practice The array is now sorted. [0][1] [2] [3] [4] [5] CS 221 - Computer Science II

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

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

  50. Like Selection Sort, Insertion Sort algorithm views the array as having a sorted side and an unsorted side. Insertion Sort in Practice [0][1] [2] [3] [4] [5] CS 221 - Computer Science II

More Related