00:01
For this question, we're basically given four types of sorting methods, and we're asked to write what the time complexities are for each of these different sorting methods.
00:09
So the first sorted method we're starting with is counting sort.
00:13
The way counting sort works is basically it uses keys between a specific range, and it counts a number of objects that have distinct key values, and then basically does arithmetic to calculate the position of each of those objects in the output sequence.
00:30
So that's basically how counting sort works.
00:33
And since counting sort basically needs to go through each element in the input array, and it also has a range of inputs, which we can call k, then the time complexity will actually be written as big o of n plus k.
00:54
And in this case n is the number of elements and k is the range of input so this will be the time complexity for that for merge sort and another way you can write this is i think simply o of n would work if that's you know what what you're looking for so either of these work the time complexity for mergesort so mergesort is a recursive algorithm so the time complexity is usually expressed with the recurrence relation t of n equals 2t of n over 2 plus theta of n and so this is obviously a recurrence relationship because you're using t over and over again but this can be solved using two methods which are kind of complex.
02:01
But simply the solution you get from this, what this basically means is that the time complexity in the form we're looking for is theta of n log n.
02:14
So that's going to be the time complexity.
02:16
And this is simply because mergersore is a recursive algorithm since, you know, that's just the way mergesort works.
02:25
You take in an algorithm, you divide the input into halves, and then you keep calling that same algorithm itself into a half, and then sort that into half, and then merge the sorted half.
02:39
And then merge the sorted half, so once you get to the smallest unit, you can sort them, and then you merge the two sorted halves, and then once you merge it, you will have your output sorted array.
02:50
So since you need to keep calling the halving method, it's a recursive algorithm.
02:56
So it's usually described using this recursive relation, and then you can solve that recursive relation to give you your time complexity, which is theta of n log n.
03:09
So the next thing we go to is selection sort.
03:12
And the way selection sort works is basically sorts an array by repeatedly finding the minimum element from the unsororor.
03:20
Part and then putting it at the beginning and then the algorithm always maintains two subarrays inside the given array so there's an array that's already sorted and then the remaining array which is unsorted and since the sorted the minimum element always goes to the front the element can maintain two separate parts of whatever's at the front is sorted and whichever is not at the front yet or whichever hasn't been sorted to the front is unsorted and can remain in the second part of the array.
03:51
So since it works like that, the time complexity is going to be basically of n squared.
03:58
And the reason is there's two arrays which are looping over and over again...