presentation on binary search, quick sort, merge sort and problems
TRANSCRIPT
![Page 1: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/1.jpg)
By- Sumita Das
Presentation On Binary Search, Quick Sort, Merge Sort And
Problems
Created by Sumita Das
![Page 2: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/2.jpg)
Binary Search• If we have an ordered list and we know
how many things are in the list (i.e., number of records in a file), we can use a different strategy.
• The binary search gets its name because the algorithm continually divides the list into two parts.
Created by Sumita Das
![Page 3: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/3.jpg)
How a Binary Search Works
Always look at the center value. Each time you get to discard half of the remaining list.
Created by Sumita Das
![Page 4: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/4.jpg)
Initialize
FailureGet MidpointHigh < Low
< >Compare
Adjust High Adjust Low
Success=
Created by Sumita Das
![Page 5: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/5.jpg)
Algorithm:
Low= 1; High = n; while Low < High { m = floor( (Low+High)/2 ); if k <= A[m]
then High = m - 1 else Low = m + 1
} if A[Low] = k then j = Low else j = 0
Created by Sumita Das
![Page 6: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/6.jpg)
•Similar to merge sort - divide-and-conquer recursive algorithm
•One of the fastest sorting algorithms •Average running time O(NlogN)
•Worst-case running time O(N2)
Quick sort
Created by Sumita Das
![Page 7: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/7.jpg)
Basic idea
1.Pick one element in the array, which will be the pivot. 2.Make one pass through the array, called a partition step, re-arranging the entries so that:
•the pivot is in its proper place. •entries smaller than the pivot are to the left of the pivot. •entries larger than the pivot are to its right. Created by Sumita Das
![Page 8: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/8.jpg)
3.Recursively apply quick sort to the part of the array that is to the left of the pivot, and to the right part of the array.
Note: Here we don't have the merge step, at the end all the elements are in the proper order.
Created by Sumita Das
![Page 9: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/9.jpg)
Algorithm:• Input: Array E and indices first, and last, s.t.
elements E[i] are defined for first i last• Ouput: E[first], …, E[last] is a sorted
rearrangement of the array• Void quickSort(Element[] E, int first, int last)
if (first < last)Element pivotElement = E[first];int splitPoint = partition(E, pivotElement,
first, last);quickSort (E, first, splitPoint –1 );quickSort (E, splitPoint +1, last );
return;Created by Sumita Das
![Page 10: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/10.jpg)
Merge Sort• Uses recursion. Slightly faster than heap,
but uses twice as much memory from the 2nd array.
• Sometimes called “divide and conquer” sort.
• Works by recursively splitting an array into two equal halves, sorting the items, then re-merging them back into a new array.
Created by Sumita Das
![Page 11: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/11.jpg)
Algorithm:
• Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ]
• sort arrays B and C
• Merge sorted arrays B and C into array A
Created by Sumita Das
![Page 12: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/12.jpg)
Using Divide and Conquer: Merge Sort
• Merge Sort Strategy
Sorted
Merge
Sorted Sorted
Sort recursively by Mergesort
Sort recursively by Mergesort
first last
(first last)2
Created by Sumita Das
![Page 13: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/13.jpg)
Example : Binary Searching
14?
14?
Created by Sumita Das
![Page 14: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/14.jpg)
Binary Searching
14
Created by Sumita Das
![Page 15: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/15.jpg)
Example : Quick sort
Created by Sumita Das
![Page 16: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/16.jpg)
Example : Merge Sort– n Power of 2
1 2 3 4 5 6 7 8
q = 462317425
1 2 3 4
74255 6 7 8
6231
1 2
253 4
745 6
317 8
62
1
52
23
44
7 16
37
28
65
Divide
![Page 17: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/17.jpg)
Example – n Power of 2
1
52
23
44
7 16
37
28
65
1 2 3 4 5 6 7 8
76543221
1 2 3 4
75425 6 7 8
6321
1 2
523 4
745 6
317 8
62
ConquerandMerge
![Page 18: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/18.jpg)
Example – n Not a Power of 262537416274
1 2 3 4 5 6 7 8 9 10 11
q = 6
4162741 2 3 4 5 6
625377 8 9 10 11
q = 9q = 3
2741 2 3
4164 5 6
5377 8 9
6210 11
741 2
23
164 5
46
377 8
59
210
611
41
72
64
15
77
38
Divide
![Page 19: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/19.jpg)
Example – n Not a Power of 277665443221
1 2 3 4 5 6 7 8 9 10 11
7644211 2 3 4 5 6
765327 8 9 10 11
7421 2 3
6414 5 6
7537 8 9
6210 11
23
46
59
210
611
41
72
64
15
77
38
741 2
614 5
737 8
ConquerandMerge
![Page 20: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/20.jpg)
References
[1]Introduction to Algorithms 2nd ,Cormen, Leiserson, Rivest and Stein, The MIT Press, 2001.
![Page 21: Presentation on binary search, quick sort, merge sort and problems](https://reader035.vdocument.in/reader035/viewer/2022070516/586fd8e31a28ab18428b5765/html5/thumbnails/21.jpg)
Thank you….
Created by Sumita Das