0% found this document useful (0 votes)
27 views7 pages

Divide and Conquer Technique Sorting

The divide-and-conquer strategy solves problems by (1) breaking the problem into smaller subproblems, (2) recursively solving the subproblems, and (3) combining the solutions to the subproblems. Examples that use this strategy are binary search, merge sort, and quick sort. The algorithms provided give the steps to solve problems using binary search and merge sort.

Uploaded by

Harsh Bajaj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

Divide and Conquer Technique Sorting

The divide-and-conquer strategy solves problems by (1) breaking the problem into smaller subproblems, (2) recursively solving the subproblems, and (3) combining the solutions to the subproblems. Examples that use this strategy are binary search, merge sort, and quick sort. The algorithms provided give the steps to solve problems using binary search and merge sort.

Uploaded by

Harsh Bajaj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Divide and Conquer

• The divide-and-conquer strategy solves a


problem by:
– 1. Breaking it into subproblems that are
themselves smaller instances of the same
type of problem
– 2. Recursively solving these subproblems
– 3. Appropriately combining their answers
• Examples : Binary Search, Merge Sort,
Quick Sort
• Algorithm for Binary search
• Algorithm for Merge sort
Algorithm for Binary search
Binary (a[ ], item, low, high)
1. Mid (variable)
2. If (low > high)
1. Return -1
3. End if
4. Mid = (low+high)/2
5. if(item == a[mid])
1. Return mid
6. End if
7. If (item<a[mid])
1. Return binary(item , a, low, mid-1)
8. Else
1. Return binary(item , a, mid+1, high)
9. End if
Algorithm for Merge sort
• Divide the array into two halves.
• Recursively sort each half.
• Merge two halves to make sorted whole.
Algorithm for Mergesort
Algorithm Mergesort(A[ ] , low , high)
• Mid (variable)
• If(low < high)
• Mid = (low + high)/2
• Mergesort( a , low, mid)
• Mergesort(a, mid+1, high)
• Simplemerge ( a, low , mid, high )
• End if
• Else
– return
Algorithm for Merge
Algorithm SimpleMerge (A, low , mid ,high)
1. i = k = low
2. j = mid + 1
3. While ( i < = mid and j <= high)
4. If (A[i] < A[j] ) then
1. C[K++] = A[i++]
5. Else
1. C[K++] = A[j++]

6. End while
• While (i <= mid )
• C[K] = A[i]
• K = K+1
• i = i+1
• End while
• While (j <= high)
• C[K] = A[j]
• K = K+1
• j = j+1
• End while
• Repeat for i = low to high
• A[i] = C[i]
• End for

You might also like