Nov 21, Fall 2006IAT 8001 Binary Search Sorting
Nov 21, Fall 2006IAT 8002 Search Often want to search for an item in a list In an unsorted list, must search linearly In a sorted list…
Nov 21, Fall 2006IAT 8003 Binary Search Start with index pointer at start and end Compute index between two end pointers
Nov 21, Fall 2006IAT 8004 Binary Search Compare middle item to search item If search < mid: move end to mid
Nov 21, Fall 2006IAT 8005 Binary Search int[] Arr = new int[8] ; 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 ; }
Nov 21, Fall 2006IAT 8006 Binary Search Run Time –O( log(N) ) –Every iteration chops list in half
Nov 21, Fall 2006IAT 8007 Sorting Need a sorted list to do binary search Numerous sort algorithms
Nov 21, Fall 2006IAT 8008 Selection sort The family of sorting methods Main sorting themes Comparison-based sorting Transposition sorting BubbleSort Insert and keep sorted Divide and conquer Insertion sort Tree sort Heap sort QuickSortMergeSort Proxmap Sort RadixSort ShellSort Diminishing increment sorting Address- -based sorting Priority queue sorting
Nov 21, Fall 2006IAT 8009 Bubble sort [transposition sorting] Not a fast sort! Code is small: end of one inner loop 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 2345 remaining elements put in place
Nov 21, Fall 2006IAT Divide and conquer sorting MergeSort QuickSort
Nov 21, Fall 2006IAT 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, S 1 of elements v, and S 2 of elements > v 4.Return QuickSort (S 1 ) followed by v followed by QuickSort (S 2 )
Nov 21, Fall 2006IAT QuickSort example Pick the middle element as the pivot, i.e., Partition into the two subsets below Sort the subsets Recombine with the pivot
Nov 21, Fall 2006IAT Partitioning example Pick the middle element as the pivot, i.e., 10 Move the pivot out of the way by swapping it with the first element swapPos Step along the array, swapping small elements into swapPos swapPos
Nov 21, Fall 2006IAT Partitioning example (2) swapPos swapPos swapPos
Nov 21, Fall 2006IAT Pseudo code for partitioning pivotPos = middle of array a; swap a[pivotPos] with a[first]; // Move the pivot out of the way swapPos = first + 1; for each element in the array from swapPos to last do: // If the current element is smaller than pivot we // move it towards start of array if (a[currentElement] < a[first]): swap a[swapPos] with a[currentElement]; increment swapPos by 1; // Now move the pivot back to its rightful place swap a[first] with a[swapPos-1]; return swapPos-1; // Pivot position