Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 Nov 21, Fall 2006IAT 8001 Binary Search Sorting

2 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… 12531629426154591215263162

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

4 Nov 21, Fall 2006IAT 8004 Binary Search  Compare middle item to search item  If search < mid: move end to mid -1 45912152631624591215263162

5 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 ; }

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

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

8 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

9 Nov 21, Fall 2006IAT 8009 Bubble sort [transposition sorting]  Not a fast sort!  Code is small: 5324352432543245 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

10 Nov 21, Fall 2006IAT 80010 Divide and conquer sorting MergeSort QuickSort

11 Nov 21, Fall 2006IAT 80011 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 21, Fall 2006IAT 80012 QuickSort example 514210391512 Pick the middle element as the pivot, i.e., 10 5142391512 Partition into the two subsets below Sort the subsets 1234591215 Recombine with the pivot 123459101215

13 Nov 21, Fall 2006IAT 80013 Partitioning example 51142510391512 Pick the middle element as the pivot, i.e., 10 Move the pivot out of the way by swapping it with the first element 10114255391512 swapPos Step along the array, swapping small elements into swapPos 10411255391512 swapPos

14 Nov 21, Fall 2006IAT 80014 Partitioning example (2) 10452511391512 swapPos 10453112591512 swapPos 10453925111512 swapPos

15 Nov 21, Fall 2006IAT 80015 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


Download ppt "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,"

Similar presentations


Ads by Google