4,460 views
0 votes
0 votes
Which of the following sorting algorithm has almost the same worst case and best case complexity?

1- Quick Sort

2- Merge Sort

3- Shell Sort

4- Heap Sort

 

PS:  Is it heap or shell, I am confused. Please clarify me.

1 Answer

2 votes
2 votes
Sorting algorithms are important concepts and should be understood properly :-

1. Quick sort :- Everything is about the selection of pivot. If we choose pivot randomly and say i selected a pivot which is the greatest or least element, and following that if I partition the array then:

T (n) = T (n-1) + n  = O(n^2)

Else if pivot is not the greatest or smallest then it's equivalent to:-

T(n) = 2T (n/2) + n = O (nlogn)

2.In case of merge sort it doest depend on pivot sort of thing hence it's not a liability here it is based on dividing the array and sorting these small arrays and combining them :-

T (n) = 2T (n/2) + n = O (nlogn)

3. Shell sort what to say it's nearly like insertion sort.

Shell sort best case -> O(nlog⁡n), Worst Case-> O(n^2)

4. Heap sort :- it's based on a complete binary tree whose height is logn. Hence for creating heap = O (n) and after that we delete the root (heap means min heap) we get 1st smallest element it takes O (logn) per delete operation. Hence for n delete operation: - O (nlogn) which is it's best and worst case.

Related questions

0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
3
NeelParekh asked Jul 27, 2023
277 views
If an array is split in the form of increasing and decreasing order then what is TC to find minimum element in the array?
2 votes
2 votes
1 answer
4
h4kr asked Dec 30, 2022
466 views
What is the correct time complexity in $\theta()$ ?