|
1 | 1 | /**
|
2 |
| - * @function CycleSort |
| 2 | + * @function cycleSort |
3 | 3 | * @description Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result.
|
4 | 4 | * @param {number[]}array - The input array
|
5 | 5 | * @return {number[]} - The sorted array.
|
|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | export const cycleSort = (array: number[]) => {
|
11 |
| - for (let index: number = 0; index < array.length - 1; index++) { |
12 |
| - MoveCycle(array, index); |
| 11 | + for (let i: number = 0; i < array.length - 1; i++) { |
| 12 | + MoveCycle(array, i); |
13 | 13 | }
|
14 | 14 | return array;
|
15 | 15 | };
|
16 | 16 |
|
17 | 17 | function MoveCycle(array: number[], startIndex: number) : void {
|
18 | 18 |
|
19 | 19 | let currentItem: number = array[startIndex];
|
20 |
| - let nextPositionChange: number = startIndex + CountSmallerElements(array, startIndex, currentItem); |
21 |
| - if(nextPositionChange == startIndex) |
| 20 | + let nextChangeIndex: number = startIndex + CountSmallerItems(array, startIndex, currentItem); |
| 21 | + if(nextChangeIndex == startIndex) |
22 | 22 | {
|
23 | 23 | return;
|
24 | 24 | }
|
25 | 25 |
|
26 |
| - nextPositionChange = OffsetSameElements(array, nextPositionChange, currentItem); |
| 26 | + nextChangeIndex = SkipDuplicates(array, nextChangeIndex, currentItem); |
27 | 27 |
|
28 |
| - let tmp: number = array[nextPositionChange]; |
29 |
| - array[nextPositionChange] = currentItem; |
| 28 | + let tmp: number = array[nextChangeIndex]; |
| 29 | + array[nextChangeIndex] = currentItem; |
30 | 30 | currentItem = tmp;
|
31 | 31 |
|
32 |
| - while (nextPositionChange != startIndex) |
| 32 | + while (nextChangeIndex != startIndex) |
33 | 33 | {
|
34 |
| - nextPositionChange = startIndex + CountSmallerElements(array, startIndex, currentItem); |
35 |
| - nextPositionChange = OffsetSameElements(array, nextPositionChange, currentItem); |
| 34 | + nextChangeIndex = startIndex + CountSmallerItems(array, startIndex, currentItem); |
| 35 | + nextChangeIndex = SkipDuplicates(array, nextChangeIndex, currentItem); |
36 | 36 |
|
37 |
| - tmp = array[nextPositionChange]; |
38 |
| - array[nextPositionChange] = currentItem; |
| 37 | + tmp = array[nextChangeIndex]; |
| 38 | + array[nextChangeIndex] = currentItem; |
39 | 39 | currentItem = tmp;
|
40 | 40 | }
|
41 | 41 | }
|
42 | 42 |
|
43 |
| -function CountSmallerElements(array: number[], startIndex: number, currentItem: number) : number{ |
44 |
| - let smallerElementsCount: number = 0; |
| 43 | +function CountSmallerItems(array: number[], startIndex: number, currentItem: number) : number{ |
| 44 | + let elementsCount: number = 0; |
45 | 45 |
|
46 |
| - for (let index: number = startIndex + 1; index < array.length; index++) { |
47 |
| - if(currentItem > array[index]) |
| 46 | + for (let i: number = startIndex + 1; i < array.length; i++) { |
| 47 | + if(currentItem > array[i]) |
48 | 48 | {
|
49 |
| - smallerElementsCount++; |
| 49 | + elementsCount++; |
50 | 50 | }
|
51 | 51 | }
|
52 | 52 |
|
53 |
| - return smallerElementsCount; |
| 53 | + return elementsCount; |
54 | 54 | }
|
55 | 55 |
|
56 |
| -function OffsetSameElements(array: number[], currentPosition: number, currentItem: number): number { |
| 56 | +function SkipDuplicates(array: number[], currentPosition: number, currentItem: number): number { |
57 | 57 | while (array[currentPosition] == currentItem) {
|
58 | 58 | currentPosition++;
|
59 | 59 | }
|
|
0 commit comments