1 / 18

IAT 265

IAT 265. Binary Search Sorting. Search. Frequently wish to organize data to support search Eg. Search for single item Eg. Search for all items between 3 and 7. 12. 4. 5. 5. 31. 9. 62. 12. 9. 15. 26. 4. 26. 31. 62. 15. Search. Often want to search for an item in a list

clyde
Télécharger la présentation

IAT 265

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. IAT 265 Binary Search Sorting IAT 265

  2. Search • Frequently wish to organize data to support search • Eg. Search for single item • Eg. Search for all items between 3 and 7 IAT 265

  3. 12 4 5 5 31 9 62 12 9 15 26 4 26 31 62 15 Search • Often want to search for an item in a list • In an unsorted list, must search linearly • In a sorted list… IAT 265

  4. 4 4 5 5 9 9 12 12 15 15 26 26 31 31 62 62 Binary Search • Start with index pointer at start and end • Compute index between two end pointers IAT 265

  5. 4 4 5 5 9 9 12 12 15 15 26 26 31 31 62 62 Binary Search • Compare middle item to search item • If search < mid: move end to mid -1 IAT 265

  6. Binary Search int[] Arr = new int[8] ; <populate array> int search = 4 ; int start = 0, end = Arr.length, mid ; mid = (start + end)/2 ; while( start <=end ) { if(search == Arr[mid] ) SUCCESS ; if( search < Arr[mid] ) end = mid – 1 ; else start = mid + 1 ; mid = (start + end)/2 ; } IAT 265

  7. Binary Search • Run Time • O( log(N) ) • Every iteration chops list in half IAT 265

  8. Sorting • Need a sorted list to do binary search • Numerous sort algorithms IAT 265

  9. The family of sorting methods Main sorting themes Address- -based sorting Comparison-based sorting Proxmap Sort RadixSort Transposition sorting BubbleSort Diminishing increment sorting Insert and keep sorted Priority queue sorting Divide and conquer ShellSort Selection sort QuickSort MergeSort Insertion sort Tree sort Heap sort IAT 265

  10. end of one inner loop 5 3 2 4 3 5 2 4 3 2 5 4 3 2 4 5 5 ‘bubbled’ to the correct position 2 3 4 5 remaining elements put in place Bubble sort [transposition sorting] • Not a fast sort! • Code is small: for (int i=arr.length; i>0; i--) { for (int j=1; j<i; j++) { if (arr[j-1] > arr[j]) { temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } IAT 265

  11. Divide and conquer sorting MergeSort QuickSort IAT 265

  12. QuickSort [divide and conquer sorting] • As its name implies, QuickSort is the fastest known sorting algorithm in practice • Its average running time is O(n logn) • The idea is as follows: 1. If the number of elements to be sorted is 0 or 1, then return 2. Pick any element, v (this is called the pivot) 3. Partition the other elements into two disjoint sets, S1 of elements  v, and S2 of elements > v 4. Return QuickSort (S1) followed by v followed by QuickSort (S2) IAT 265

  13. Partition into the two subsets below 5 1 4 2 3 9 15 12 Sort the subsets 1 2 3 4 5 9 12 15 Recombine with the pivot 1 2 3 4 5 9 10 12 15 QuickSort example 5 1 4 2 10 3 9 15 12 Pick the middle element as the pivot, i.e., 10 IAT 265

  14. Move the pivot out of the way by swapping it with the first element 10 11 4 25 5 3 9 15 12 swapPos swapPos Step along the array, swapping small elements into swapPos 10 4 11 25 5 3 9 15 12 Partitioning example 5 11 4 25 10 3 9 15 12 Pick the middle element as the pivot, i.e., 10 IAT 265

  15. 10 4 11 25 5 3 9 15 12 10 4 5 3 11 25 9 15 12 swapPos swapPos swapPos-1 swapPos swapPos 10 4 5 3 9 25 11 15 12 10 4 5 25 11 3 9 15 12 9 4 5 3 10 25 11 15 12 IAT 265

  16. Pseudocode for Quicksort procedure quicksort(array, left, right) if right > left select a pivot index (e.g. pivotIdx = left) pivotIdxNew = partition(array, left, right, pivotIdx) quicksort(array, left, pivotIdxNew - 1) quicksort(array, pivotIdxNew + 1, right) IAT 265

  17. Pseudo code for partitioning pivotIdx = middle of array a;swap a[pivotIdx] with a[first]; // Move the pivot out of the way swapPos = first + 1; for( i = swapPos +1; i <= last ; i++ ) if (a[i] < a[first]) { swap a[swapPos] with a[i]; swapPos++ ; } // Now move the pivot back to its rightful place swap a[first] with a[swapPos-1]; return swapPos-1; // Pivot position IAT 265

  18. Java • Sort and binary search provided on Arrays • sort() ints, floats • sort( Object[] a, Comparator c ) • you supply the Comparator object, which Contains a function to compare 2 objects • binarySearch() • ints, floats…. • Search Objects with Comparator object IAT 265

More Related