Name | Best-case | Average-case | Worst-case | Memory | Stable |
---|---|---|---|---|---|
CycleSort | - | n2 | n2 | 1 | no |
public void cycleSort(int[] array) { int pos; int item; int temp; for (int cycleStart = 0; cycleStart < array.length - 1; cycleStart++) { item = array[ cycleStart ]; pos = cycleStart; for (int i = cycleStart + 1; i < array.length; i++) { if (array[ i ] < item) pos++; } if (pos == cycleStart) continue; while (item == array[ pos ]) pos++; if (item != array[ pos ]) { temp = array[ pos ]; array[ pos ] = item; item = temp; } while (pos != cycleStart) { pos = cycleStart; for (int i = cycleStart + 1; i < array.length; i++) { if (array[ i ] < item) pos++; } while (item == array[ pos ]) pos++; if (item != array[ pos ]) { temp = array[ pos ]; array[ pos ] = item; item = temp; } } } }
Use the textfield to type in a number and add it by either pressing ENTER or by clicking on the "Add" button.
You can also add 10 random numbers at once by clicking on the "10 Random Keys" button. Overall you can add up to 50 keys.
The "Sort" button starts to sort the keys with the selected algorithm. Alternatively you can sort 100 random keys fast for a quick impression of how the algorithm works.