IAT 800 Binary Search Sorting Nov 10 Fall
IAT 800 Binary Search Sorting Nov 10, Fall 2009 IAT 800 1
Search g Often want to search for an item in a list g In an unsorted list, must search linearly 12 g In 5 31 62 9 4 26 15 15 26 31 62 a sorted list… 4 Nov 10, Fall 2009 5 9 12 IAT 800 2
Binary Search g Start 4 with index pointer at start and end 5 g Compute 4 Nov 10, Fall 2009 5 9 12 15 26 31 62 index between two end pointers 9 12 15 26 IAT 800 31 62 3
Binary Search g Compare 4 g If 5 middle item to search item 9 12 15 26 31 62 search < mid: move end to mid -1 4 Nov 10, Fall 2009 5 9 12 15 26 IAT 800 31 62 4
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 ; } Nov 10, Fall 2009 IAT 800 5
Binary Search g Run Time – O( log(N) ) – Every iteration chops list in half Nov 10, Fall 2009 IAT 800 6
Sorting g Need a sorted list to do binary search g Numerous sort algorithms Nov 10, Fall 2009 IAT 800 7
The family of sorting methods Main sorting themes Comparison-based sorting Address-based sorting Proxmap Sort Transposition sorting Radix. Sort Bubble. Sort Insert and keep sorted Insertion sort Tree sort Nov 10, Fall 2009 Priority queue sorting Selection sort Heap sort IAT 800 Divide and conquer Quick. Sort Merge. Sort Diminishing increment sorting Shell. Sort 8
Bubble sorting] g Not [transposition a fast sort! end of one inner loop g 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 Nov 10, Fall 2009 IAT 800 5 3 2 4 3 5 2 4 3 2 5 4 3 2 4 5 2 3 4 5 9
Divide and conquer sorting Merge. Sort Nov 10, Fall 2009 Quick. Sort IAT 800 10
Quick. Sort sorting] [divide and conquer g As its name implies, Quick. Sort is the fastest known sorting algorithm in practice g Its average running time is O(n log n) g 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 Quick. Sort (S 1) followed by v followed by Quick. Sort (S 2) Nov 10, Fall 2009 IAT 800 11
Quick. Sort 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 Nov 10, Fall 2009 2 3 4 5 9 IAT 800 10 12 15 12
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 swap. Pos Step along the array, swapping small elements into swap. Pos 10 4 11 25 5 3 9 15 12 swap. Pos Nov 10, Fall 2009 IAT 800 13
Partitioning example (2) 10 4 5 25 11 3 9 15 12 11 25 9 15 12 swap. Pos 10 4 5 3 9 25 11 15 12 swap. Pos 9 4 5 3 10 25 11 15 12 swap. Pos-1 Nov 10, Fall 2009 IAT 800 14
Pseudocode for Quicksort procedure quicksort(array, left, right) if right > left select a pivot index (e. g. pivot. Idx = left) pivot. Idx. New = partition(array, left, right, pivot. Idx) quicksort(array, left, pivot. Idx. New - 1) quicksort(array, pivot. Idx. New + 1, right) Nov 10, Fall 2009 IAT 800 15
Pseudo code for partitioning pivot. Idx = middle of array a; swap a[pivot. Idx] with a[first]; // Move the pivot out of the way swap. Pos = first + 1; for( i = swap. Pos +1; i <= last ; i++ ) if (a[i] < a[first]) { swap a[swap. Pos] with a[i]; swap. Pos++ ; } // Now move the pivot back to its rightful place swap a[first] with a[swap. Pos-1]; return swap. Pos-1; // Pivot position Nov 10, Fall 2009 IAT 800 16
- Slides: 16