180 likes | 388 Views
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
E N D
IAT 265 Binary Search Sorting IAT 265
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
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 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
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
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
Binary Search • Run Time • O( log(N) ) • Every iteration chops list in half IAT 265
Sorting • Need a sorted list to do binary search • Numerous sort algorithms IAT 265
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
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
Divide and conquer sorting MergeSort QuickSort IAT 265
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
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
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
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
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
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
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