36

Divide & conqre

Embed Size (px)

Citation preview

Page 1: Divide & conqre
Page 2: Divide & conqre

Divide &

conquer

Page 3: Divide & conqre

DIVIDE AND CONQUER RULEDEFINITION:

The divide-and-conquer rule consists in breaking a problem into simpler sub-problems of the same type, next to solve these sub-problems, finally to merge the obtained results into a solution to the problem.

Page 4: Divide & conqre

Why we use it?

Divide and conquer technique is used for sorting the elements of array in some specific order.

Page 5: Divide & conqre

ALGORITHM:Dividing the main problem into smaller ones.These smaller sub-problems are divided further.Recombine them to achieve the main objective.

Page 6: Divide & conqre

ANALYSIS:Let’ see few analysis to confirm the usefulness of the divide and conquer technique.To sort the one major problem time is nlog(n)2To sort the halves approximate time is (n/2) 2+(n/2) 2To merge the two halves approximate time is n

So, for n=100, divide and conquer takes approximately:= (100/2) 2 + (100/2) 2 + 100= 2500 + 2500 + 100= 5100

Suppose that n is 100. Considering if we apply insertion sort algorithm on it then thetime taken will be approximately (100) 2 n = 10000. Now, if we apply divide and conquer technique on it. Then for first half approximate time will be (100/2) 2. Similarly for second half it will be (100/2) 2. The merging approximate time will be 100. So the whole operation of sorting using this divide and conquer technique in insertion sort will take around (100/2) 2 + (100/2)2+100 = 5100. Clearly the time spent (5100) after applying divide and conquer mechanism is significantly lesser thanthe previous time (10000).

Page 7: Divide & conqre

WORKING:

subproblem 2 of size n/2

subproblem 1 of size n/2

a solution to subproblem 1

a solution tothe original problem

a solution to subproblem 2

a problem of size n

Page 8: Divide & conqre

EXAMPLES:

Merge sortQuick sort

Page 9: Divide & conqre

EXAMPLE OF MERGE SORT

Page 10: Divide & conqre

5 10 3 8

Suppose we have an array of four elements

Page 11: Divide & conqre

5 10 3 8

5 10 3 8

Page 12: Divide & conqre

5 10 3 8

5 10 3 8

Page 13: Divide & conqre

5 10 3 8

5 10

3

8

Page 14: Divide & conqre

5 10 3 8

5 10

3

8

Page 15: Divide & conqre

5 10 3 8

5

10

3

8

Page 16: Divide & conqre

5 10 3 8

5

10

3

8

Page 17: Divide & conqre

5 10 3 8

5

10

3 8

Page 18: Divide & conqre

5 10 3 8

5 103 8

Un sorted

sorted

Page 19: Divide & conqre

EXAMPLE OF QUICK SORT

Page 20: Divide & conqre

5 10 3 8

Suppose we have an array of five elements

1

Page 21: Divide & conqre

5 10 3 8 1

pivot

Page 22: Divide & conqre

5 10 3 8 1

Page 23: Divide & conqre

5 10 3 8 1

Page 24: Divide & conqre

5

10 3 8

1

Page 25: Divide & conqre

510 3 81

Page 26: Divide & conqre

510 3 81

Page 27: Divide & conqre

5

10

3 81

Page 28: Divide & conqre

5

10

3 81

Page 29: Divide & conqre

5103 81

Page 30: Divide & conqre

5 103 81

pivot

Page 31: Divide & conqre

5 103 81

Page 32: Divide & conqre

5 103 81

Page 33: Divide & conqre

5

10

3 81

Page 34: Divide & conqre

5

10

3 81

Page 35: Divide & conqre

5 103 81

Page 36: Divide & conqre

5 103 81

sorted