advertisement

IAT 265 Binary Search Sorting June 23, 2015 IAT 265 1 Search Frequently wish to organize data to support search – Eg. Search for single item – Eg. Search for all items between 3 and 7 June 23, 2015 IAT 265 2 Search Often want to search for an item in a list In an unsorted list, must search linearly 12 In 5 31 62 9 4 26 15 15 26 31 62 a sorted list… 4 June 23, 2015 5 9 12 IAT 265 3 Binary Search Start 4 with index pointer at start and end 5 Compute 4 June 23, 2015 5 9 12 15 26 31 62 index between two end pointers 9 12 15 26 IAT 265 31 62 4 Binary Search Compare 4 If 5 middle item to search item 9 12 15 26 31 62 search < mid: move end to mid -1 4 June 23, 2015 5 9 12 15 26 IAT 265 31 62 5 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 ; } June 23, 2015 IAT 265 6 Binary Search Run Time – O( log(N) ) – Every iteration chops list in half June 23, 2015 IAT 265 7 Sorting Need a sorted list to do binary search Numerous sort algorithms June 23, 2015 IAT 265 8 The family of sorting methods Main sorting themes Address-based sorting Comparison-based sorting Proxmap Sort Transposition sorting RadixSort BubbleSort Insert and keep sorted Insertion sort June 23, 2015 Tree sort Priority queue sorting Selection sort Heap sort IAT 265 Divide and conquer QuickSort MergeSort Diminishing increment sorting ShellSort 9 Bubble sort Not [transposition sorting] a fast sort! end of one inner loop 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; } } } 5 ‘bubbled’ to the correct position remaining elements put in place June 23, 2015 IAT 265 5 3 2 4 3 5 2 4 3 2 5 4 3 2 4 5 2 3 4 5 10 Divide and conquer sorting MergeSort June 23, 2015 QuickSort IAT 265 11 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 log n) 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) June 23, 2015 IAT 265 12 QuickSort example 5 1 4 2 10 3 9 15 12 Pick the middle element as the pivot, i.e., 10 Partition into the two subsets below 5 1 4 2 3 9 4 5 9 15 12 Sort the subsets 1 2 3 12 15 Recombine with the pivot 1 June 23, 2015 2 3 4 5 9 IAT 265 10 12 15 13 Partitioning example 5 11 4 25 10 3 9 15 12 Pick the middle element as the pivot, i.e., 10 Move the pivot out of the way by swapping it with the first element 10 11 4 25 5 3 9 15 12 swapPos Step along the array, swapping small elements into swapPos 10 4 11 25 5 3 9 15 12 swapPos June 23, 2015 IAT 265 14 10 4 11 25 5 3 9 15 12 3 9 15 12 25 9 15 12 swapPos 10 4 5 25 11 swapPos 10 4 5 3 11 swapPos 10 4 5 3 9 25 11 15 12 swapPos 9 4 5 3 10 25 11 15 12 swapPos-1 June 23, 2015 IAT 265 15 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) June 23, 2015 IAT 265 16 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 June 23, 2015 IAT 265 17 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 June 23, 2015 IAT 265 18