forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyclic_sort_test.go
63 lines (54 loc) · 1.43 KB
/
cyclic_sort_test.go
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
61
62
63
/*
Problem:
- Given an array containing n objects where each object, when created,
was assigned a unique number from 1 to n based on their creation sequence.
This means that the object with sequence number 3 was created just before
the object with sequence number 4.
- Write a function to sort the objects in-place on their creation sequence
number in O(n) and without any extra space.
Example:
- Input: []int{6, 3, 5, 2, 4, 1}
Output: []int{1, 2, 3, 4, 5, 6}
Approach:
- Use the fact that we are given a range of 1-n, can try placing each number at
its correct index in the array, say 1 at 0, 2 at 1, 3 at 2 and so on.
- Iterate through the array and if the current number is not at the correct index,
swap it with the number at its correct index.
Cost:
- O(n) time, O(1) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestCyclicSort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{6, 4, 5, 2, 3, 1}, []int{1, 2, 3, 4, 5, 6}},
}
for _, tt := range tests {
cyclicSort(tt.in)
common.Equal(
t,
tt.expected,
tt.in,
)
}
}
func cyclicSort(nums []int) {
i := 0
for i < len(nums) {
// if the current number is not at the correct index, swap it with the
// one that is at the correct index.
correctIndex := nums[i] - 1
if nums[i] != nums[correctIndex] {
common.Swap(nums, i, correctIndex)
} else {
i++
}
}
}