Lec4 - Design and Analysis of Algorithms
Lec4 - Design and Analysis of Algorithms
Algorithms Design
Techniques
1- Divide And
Conquer
1
Divide And Conquer
Divide And conquer is an important algorithm
design paradigm.
It works by recursively breaking down a
problems.
recursively.
a problem of size n
instanc(
)e
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Binary search
Merge sort
Quick sort
Maximum and
Minimum
5
Merge sort.
Split array A[0..n-1] into about equal halves and make copies of
arrays:
compare the first elements in the remaining unprocessed
:B 2 3 8 9 :C 1 4 5 7
:A 1 2 3 4 5 7 8 9
7
Algorithm 1: mergesort(left, right)
1. If(left<right)
2. mid=(left + right)/2 //divide problem
into sub-problems
3. mergesort(left, mid) //solve first
sub-problem
4. mergesort(mid+1,right) //solve second
sub-problem
5. merge(left, mid, right) // combine the
solutions
6. Endif
8
Algorithm 2: merge (left, mid, right)
1. i=j=left, k=mid+1
2. While j≤ mid and k ≤ right
3. If(a[j] <a[k])
4. temp[i]=a[j] //Increment i and j
5. Else
6. temp[i]=a[k] //Increment i and k
7. Endif
8. Endwhile
9. For i= left to k
10. a[i]= temp[i]
11.Endfor
9
Time complexity for merge sort
We analyze the worst case running time of merge sort on n
takes O(n).
10
Time complexity for merge sort
We add a function that is O(n) and a function that is O(1) .
11
Mergesort(contd.)
:Divide
8 3 2 9 7 1 5 4
:Merge
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
12
Quick sort.
Quicksort can perform quite fast, on average about
14
2. amid=a[(left + right/2)]
Algorithm 3: Quicksort(a, left, right)
//pivot
3. While newleft ≤ newright
//partition
4. While (a[newleft] < amid and newleft < right
//loop 1
5. Increment newleft
6. Endwhile
7. While (amid < a[newright] and new right > left)
//loop2
8. Decrement newright
9. Endwhile
10. If newleft ≤ newright , then
11. swap(a[newleft] , a[newright] )
12. Increment newleft and Decrement newright
13. End if
14. Endwhile /
/partition ends
15. If left<newright, then
16. call Quicksort(a, left, newright) // sort
left sub-array
15
17. Endif
Example: Quick sort
We have array with 12 elements.
6 3 1 9 7 4 4 6 9 2 8 5
5 5 5 0 5 5 0 0 5 5 5 5
left=1, right=12, newleft=left,
newright =right
mid=newleft + newright/2 =6,
a[mid]=45
Left sub-array is 1 to 5 and right
sub-array is 7 to 12
16
Example: Quick sort
Loop 1 scan left sub-array from left to
right as the element 65 is greater than
(45) , 65 is to shifted to right sub-array.
The newleft =1
Loop2 scans right sub-array from right to
left as the element 25 is less than (45).
The newright =10. the element in newleft
and newright are interchanged.
6 3 1 9 7 4 4 6 9 25 85 55 Newlef Newrig
Swap(65,25)
5 5 5 0 5 5 0 0 5 t ht
1 10
17
Example: Quick sort
2 3 1 9 7 4 4 6 9 65 85 55 Newlef Newrig
5 5 5 0 5 5 0 0 5 t ht
4 7
2 3 1 4 7 4 9 6 9 65 85 55 Newlef Newrig
5 5 5 0 5 5 0 0 5 t ht
5 6
2 3 1 4 4 7 9 6 9 65 85 55 Newlef Newrig
5 5 5 0 5 5 0 0 5 t ht
6 5
21
Exercise
1. Proof the average case run time for quick
sort.
2. Implement algorithm3 using Java
programming language.
22