CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
Merge sort and Quick sort
1.
Design and Analysis of Algorithm
VADODARA INSTITUTE OF ENGINEERING
ACTIVE LEARNING ASSIGNEMENT
TOPIC: QUICK AND MERGE SORT
COMPUTER ENGINEERING(CE-1)
Presented By:
Heta Patel 150800107041
Maitree Patel 150800107048
Riddhi Patel 150800107053
3.
Quicksort
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
Quicksort two sub-arrays
Return results
4. Partitioning Array
Given a pivot, partition the elements of the array such
that the resulting array consists of:
One sub-array that contains elements >= pivot
Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements
below/above pivot.
6.
partition(A,low,high)
(1) [initialize]
Set pivot =A[low]
Set i=low+1
Set j=high
(2) Repeat step 3,4,5
While i<=j
(3)While A[i]<pivot
Then i++
[end of while]
(4)While A[j]>pivot then
j- -
[end while]
40 20 10 80 60 50 7 30 100
7.
(5)If i<j then
Swap A[i] and A[j]
[end if]
[end of main while]
(6)Swap A[j] and pivot
(7)Return j
(8)exit
40 20 10 80 60 50 7 30 100
8. Pick Pivot Element
There are a number of ways to pick the pivot element.
In this example, we will use the first element in the
array:
40 20 10 80 60 50 7 30 100
9. Example
We are given array of n integers to sort:
40 20 10 80 60 50 7 30 100
36.
Time Complexity of quick
sort
T(N) = T(i) + T(N - i -1) + cN
The time to sort the file is equal to
the time to sort the left partition with i elements, plus
the time to sort the right partition with N-i-
1 elements, plus
the time to build the partitions
38.
t(n) = 2t(n/2) + cn
for best case time complexity is: Θ(nlogn)
For worst case time complexity is: Θ(n²)
For average case time complexity is:
Θ(nlogn)
39.
Advantages:
One of the fastest algorithms on average.
Does not need additional memory (the sorting takes
place in the array - this is called in-place processing).
Compare with mergesort: mergesort needs additional
memory for merging.
40. Merge sort
ALGORITHM
MERGE-SORT (A, L, H)
1. IF L< H
2. THEN m= [(L+ H)/2]
3. MERGE (A, L, M)
4. MERGE (A, M + 1, H)
5. MERGE (A, L, H, M)
100.
How many times are these steps
executed?
For this, look at the tree below - for each level
from top to bottom Level 2 calls merge method
on 2 sub-arrays of length n/2 each.
The complexity here is 2 * (cn/2) = cn Level 3
calls merge method on 4 sub-arrays of length
n/4 each. The complexity here is 4 * (cn/4) = cn
and so on ...
Now, the height of this tree is (logn + 1) for a
given n. Thus the overall complexity is (logn +
1)*(cn). That is O(nlogn) for the merge sort
algorithm.
101.
t(n) = 2t(n/2) + Cn
for best case time complexity is: Θ(n log(n))
For worst case time complexity is: Θ(n log(n))
For average case time complexity is: Θ(n
log(n))
102.
The advantages to merge sort is it is always
fast. Even in its worst case its runtime is
O(nlogn).
Disadvantages of Merge sort are that it is not
in place so merge sort uses a lot of memory.
It uses extra space