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

  • @IndianVideoGameCollector
    @IndianVideoGameCollector2 жыл бұрын

    Thank you, this was straightforward and easy to understand

  • @CodingSimplified

    @CodingSimplified

    2 жыл бұрын

    Thanks for your nice feedback. Keep Watching.

  • @reekrev
    @reekrev Жыл бұрын

    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

  • @jitendraraghuwanshi1365
    @jitendraraghuwanshi13654 жыл бұрын

    explained very well 👍🏼 keep up the good work

  • @CodingSimplified

    @CodingSimplified

    4 жыл бұрын

    Glad you liked it. Keep Watching.

  • @KK7155.
    @KK7155.3 жыл бұрын

    Thank you so much. Very nicely explained 👍

  • @CodingSimplified

    @CodingSimplified

    3 жыл бұрын

    Thanks for your nice feedback. Keep Watching.

  • @veeresh4441
    @veeresh44412 жыл бұрын

    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

    @sohailbasha7781

    2 жыл бұрын

    Yes, but i think the logic is helpful in solving dsa

  • @user-xe8lz1li9w
    @user-xe8lz1li9w9 ай бұрын

    Sir,, Your way of teaching is Amazing. Please add source code.

  • @Randomizer02
    @Randomizer02 Жыл бұрын

    What if duplicate values exist?

  • @prasanthkumar6393
    @prasanthkumar6393 Жыл бұрын

    This video defines the channel name❤

  • @as_if
    @as_if Жыл бұрын

    Now this is what you call beautiful

  • @nikhilgoyal8340
    @nikhilgoyal83403 жыл бұрын

    The time complexity mentioned in the description is not correct.

  • @AmitKumar-rr5jx

    @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.

  • @pranays6503
    @pranays6503 Жыл бұрын

    great explanation broo :)

  • @CodingSimplified

    @CodingSimplified

    Жыл бұрын

    Thanks for your nice feedback. Keep Watching :)

  • @free-palestine000
    @free-palestine0002 жыл бұрын

    best channel

  • @CodingSimplified

    @CodingSimplified

    2 жыл бұрын

    Thanks for your nice feedback. Keep Watching.

  • @PankajGupta-gh9cm
    @PankajGupta-gh9cm4 жыл бұрын

    what if array contains duplicates

  • @ayushraj2928

    @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

  • @yogendrapratapsingh7618
    @yogendrapratapsingh76184 жыл бұрын

    Cycle sort is also for non continuous elements

  • @TheSimpleEngineer

    @TheSimpleEngineer

    4 жыл бұрын

    This is cyclic sort, not cycle sort. There is a difference.

  • @cwash08

    @cwash08

    3 жыл бұрын

    @@TheSimpleEngineer thanks for the tip

  • @viharivemuri7202
    @viharivemuri7202Ай бұрын

    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?