-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathcycle_sort.r
59 lines (52 loc) · 1.33 KB
/
cycle_sort.r
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
# Cycle Sort Function
# Sorts an input vector in-place using the Cycle Sort algorithm.
# Parameters:
# - arr: Input vector to be sorted.
# Returns:
# - Sorted vector.
cycle_sort <- function(arr) {
n <- length(arr)
for (cycle_start in 1:(n - 1)) {
item <- arr[cycle_start]
pos <- cycle_start
# Find the correct position for the current item
for (i in (cycle_start + 1):n) {
if (arr[i] < item) {
pos <- pos + 1
}
}
# Skip if the item is already in the correct position
if (pos == cycle_start) {
next
}
# Move the item to its correct position
while (item == arr[pos]) {
pos <- pos + 1
}
temp <- arr[pos]
arr[pos] <- item
item <- temp
# Rotate the remaining cycle
while (pos != cycle_start) {
pos <- cycle_start
for (i in (cycle_start + 1):n) {
if (arr[i] < item) {
pos <- pos + 1
}
}
# Skip if the item is already in the correct position
while (item == arr[pos]) {
pos <- pos + 1
}
# Move the item to its correct position
temp <- arr[pos]
arr[pos] <- item
item <- temp
}
}
return(arr)
}
# Example usage:
elements_vec <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
cycle_sorted_vec <- cycle_sort(elements_vec)
print(cycle_sorted_vec)