Download presentation

Presentation is loading. Please wait.

Published byBrian Chafin Modified over 3 years ago

1
Jun 23, 2014IAT 2651 Binary Search Sorting

2
Jun 23, 2014IAT 2652 Search Frequently wish to organize data to support search –Eg. Search for single item –Eg. Search for all items between 3 and 7

3
Jun 23, 2014IAT 2653 Search Often want to search for an item in a list In an unsorted list, must search linearly In a sorted list… 1253162942615 4591215263162

4
Jun 23, 2014IAT 2654 Binary Search Start with index pointer at start and end Compute index between two end pointers 45912152631624591215263162

5
Jun 23, 2014IAT 2655 Binary Search Compare middle item to search item If search < mid: move end to mid -1 45912152631624591215263162

6
Jun 23, 2014IAT 2656 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 ; mid = (start + end)/2 ; }

7
Jun 23, 2014IAT 2657 Binary Search Run Time –O( log(N) ) –Every iteration chops list in half

8
Jun 23, 2014IAT 2658 Sorting Need a sorted list to do binary search Numerous sort algorithms

9
Jun 23, 2014IAT 2659 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

10
Jun 23, 2014IAT 26510 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
*

11
Jun 23, 2014IAT 26511 Divide and conquer sorting MergeSort QuickSort

12
Jun 23, 2014IAT 26512 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 )

13
Jun 23, 2014IAT 26513 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

14
Jun 23, 2014IAT 26514 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

15
Jun 23, 2014IAT 26515 10452511391512 swapPos 10453112591512 swapPos 10453925111512 swapPos 94531025111512 swapPos-1 10411255391512 swapPos

16
Jun 23, 2014IAT 26516 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)

17
Jun 23, 2014IAT 26517 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

18
Jun 23, 2014IAT 26518 Java Sort and binary search provided on Arrays –sort() ints, floats –sort( Object[] a, Comparator c ) you supply the Comparator object, which Contains a function to compare 2 objects –binarySearch() ints, floats…. Search Objects with Comparator object

Similar presentations

OK

Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.

Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.

© 2018 SlidePlayer.com Inc.

All rights reserved.

By using this website, you agree with our use of **cookies** to functioning of the site. More info in our Privacy Policy and Google Privacy & Terms.

Ads by Google

Ppt on astronomy and astrophysics journals Ppt on information security threats Mp ppt online registration 2012 Ppt on indian politics in present Ppt on seasons of the year Export pdf to ppt online conversion Flat panel display ppt on tv Ppt on blood stain pattern analysis problems Ppt on village life in hindi Ppt on table tennis game