-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathcycleSort.rb
53 lines (40 loc) · 1.2 KB
/
cycleSort.rb
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
def cycle_sort(array, swaps)
swaps = 0
cycle_start = 0
while cycle_start < array.length - 1
number_to_swap = array[cycle_start]
position = cycle_start
i = cycle_start + 1
while i < array.length
if array[i] < number_to_swap
position += 1
end
i += 1
end
if position != cycle_start
while number_to_swap == array[position]
position += 1
end
array[position], number_to_swap = number_to_swap, array[position]
swaps += 1
end
while position != cycle_start
position = cycle_start
j = cycle_start + 1
while j < array.length - 1
if array[j] < number_to_swap
position += 1
end
while number_to_swap == array[position]
position += 1
end
j += 1
end
array[position], number_to_swap = number_to_swap, array[position]
swaps += 1
end
cycle_start += 1
end
return array, swaps
end
print(cycle_sort([120, 20, 12, 1, 5, 5, 78], [0]))