Chapter 2 : Divide & Conquer
Content to be cover.
• Introduction
• Binary Search Algorithm
• Merge Sort Using Divide & Conquer
• Quick Sort Using Divide & Conquer
• Strassen’s Matrix Multiplication
Content Covered : Introduction & Binary Search Algorithm
Introduction
• This chapter introduced and explains an algorithm design technique divide & conquer.
• Applied to large size problem.
• There are basically three tasks related to divide and conquer.
(i) D and C (P) : Divide and conquer
(ii) Small (p)
(iii) Combine
Chapter 2: Divide & Conquer
D and C (Binarysearch (i, j, item))
{
if (i = = j)
{
if (A [i] = = item)
return 1;
else
return 0;
}
else
{
mid = if (item = = A [mid])
return mid;
else if (item < A [mid])
D and C (Binarysearch (i, mid – 1, item))
else if (item > A [mid])
D and C (Binarysearch (mid + 1, j, item))
return;
}
}
Complexity of Binary Search : O(log2 n)
Divide & Conquer Binary Search Algorithm
Chapter 2: Divide & Conquer
Example of Binary Search Algorithm
Content Covered : Merge Sort Algorithm using divide & conquer
Algorithm for Merge Sort using divide & conquer:
D and C Mergesort (low, high)
{
if (low < high)
{
mid = (low + high) / 2
DandCMergesort (low, mid);
DandCMergesort (mid + 1, high);
Merge (low, mid, high)
}
}
Complexity = O (n log2 n)
Chapter 2: Divide & Conquer
Algorithm for Quick Sort Using Divide & Conquer
D and C Quicksort (i, j)
{
if (i < j) then divide the sequence into two subsequence
{
q = partition (i, j)
D and C Quicksort (i, q – 1)
D and C Quicksort (q + 1, j)
}
}
• Complexity of Quick Sort : O ( n log2 n )
Partition Algorithm :
1. i = 1, j = n
2. Compare A [i] and A [j]
3. if (A [i]  A [j])
{
j = j – 1 and go to step 2;
}
4. if (A [i] > A [j])
{
Interchange A [i] and A [j] and i = i + 1;
}
5. Compare A [i] and A [j]
6. if (A [i]  A [j])
{
i = i + 1 and go to step 5;
}
7. if (A [i] > A [j])
{
Interchange A [i] and A [j] and go to step 2;
}
Content Covered : Quick Sort Algorithm using divide & conquer
Chapter 2: Divide & Conquer
Example :
2 2 4 3 1
Solution :
Given array : 2 2 4 3 1
i = 1 j = 5
Now perform according to above algorithms
D and C Quicksort (i, j) i.e. D and C Quicksort (1, 5) Here i = 1 and j = 5
Since (i < j) i.e. (1 < 5) condition satisfied.
Divide this sequence into two subsequence, i.e. q = partition (i, j) = partition (1, 5)
Use partition algorithm and find out the pivot element q.
Then one sequence is (i to q – 1) and other sequence is (q + 1 to j).
Now apply partitioning algorithm. i = 1 and j = 5
Sort following array by using Divide and Conquer Quick sort algorithm.
Content Covered : Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer
Content Covered :Strassen’s Matrix Multiplication
Chapter 2: Divide & Conquer

Chapter 2 divide &amp; conquer

  • 1.
    Chapter 2 :Divide & Conquer Content to be cover. • Introduction • Binary Search Algorithm • Merge Sort Using Divide & Conquer • Quick Sort Using Divide & Conquer • Strassen’s Matrix Multiplication
  • 2.
    Content Covered :Introduction & Binary Search Algorithm Introduction • This chapter introduced and explains an algorithm design technique divide & conquer. • Applied to large size problem. • There are basically three tasks related to divide and conquer. (i) D and C (P) : Divide and conquer (ii) Small (p) (iii) Combine Chapter 2: Divide & Conquer
  • 3.
    D and C(Binarysearch (i, j, item)) { if (i = = j) { if (A [i] = = item) return 1; else return 0; } else { mid = if (item = = A [mid]) return mid; else if (item < A [mid]) D and C (Binarysearch (i, mid – 1, item)) else if (item > A [mid]) D and C (Binarysearch (mid + 1, j, item)) return; } } Complexity of Binary Search : O(log2 n) Divide & Conquer Binary Search Algorithm Chapter 2: Divide & Conquer
  • 4.
    Example of BinarySearch Algorithm
  • 5.
    Content Covered :Merge Sort Algorithm using divide & conquer Algorithm for Merge Sort using divide & conquer: D and C Mergesort (low, high) { if (low < high) { mid = (low + high) / 2 DandCMergesort (low, mid); DandCMergesort (mid + 1, high); Merge (low, mid, high) } } Complexity = O (n log2 n) Chapter 2: Divide & Conquer
  • 6.
    Algorithm for QuickSort Using Divide & Conquer D and C Quicksort (i, j) { if (i < j) then divide the sequence into two subsequence { q = partition (i, j) D and C Quicksort (i, q – 1) D and C Quicksort (q + 1, j) } } • Complexity of Quick Sort : O ( n log2 n ) Partition Algorithm : 1. i = 1, j = n 2. Compare A [i] and A [j] 3. if (A [i]  A [j]) { j = j – 1 and go to step 2; } 4. if (A [i] > A [j]) { Interchange A [i] and A [j] and i = i + 1; } 5. Compare A [i] and A [j] 6. if (A [i]  A [j]) { i = i + 1 and go to step 5; } 7. if (A [i] > A [j]) { Interchange A [i] and A [j] and go to step 2; } Content Covered : Quick Sort Algorithm using divide & conquer Chapter 2: Divide & Conquer
  • 7.
    Example : 2 24 3 1 Solution : Given array : 2 2 4 3 1 i = 1 j = 5 Now perform according to above algorithms D and C Quicksort (i, j) i.e. D and C Quicksort (1, 5) Here i = 1 and j = 5 Since (i < j) i.e. (1 < 5) condition satisfied. Divide this sequence into two subsequence, i.e. q = partition (i, j) = partition (1, 5) Use partition algorithm and find out the pivot element q. Then one sequence is (i to q – 1) and other sequence is (q + 1 to j). Now apply partitioning algorithm. i = 1 and j = 5 Sort following array by using Divide and Conquer Quick sort algorithm.
  • 9.
    Content Covered :Strassen’s Matrix Multiplication Chapter 2: Divide & Conquer
  • 10.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer
  • 11.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer
  • 12.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer
  • 13.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer
  • 14.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer
  • 15.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer
  • 16.
    Content Covered :Strassen’sMatrix Multiplication Chapter 2: Divide & Conquer