topic 11 s sorting and searching ti d s hi
play

Topic 11 S Sorting and Searching ti d S hi "There's - PowerPoint PPT Presentation

Topic 11 S Sorting and Searching ti d S hi "There's nothing in your head the There s nothing in your head the sorting hat can't see. So try me on and I will tell you where you on and I will tell you where you ought to be." -The


  1. Topic 11 S Sorting and Searching ti d S hi "There's nothing in your head the There s nothing in your head the sorting hat can't see. So try me on and I will tell you where you on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter The Sorting Hat Harry Potter and the Sorcerer's Stone CS 307 Fundamentals of 1 Computer Science Sorting and Searching

  2. Sorting and Searching 8 Fundamental problems in computer science and programming 8 Sorting done to make searching easier 8 Multiple different algorithms to solve the u t p e d e e t a go t s to so e t e same problem – How do we know which algorithm is "better"? How do we know which algorithm is better ? 8 Look at searching first 8 E amples 8 Examples will use arrays of ints to illustrate ill se arra s of ints to ill strate algorithms CS 307 Fundamentals of 2 Computer Science Sorting and Searching

  3. Searching CS 307 Fundamentals of 3 Computer Science Sorting and Searching

  4. Searching 8 Gi 8 Given a list of data find the location of a li t f d t fi d th l ti f particular value or report that value is not present present 8 linear search – intuitive approach int iti e approach – start at first item – is it the one I am looking for? is it the one I am looking for? – if not go to next item – repeat until found or all items checked repeat until found or all items checked 8 If items not sorted or unsortable this approach is necessary approach is necessary CS 307 Fundamentals of 4 Computer Science Sorting and Searching

  5. Linear Search /* pre: list != null p post: 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) { for(int i = 0; i < list.length; i++) if( list[i] == target ) if( list[i] target ) return i; return -1; } CS 307 Fundamentals of 5 Computer Science Sorting and Searching

  6. Linear Search, Generic /* pre: list != null, target != null post: return the index of the first occurrence of target in list or -1 if target not present in list list */ public int linearSearch(Object[] list, Object target) { for(int i = 0; i < list.length; i++) if( list[i] != null && list[i].equals(target) ) i i i i i return i; return -1; } T(N)? Big O? Best case, worst case, average case? CS 307 Fundamentals of 6 Computer Science Sorting and Searching

  7. Attendance Question 1 8 What is the average case Big O of linear search in an array with N items, if an item is present? A. O(N) B. O(N 2 ) C O(1) C. O(1) D. O(logN) E O(Nl E. O(NlogN) N) CS 307 Fundamentals of 7 Computer Science Sorting and Searching

  8. Searching in a Sorted List 8 If items are sorted then we can divide and 8 If it t d th di id d conquer 8 dividing your work in half with each step 8 di idi k i h lf ith h t – generally a good thing 8 The Binary Search on List in Ascending order 8 Th Bi S h Li t i A di d – Start at middle of list – is that the item? i th t th it ? – If not is it less than or greater than the item? – less than, move to second half of list l th t d h lf f li t – greater than, move to first half of list – repeat until found or sub list size = 0 repeat until found or sub list size = 0 CS 307 Fundamentals of 8 Computer Science Sorting and Searching

  9. Binary Search list low item middle item high item Is middle item what we are looking for? If not is it Is middle item what we are looking for? If not is it more or less than the target item? (Assume lower) list low middle high item item item and so forth… CS 307 Fundamentals of 9 Computer Science Sorting and Searching

  10. Binary Search in Action 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 public static int bsearch(int[] list, int target) public static int bsearch(int[] list, int target) { int result = -1; int low = 0; int high = list.length - 1; int mid; while( result == -1 && low <= high ) { mid = low + ((high - low) / 2); if( list[mid] == target ) result = mid; else if( list[mid] < target) low = mid + 1; else high = mid - 1; } return result; } // mid = ( low + high ) / 2; // may overflow!!! // // or mid = (low + high) >>> 1; using bitwise op id (l hi h) 1 i bi i CS 307 Fundamentals of 10 Computer Science Sorting and Searching

  11. Trace When Key == 3 Trace When Key == 30 Variables of Interest? CS 307 Fundamentals of 11 Computer Science Sorting and Searching

  12. Attendance Question 2 What is the worst case Big O of binary search in an array with N items, if an item is present? A. O(N) B. O(N 2 ) C. O(1) D. O(logN) E. O(NlogN) CS 307 Fundamentals of 12 Computer Science Sorting and Searching

  13. Generic Binary Search public static int bsearch(Comparable[] list, Comparable target) { int result = -1; int low = 0; int high = list.length - 1; g g int mid; while( result == -1 && low <= high ) { mid = low + ((high - low) / 2); if( target.equals(list[mid]) ) if( target equals(list[mid]) ) result = mid; else if(target.compareTo(list[mid]) > 0) low = mid + 1; else l high = mid - 1; } return result; } CS 307 Fundamentals of 13 Computer Science Sorting and Searching

  14. Recursive Binary Search public static int bsearch(int[] list public static int bsearch(int[] list, int target){ int target){ return bsearch(list, target, 0, list.length – 1); } public static int bsearch(int[] list, int target, bli i i b h(i [] li i int first, int last){ if( first <= last ){ int mid = low + ((high - low) / 2); if( list[mid] == target ) return mid; else if( list[mid] > target ) return bsearch(list, target, first, mid – 1); return bsearch(list, target, first, mid 1); else return bsearch(list, target, mid + 1, last); } return -1; return 1; } CS 307 Fundamentals of 14 Computer Science Sorting and Searching

  15. Other Searching Algorithms 8 Interpolation Search – more like what people really do 8 Indexed Searching 8 Binary Search Trees Binary Search Trees 8 Hash Table Searching 8 Grover's Algorithm (Waiting for 8 G ' Al ith (W iti f quantum computers to be built) 8 best-first 8 A* CS 307 Fundamentals of 15 Computer Science Sorting and Searching

  16. Sorting Sorting CS 307 Fundamentals of 16 Computer Science Sorting and Searching

  17. Sorting Fun Sorting Fun Why Not Bubble Sort? y CS 307 Fundamentals of 17 Computer Science Sorting and Searching

  18. Sorting 8 A fundamental application for computers 8 A fundamental application for computers 8 Done to make finding data (searching) faster 8 M 8 Many different algorithms for sorting diff t l ith f ti 8 One of the difficulties with sorting is working with a fixed size storage container (array) ith fi d i t t i ( ) – if resize, that is expensive (slow) 8 The "simple" sorts run in quadratic time O(N 2 ) – bubble sort b bbl t – selection sort – insertion sort i ti t CS 307 Fundamentals of 18 Computer Science Sorting and Searching

  19. Stable Sorting 8 A 8 A property of sorts t f t 8 If a sort guarantees the relative order of equal items stays the same then it is a stable l it t th th it i t bl sort 8 [7 1 , 6, 7 2 , 5, 1, 2, 7 3 , -5] 8 [7 6 7 5 1 2 7 5] – subscripts added for clarity 8 [ 5 1 2 5 6 7 8 [-5, 1, 2, 5, 6, 7 1 , 7 2 , 7 3 ] 7 7 ] – result of stable sort 8 Real world example: 8 R l ld l – sort a table in Wikipedia by one criteria, then another – sort by country then by major wins – sort by country, then by major wins CS 307 Fundamentals of 19 Computer Science Sorting and Searching

  20. Selection sort 8 Algorithm – Search through the list and find the smallest element – swap the smallest element with the first element – repeat starting at second element and find the second repeat starting at second element and find the second smallest element public static void selectionSort(int[] list) { { int min; int min; int temp; for(int i = 0; i < list.length - 1; i++) { min = i; for(int j = i + 1; j < list.length; j++) if( list[j] < list[min] ) min = j; t temp = list[i]; li t[i] list[i] = list[min]; list[min] = temp; } } } CS 307 Fundamentals of 20 Computer Science Sorting and Searching

  21. Selection Sort in Practice 44 68 191 119 119 37 83 82 191 45 158 130 76 153 39 25 What is the T(N) actual number of statements What is the T(N), actual number of statements executed, of the selection sort code, given a list of N elements? What is the Big O? g CS 307 Fundamentals of 21 Computer Science Sorting and Searching

  22. Generic Selection Sort public void selectionSort(Comparable[] list) { int min; Comparable temp; for(int i = 0; i < list.length - 1; i++) { ( ; g ; ) { { min = i; for(int j = i + 1; j < list.length; j++) if( list[min].compareTo(list[j]) > 0 ) min = j; temp = list[i]; list[i] = list[min]; list[min] = temp; } } 8 B 8 Best case, worst case, average case Big O? t t Bi O? CS 307 Fundamentals of 22 Computer Science Sorting and Searching

  23. Attendance Question 3 Is selection sort always stable? A. Yes B. No CS 307 Fundamentals of 23 Computer Science Sorting and Searching

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend