Skip to content

Commit 2fbfa44

Browse files
committed
feat: add cycle sort implementation - post review changes
1 parent 6654fd1 commit 2fbfa44

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

sorts/cycle_sort.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @function CycleSort
2+
* @function cycleSort
33
* @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.
44
* @param {number[]}array - The input array
55
* @return {number[]} - The sorted array.
@@ -8,52 +8,52 @@
88
*/
99

1010
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);
1313
}
1414
return array;
1515
};
1616

1717
function MoveCycle(array: number[], startIndex: number) : void {
1818

1919
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)
2222
{
2323
return;
2424
}
2525

26-
nextPositionChange = OffsetSameElements(array, nextPositionChange, currentItem);
26+
nextChangeIndex = SkipDuplicates(array, nextChangeIndex, currentItem);
2727

28-
let tmp: number = array[nextPositionChange];
29-
array[nextPositionChange] = currentItem;
28+
let tmp: number = array[nextChangeIndex];
29+
array[nextChangeIndex] = currentItem;
3030
currentItem = tmp;
3131

32-
while (nextPositionChange != startIndex)
32+
while (nextChangeIndex != startIndex)
3333
{
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);
3636

37-
tmp = array[nextPositionChange];
38-
array[nextPositionChange] = currentItem;
37+
tmp = array[nextChangeIndex];
38+
array[nextChangeIndex] = currentItem;
3939
currentItem = tmp;
4040
}
4141
}
4242

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;
4545

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])
4848
{
49-
smallerElementsCount++;
49+
elementsCount++;
5050
}
5151
}
5252

53-
return smallerElementsCount;
53+
return elementsCount;
5454
}
5555

56-
function OffsetSameElements(array: number[], currentPosition: number, currentItem: number): number {
56+
function SkipDuplicates(array: number[], currentPosition: number, currentItem: number): number {
5757
while (array[currentPosition] == currentItem) {
5858
currentPosition++;
5959
}

0 commit comments

Comments
 (0)