sorting and searching topic 11 sorting and searching s ti
play

Sorting and Searching Topic 11 Sorting and Searching S ti d S - PowerPoint PPT Presentation

Sorting and Searching Topic 11 Sorting and Searching S ti d S hi Fundamental problems in computer science and programming "There's nothing in your head the There s nothing in your head the Sorting done to make searching easier


  1. Sorting and Searching Topic 11 Sorting and Searching S ti d S hi � Fundamental problems in computer science and programming "There's nothing in your head the There s nothing in your head the � Sorting done to make searching easier sorting hat can't see. So try me � Multiple different algorithms to solve the u t p e d e e t a go t s to so e t e on and I will tell you where you on and I will tell you where you same problem ought to be." – How do we know which algorithm is "better"? How do we know which algorithm is better ? -The Sorting Hat, Harry Potter The Sorting Hat Harry Potter � Look at searching first and the Sorcerer's Stone � E amples � Examples will use arrays of ints to illustrate ill se arra s of ints to ill strate algorithms CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Sorting and Searching Computer Science Sorting and Searching Searching � Given a list of data find the location of a � Gi li t f d t fi d th l ti f Searching particular value or report that value is not present present � 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 � If items not sorted or unsortable this approach is necessary approach is necessary CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science Sorting and Searching Computer Science Sorting and Searching

  2. Linear Search Linear Search, Generic /* p pre: list != null /* pre: list != null, target != null post: return the index of the first occurrence post: return the index of the first occurrence of target in list or -1 if target not present in of target in list or -1 if target not present in list list list */ */ */ public int linearSearch(int[] list, int target) { public int linearSearch(Object[] list, Object target) { for(int i = 0; i < list.length; i++) for(int i = 0; i < list.length; i++) if( list[i] == target ) if( list[i] target ) if( list[i] != null && list[i].equals(target) ) i i i i i return i; return i; return -1; return -1; } } T(N)? Big O? Best case, worst case, average case? CS 307 Fundamentals of CS 307 Fundamentals of 5 6 Computer Science Sorting and Searching Computer Science Sorting and Searching Attendance Question 1 Searching in a Sorted List � If items are sorted then we can divide and � If it t d th di id d � What is the average case Big O of linear conquer search in an array with N items, if an item is � dividing your work in half with each step � di idi k i h lf ith h t present? – generally a good thing A. O(N) � Th � The Binary Search on List in Ascending order Bi S h Li t i A di d B. O(N 2 ) – Start at middle of list C O(1) C. O(1) – is that the item? i th t th it ? D. O(logN) – 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 E O(Nl E. O(NlogN) N) – 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 7 CS 307 Fundamentals of 8 Computer Science Sorting and Searching Computer Science Sorting and Searching

  3. Binary Search in Action Binary Search 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 list 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; low item middle item high item int low = 0; int high = list.length - 1; Is middle item what we are looking for? If not is it Is middle item what we are looking for? If not is it int mid; while( result == -1 && low <= high ) more or less than the target item? (Assume lower) { mid = low + ((high - low) / 2); if( list[mid] == target ) result = mid; else if( list[mid] < target) list low = mid + 1; else high = mid - 1; } low middle high return result; } item item item // mid = ( low + high ) / 2; // may overflow!!! // or mid = (low + high) >>> 1; using bitwise op // id (l hi h) 1 i bi i and so forth… CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Sorting and Searching Computer Science Sorting and Searching Attendance Question 2 Trace When Key == 3 What is the worst case Big O of binary search in Trace When Key == 30 an array with N items, if an item is present? A. O(N) Variables of Interest? B. O(N 2 ) C. O(1) D. O(logN) E. O(NlogN) CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Sorting and Searching Computer Science Sorting and Searching

  4. Generic Binary Search Recursive Binary Search public static int bsearch(int[] list public static int bsearch(int[] list, int target){ int target){ public static int bsearch(Comparable[] list, Comparable target) return bsearch(list, target, 0, list.length – 1); { int result = -1; } int low = 0; int high = list.length - 1; g g public static int bsearch(int[] list, int target, bli i i b h(i [] li i int mid; int first, int last){ while( result == -1 && low <= high ) if( first <= last ){ { mid = low + ((high - low) / 2); int mid = low + ((high - low) / 2); if( target equals(list[mid]) ) if( target.equals(list[mid]) ) if( list[mid] == target ) result = mid; return mid; else if(target.compareTo(list[mid]) > 0) else if( list[mid] > target ) low = mid + 1; return bsearch(list, target, first, mid return bsearch(list, target, first, mid – 1); 1); else l else high = mid - 1; return bsearch(list, target, mid + 1, last); } } return result; return -1; return 1; } } CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Sorting and Searching Computer Science Sorting and Searching Other Searching Algorithms � Interpolation Search Sorting Sorting – more like what people really do � Indexed Searching � Binary Search Trees Binary Search Trees � Hash Table Searching � Grover's Algorithm (Waiting for � G ' Al ith (W iti f quantum computers to be built) � best-first � A* CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Sorting and Searching Computer Science Sorting and Searching

  5. Sorting Sorting Fun Sorting Fun � A fundamental application for computers � A fundamental application for computers � Done to make finding data (searching) faster Why Not Bubble Sort? y � Many different algorithms for sorting � M diff t l ith f ti � 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) � 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 CS 307 Fundamentals of 17 18 Computer Science Sorting and Searching Computer Science Sorting and Searching Selection sort Stable Sorting � Algorithm � A property of sorts � A t f t – Search through the list and find the smallest element � If a sort guarantees the relative order of – 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 equal items stays the same then it is a stable l it t th th it i t bl smallest element sort public static void selectionSort(int[] list) { { int min; int min; � [7 1 , 6, 7 2 , 5, 1, 2, 7 3 , -5] � [7 6 7 5 1 2 7 5] int temp; – subscripts added for clarity for(int i = 0; i < list.length - 1; i++) { min = i; � [ 5 1 2 5 6 7 � [-5, 1, 2, 5, 6, 7 1 , 7 2 , 7 3 ] 7 7 ] for(int j = i + 1; j < list.length; j++) if( list[j] < list[min] ) – result of stable sort min = j; � R � Real world example: l ld l t temp = list[i]; li t[i] list[i] = list[min]; – sort a table in Wikipedia by one criteria, then another list[min] = temp; } } – sort by country then by major wins – sort by country, then by major wins } CS 307 Fundamentals of 19 CS 307 Fundamentals of 20 Computer Science Sorting and Searching Computer Science Sorting and Searching

  6. Selection Sort in Practice Generic Selection Sort 44 68 191 119 119 37 83 82 191 45 158 130 76 153 39 25 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; } 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 � B � Best case, worst case, average case Big O? t t Bi O? CS 307 Fundamentals of CS 307 Fundamentals of 21 22 Computer Science Sorting and Searching Computer Science Sorting and Searching Attendance Question 3 Insertion Sort � Another of the O(N^2) sorts Is selection sort always stable? A. Yes � The first item is sorted B. No � Compare the second item to the first – if smaller swap if smaller swap � Third item, compare to item next to it – need to swap need to swap – after swap compare again � A d � And so forth… f th CS 307 Fundamentals of 23 CS 307 Fundamentals of 24 Computer Science Sorting and Searching 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