160 likes | 170 Views
Learn about binary search and various sorting algorithms with examples and pseudocode for quick sorting. Understand the importance of sorted lists for efficient search operations.
E N D
IAT 800 Binary Search Sorting IAT 800
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 800
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 800
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 800
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 ; } IAT 800
Binary Search • Run Time • O( log(N) ) • Every iteration chops list in half IAT 800
Sorting • Need a sorted list to do binary search • Numerous sort algorithms IAT 800
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 800
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 800
Divide and conquer sorting MergeSort QuickSort IAT 800
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 800
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 800
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 800
10 4 5 3 11 25 9 15 12 swapPos swapPos-1 swapPos swapPos 10 4 5 3 9 25 11 15 12 Partitioning example (2) 10 4 5 25 11 3 9 15 12 9 4 5 3 10 25 11 15 12 IAT 800
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 800
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 800