Array - 43: Cyclic Sort | Sort the elements from 1 to n
Source Code:thecodingsimplified.com/cycli...
Solution:
- We'll start from 0th index & iterate all elements of all array
- If we arrar value is not index + 1, then we need to swap the value, So we get the index value & put this value to it's correct index
- If it's not the case, then we move to next index.
- So in one iteration of array, we'll sort the array.
Time Complexity: O(n * n)
Space Complexity: O(1)
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimplified.com
I started my KZread channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.
★☆★ SUBSCRIBE TO ME ON KZread: ★☆★
kzread.info...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
Пікірлер: 25
Thank you, this was straightforward and easy to understand
@CodingSimplified
2 жыл бұрын
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
explained very well 👍🏼 keep up the good work
@CodingSimplified
4 жыл бұрын
Glad you liked it. Keep Watching.
Thank you so much. Very nicely explained 👍
@CodingSimplified
3 жыл бұрын
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?
@sohailbasha7781
2 жыл бұрын
Yes, but i think the logic is helpful in solving dsa
Sir,, Your way of teaching is Amazing. Please add source code.
What if duplicate values exist?
This video defines the channel name❤
Now this is what you call beautiful
The time complexity mentioned in the description is not correct.
@AmitKumar-rr5jx
2 жыл бұрын
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.
great explanation broo :)
@CodingSimplified
Жыл бұрын
Thanks for your nice feedback. Keep Watching :)
best channel
@CodingSimplified
2 жыл бұрын
Thanks for your nice feedback. Keep Watching.
what if array contains duplicates
@ayushraj2928
4 жыл бұрын
//Scan vector/array A(n) //Now cycle logic for (int i = 0; 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
Cycle sort is also for non continuous elements
@TheSimpleEngineer
4 жыл бұрын
This is cyclic sort, not cycle sort. There is a difference.
@cwash08
3 жыл бұрын
@@TheSimpleEngineer thanks for the tip
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?