1

I was reading about the cycle sort algorithm and found out that it is unstable in nature, however, I am having difficulty in coming up with a case that shows the unstable nature of the cyclic algorithm. Could someone give a case where we can observe the unstable nature of the algorithm?

More information on the algorithm:- https://en.wikipedia.org/wiki/Cycle_sort

Here is my code for Cycle sort algorithm:-

        #include <iostream> 

        using namespace std;

        int main()
        {
            int n;
            cin >> n;
            int *arr;
            arr=new int[n];
            for (int i = 0; i < n; i++)
                cin >> arr[i];
            int cyStart, item, pos;
            for (int cyStart = 0; cyStart < (n - 1); cyStart++)
            {
                item = arr[cyStart];
                pos = cyStart;
                for (int i = cyStart + 1; i < n; i++)
                {
                    if (item > arr[i])
                        pos++;
                }
                if (pos == cyStart)
                    continue;
                while (item == arr[pos])
                    pos++;
                if (item != arr[pos])
                    swap(arr[pos], item);
                while (cyStart != pos)
                {
                    pos = cyStart;
                    for (int i = cyStart + 1; i < n; i++)
                    {
                        if (item > arr[i])
                            pos++;
                    }
                    while (item == arr[pos])
                        pos++;   
                    if (item != arr[pos])
                        swap(arr[pos], item);
                }
            }
            for(int i=0;i<n;i++)
                cout << arr[i] << " ";
            return 0;
        }
  • 2
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jul 02 '20 at 08:16
  • 1
    What is the "cyclic algorithm"? Perhaps provide a description or a link, since I don't think it's well-known. – Paul Hankin Jul 02 '20 at 08:16
  • 1
    I also suggest you pick up [a decent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) or two, and learn more about the standard library and what it can help you with (for example by using [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) rather than allocating memory explicitly). – Some programmer dude Jul 02 '20 at 08:17
  • 4
    Stability of sorting algorithm can only be observed when sorted elements have some characteristic that is not taken into account when sorting. For `int`, stable or unstable algorithm will produce exactly the same result. For `struct X {int n; std::string name;};`, if you sort such structures by `n` value only, you might notice a difference. – Yksisarvinen Jul 02 '20 at 08:55
  • Please take a look at [this question](https://stackoverflow.com/questions/61202074/calling-pandas-df-sort-values-multiple-times-on-the-same-column-gives-differen). I believe it is exactly the case you looking for, except that sorting is done using Python. – Suthiro Jul 02 '20 at 09:38

1 Answers1

5

So let's say we have a group of data as below:enter image description here 2 sets of letter "A" to "E". To better differentiate them, I'm gonna call duplicate data with a number attached to them. So "A"s would be "A1" and "A2" and so on. enter image description here Now let's try to sort them based on the algorithm you mentioned.

  • Step 1: enter image description here Taking the first data out, and find the place it should be put to. Since we know there are 10 items, or 2 sets of 5 identical items, we know "E1" would be placed to the 9th place.

  • Step 2: enter image description here "E1" is put into 9th place, and the item that was in 9th place before is taken out to be sorted.

  • Step 3: enter image description here "E2" is put into 10th place, "D2" is taken out to be sorted.

  • Step 4: enter image description here "E2' is put into 7th place, "B2" is taken out to be sorted.


    Now you might have noticed a problem!!

    "D2" is put into 7th place, which means "D1" can only be put into 8th place, despite it should be put in front of "D2".

    Well, this is what an "unstable sort" means. The result of the sort would be independent from the order of the original data!


Continue the sort:

  • Step 5: enter image description here
  • Step 6: enter image description here
  • Step 7: enter image description here
  • You have just completed the first cycle.
  • Starting the next cycle:
  • Step 8: enter image description here
  • Step 9: enter image description here
  • Step 10: enter image description here
  • Step 11: enter image description here
  • Step 12: enter image description here
  • You have just completed your sort

Now look at your data, you would notice that 2 pairs of data were sorted unstably:enter image description here

Ranoiaetep
  • 1,446
  • 6
  • 19