DAA03 Quick Sort Stressen
DAA03 Quick Sort Stressen
DAA03 Quick Sort Stressen
Algorithms (CS205)
Lecture 3
Divide & Conquer:
Merge sort
Divide And Conquer: Control Abstraction
01 02 03 04 05
1. The divide and 3. The divide and 4. The divide and 6. Brute force 7. The divide and
conquer algorithm conquer divides the conquer strategy technique and divide conquer technique is
solves all the problem into sub- makes use of cache and conquer quite faster than
problems recursively, problems which can memory because of techniques are other algorithms.
so any problem, run parallelly at the the repeated use of similar but divide and
which requires same time. Thus, this variables in conquer is more
recursion can make algorithm works on recursion. proficient than the
use of Divide and parallelism. This brute force method.
conquer. property of divide
and conquer is
extensively used in
the operating
system.
Disadvantages of Divide and Conquer Algorithm
01 02 03 04
1. The divide and conquer 2. The implementation of 3. Memory overuse is 4. The system may crash in
technique uses recursion. divide and conquer requires possible by an explicit stack. case the recursion is not
Recursion in turn leads to high memory management. performed properly.
lots of space complexity
because it makes use of the
stack.
Applications of Divide and Conquer
Defective
Quicksort Mergesort Binary search
chessboard
Finding the
maximum and Strassen’s matrix
minimum in an multiplication
array
Quick Sort Idea(DAC)
• Get pivot element, usually first element; find where will the pivot element be placed
• Two pointers P,Q
• P moves left to right until element greater than pivot is found or (ii) end of array is
found
• Q moves right to left until element less than or equal pivot is found
35 50 15 25 80 20 90
How does quick sort work?
• Pick: Select an element.
• Divide: Split the problem set, move smaller parts to
the left of the pivot and larger items to the right.
• Repeat and combine: Repeat the steps and
combine the arrays that have previously been
sorted.
• Partition algo:
Quicksort
Internal: All
elements needs to • Bubble, insertion, merge, quick,
be in main memory selection
before sorting
N
Ci , j = å ai ,k bk , j
Time analysis k =1
N N N
Thus T ( N ) = ååå c = cN 3 = O( N 3 )
i =1 j =1 k =1
Strassens’s Matrix
Multiplication
• Strassen showed that 2x2 matrix multiplication can
be accomplished in 7 multiplication and 18
additions or subtractions. .(2 =2 )
log 7
2
2.807
C11 = P1 + P4 - P5 + P7
= (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) * B22+
(A12 - A22) * (B21 + B22)
= A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 – A22 B11 -
A11 B22 -A12 B22 + A12 B21 + A12 B22 – A22 B21 – A22 B22
= A11 B11 + A12 B21
Strassen Algorithm
void matmul(int *A, int *B, int *R, int n) {
if (n == 1) {
(*R) += (*A) * (*B);
} else {
matmul(A, B, R, n/4);
matmul(A, B+(n/4), R+(n/4), n/4);
matmul(A+2*(n/4), B, R+2*(n/4), n/4);
matmul(A+2*(n/4), B+(n/4), R+3*(n/4), n/4);
matmul(A+(n/4), B+2*(n/4), R, n/4);
matmul(A+(n/4), B+3*(n/4), R+(n/4), n/4);
matmul(A+3*(n/4), B+2*(n/4), R+2*(n/4), n/4); Divide matrices in
sub-matrices and
matmul(A+3*(n/4), B+3*(n/4), R+3*(n/4), n/4);
recursively multiply
} sub-matrices
Time Analysis
Basic Matrix Multiplication
• Compute multiplication of square matrices
Method 2: Divide and conquer
• Divide sq matrix of size N to 4 matrices of size n/2
Time Complexity
• n2
Method 3: Stressmen Approach
Time Complexity
Trick to memorize