Presentation on binary search, quick sort, merge sort and problems

Post on 07-Jan-2017

97 views 5 download

Transcript of Presentation on binary search, quick sort, merge sort and problems

By- Sumita Das

Presentation On Binary Search, Quick Sort, Merge Sort And

Problems

Created by Sumita Das

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

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

Initialize

FailureGet MidpointHigh < Low

< >Compare

Adjust High Adjust Low

Success=

Created by Sumita Das

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

•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

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

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

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

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

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

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

Example : Binary Searching

14?

14?

Created by Sumita Das

Binary Searching

14

Created by Sumita Das

Example : Quick sort

Created by Sumita Das

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

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

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

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

References

[1]Introduction to Algorithms 2nd ,Cormen, Leiserson, Rivest and Stein, The MIT Press, 2001.

Thank you….

Created by Sumita Das