I have two doubts here: 1. The time complexity is O(n^2), not O(n); can you see why? 2. This is not actually sorting. If you know beforehand the desired result is the ordered list of numbers from k…n you can just generate that list in O(n) time: for(int i=k;i
Time complexity is correct for the above case. That is if elements are not greater than n. So one can say, this is a special case. But the complexity of cyclic sort is 0(n ^ 2) and the algorithm will be quite different for finding the correct position.
//Scan vector/array A(n) //Now cycle logic for (int i = 0; i < n; i++) { int pos = A[i] - 1; if (A[pos] != A[i]) { swap(A[pos], A[i]); i--; } } //Print elements It will put the numbers in array in correct indexes..and the other indexes whose values are not in array will get any other repeated value Like for INPUT : 5 3 3 1 2 2 OUTPUT : 1 2 3 3 5 2
explained very well 👍🏼 keep up the good work
Glad you liked it. Keep Watching.
Thank you, this was straightforward and easy to understand
Thanks for your nice feedback. Keep Watching.
I have two doubts here:
1. The time complexity is O(n^2), not O(n); can you see why?
2. This is not actually sorting. If you know beforehand the desired result is the ordered list of numbers from k…n you can just generate that list in O(n) time:
for(int i=k;i
cry about it baby
best channel
Thanks for your nice feedback. Keep Watching.
This video defines the channel name❤
Sir,, Your way of teaching is Amazing. Please add source code.
Thank you so much. Very nicely explained 👍
Thanks for your nice feedback. Keep Watching.
We can directly replace the values with ind+1 (or ind + x) right? No need to even have checks, what's the use of cycle sort?
Yes, but i think the logic is helpful in solving dsa
great explanation broo :)
Thanks for your nice feedback. Keep Watching :)
The time complexity mentioned in the description is not correct.
Time complexity is correct for the above case. That is if elements are not greater than n. So one can say, this is a special case.
But the complexity of cyclic sort is 0(n ^ 2) and the algorithm will be quite different for finding the correct position.
What if duplicate values exist?
Now this is what you call beautiful
what if array contains duplicates
//Scan vector/array A(n)
//Now cycle logic
for (int i = 0; i < n; i++) {
int pos = A[i] - 1;
if (A[pos] != A[i]) {
swap(A[pos], A[i]);
i--;
}
}
//Print elements
It will put the numbers in array in correct indexes..and the other indexes whose values are not in array will get any other repeated value
Like for
INPUT : 5 3 3 1 2 2
OUTPUT : 1 2 3 3 5 2
What a silly video? You need to do all that to just sort numbers from 1 to n? Why can'y you just do for each index i, arr[i] = i+1?
Cycle sort is also for non continuous elements
This is cyclic sort, not cycle sort. There is a difference.
@@TheSimpleEngineer thanks for the tip