Download presentation

Presentation is loading. Please wait.

Published byTrey Warters Modified over 2 years ago

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

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

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

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: 5324352432543245 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 80010 Divide and conquer sorting MergeSort QuickSort

11
Nov 10, Fall 2009IAT 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 10, Fall 2009IAT 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 10, Fall 2009IAT 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 10, Fall 2009IAT 80014 Partitioning example (2) 10452511391512 swapPos 10453112591512 swapPos 10453925111512 swapPos 94531025111512 swapPos-1

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

Similar presentations

OK

Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.

Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google