Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nov 10, Fall 2009IAT 8001 Binary Search Sorting. Nov 10, Fall 2009IAT 8002 Search  Often want to search for an item in a list  In an unsorted list,

Similar presentations


Presentation on theme: "Nov 10, Fall 2009IAT 8001 Binary Search Sorting. Nov 10, Fall 2009IAT 8002 Search  Often want to search for an item in a list  In an unsorted list,"— Presentation transcript:

1 Nov 10, Fall 2009IAT 8001 Binary Search Sorting

2 Nov 10, Fall 2009IAT 8002 Search  Often want to search for an item in a list  In an unsorted list, must search linearly  In a sorted list…

3 Nov 10, Fall 2009IAT 8003 Binary Search  Start with index pointer at start and end  Compute index between two end pointers

4 Nov 10, Fall 2009IAT 8004 Binary Search  Compare middle item to search item  If search < mid: move end to mid

5 Nov 10, Fall 2009IAT 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 ; }

6 Nov 10, Fall 2009IAT 8006 Binary Search  Run Time –O( log(N) ) –Every iteration chops list in half

7 Nov 10, Fall 2009IAT 8007 Sorting  Need a sorted list to do binary search  Numerous sort algorithms

8 Nov 10, Fall 2009IAT 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

9 Nov 10, Fall 2009IAT 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 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

10 Nov 10, Fall 2009IAT Divide and conquer sorting MergeSort QuickSort

11 Nov 10, Fall 2009IAT 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 )

12 Nov 10, Fall 2009IAT QuickSort example Pick the middle element as the pivot, i.e., Partition into the two subsets below Sort the subsets Recombine with the pivot

13 Nov 10, Fall 2009IAT 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

14 Nov 10, Fall 2009IAT Partitioning example (2) swapPos swapPos swapPos swapPos-1

15 Nov 10, Fall 2009IAT 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)

16 Nov 10, Fall 2009IAT 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


Download ppt "Nov 10, Fall 2009IAT 8001 Binary Search Sorting. Nov 10, Fall 2009IAT 8002 Search  Often want to search for an item in a list  In an unsorted list,"

Similar presentations


Ads by Google