-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathcycleSort.js
60 lines (57 loc) · 1.5 KB
/
cycleSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Cycle sort function
function cycleSort(arr) {
let writes = 0;
const n = arr.length;
// traverse array elements
for (let cycleStart = 0; cycleStart <= n - 2; cycleStart++) {
item = arr[cycleStart];
// Find position where the item should be.
// counting all the smaller elements on the right side of the current item.
pos = cycleStart;
for (i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) pos++;
}
// If item is already in correct position just continue
if (pos == cycleStart) {
continue;
}
// ignore all duplicate elements
while (item == arr[pos]) {
pos += 1;
}
// put the item to it's right position
if (pos != cycleStart) {
temp = item;
item = arr[pos];
arr[pos] = temp;
writes++;
}
// Rotate rest of the cycle
while (pos != cycleStart) {
pos = cycleStart;
// Find position where we put the element
for (i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) pos += 1;
}
// ignore all duplicate elements
while (item == arr[pos]) {
pos += 1;
}
// put the item to it's right position
if (item != arr[pos]) {
temp = item;
item = arr[pos];
arr[pos] = temp;
writes++;
}
}
}
return arr;
}
function main() {
const arr = [11, 8, 23, 5, 10, 12, 2, 14, 11, 9, 4, 4, 6, 24];
console.log("Before sort:", arr.join(","));
result = cycleSort(arr);
console.log("After sort:", result.join(","));
}
main();