Array - 43: Cyclic Sort | Sort the elements from 1 to n

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 26

  • @jitendraraghuwanshi1365
    @jitendraraghuwanshi1365 4 роки тому +5

    explained very well 👍🏼 keep up the good work

  • @IndianVideoGameCollector
    @IndianVideoGameCollector 3 роки тому +2

    Thank you, this was straightforward and easy to understand

  • @reekrev
    @reekrev 2 роки тому +1

    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

  • @free-palestine000
    @free-palestine000 2 роки тому +1

    best channel

  • @prasanthkumar6393
    @prasanthkumar6393 Рік тому

    This video defines the channel name❤

  • @AnushaPoosala
    @AnushaPoosala Рік тому

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

  • @KK7155.
    @KK7155. 3 роки тому +1

    Thank you so much. Very nicely explained 👍

  • @veeresh4441
    @veeresh4441 2 роки тому +1

    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

  • @pranays6503
    @pranays6503 2 роки тому +1

    great explanation broo :)

  • @nikhilgoyal8340
    @nikhilgoyal8340 3 роки тому

    The time complexity mentioned in the description is not correct.

    • @AmitKumar-rr5jx
      @AmitKumar-rr5jx 3 роки тому

      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.

  • @Randomizer02
    @Randomizer02 2 роки тому

    What if duplicate values exist?

  • @as_if
    @as_if 2 роки тому

    Now this is what you call beautiful

  • @PankajGupta-gh9cm
    @PankajGupta-gh9cm 4 роки тому +1

    what if array contains duplicates

    • @ayushraj2928
      @ayushraj2928 4 роки тому +1

      //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

  • @viharivemuri7202
    @viharivemuri7202 8 місяців тому

    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?

  • @yogendrapratapsingh7618
    @yogendrapratapsingh7618 4 роки тому

    Cycle sort is also for non continuous elements

    • @TheSimpleEngineer
      @TheSimpleEngineer 4 роки тому +1

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

    • @cwash08
      @cwash08 4 роки тому

      @@TheSimpleEngineer thanks for the tip