0% found this document useful (0 votes)
6 views12 pages

Adv Sorting Algo

Uploaded by

riyabhart02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Adv Sorting Algo

Uploaded by

riyabhart02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CS-2001 Data Structures

Advanced Sorting
Algorithms
Merge Sort
• Merge Sort Algorithm is a Divide &
Conquer algorithm. It divides input array in
two halves, calls itself for the two
halves(recursively) and then merges the two
sorted halves. A separate merge() function is
used for merging two halves.
• Time Complexity: O(n*log(n))
• Space Complexity: n
Merge Sort - Continue
There are 3 Phases in the Merge Sort Algorithm
1. Division Phase : Divide the array into 2 halves by finding the
mid of the array.
1. Mid (m) = (left + right)/ 2
2. left is the starting index & right is the last index of the array
2. Recursion Phase
1. Call Merge Sort on the left sub-array
2. Call Merge Sort on the right sub-array
3. Merge Phase
1. Call merge function to merge the divided sub-arrays back to the
original array.
2. Perform sorting of these smaller sub arrays before merging them
back.
Merge Sort- Division
Merge Sort- Merging
Merge Sort - Algorithm
mergeSort(arr[],l,r)
{
if(l<r)
{
midpoint = (l+r)/2
mergeSort(arr,l,m)
mergeSort(arr,m+1,r)
merge(arr,l,m,r)
}
}
Merge Sort
Advantages
• It is quicker for larger lists because unlike insertion and
bubble sort it doesnt go through the whole list seveal
times.
• It has a consistent running time, carries out different
bits with similar times in a stage.
Disadvantages
• Slower comparative to the other sort algorithms for
smaller tasks.
• uses more memory space to store the sub elements of
the initial split list.
Quick Sort
• Quick Sort Algorithm is a Divide &
Conquer algorithm. It divides input array in two
partitions, calls itself for the two
partitions(recursively) and performs in-place
sorting while doing so. A
separate partition() function is used for
performing this in-place sorting at every iteration.
• Time Complexity: θ(nlog(n))
• Space Complexity: O(log(n))
Quick Sort - Continue
• There are 2 Phases in the Quick Sort Algorithm.
1. Division Phase – Divide the array into 2 halves by
finding the pivot point to perform the partition of
the array.
1. The in-place sorting happens in this partition process
itself.
2. Recursion Phase –
1. Call Quick Sort on the left partition
2. Call Quick Sort on the right partition.
Quick Sort - Algorithm
Quick Sort Algorithm Quick Sort Partition Function
QuickSort(arr[], s, e) Partition(arr[], s, e)
{
{ pivot = arr[e]
if(s<e) plndex=s
for (i=s to e-1)
{ {
p = Partition(arr[],s,e) if(arr[i]<=pivot)
{
QuickSort(arr[], s, (p-1)) swap(arr[i], arr[pIndex])
plndex++
QuickSort(arr[], (p+1), e) }
} }
swap(arr[e], arr[plndex])
} return pIndex
}
Quick Sort - Working
Quick Sort
Advantages
• It is in-place since it uses only a small auxiliary
stack.
• It requires only n (log n) time to sort n items.
• It has an extremely short inner loop.
Disadvantages
• It is recursive. Especially, if recursion is not
available, the implementation is extremely
complicated.
• It requires quadratic (n2) time in the worst-case.

You might also like