-
Notifications
You must be signed in to change notification settings - Fork 1
/
no-smaller-than-current.go
66 lines (55 loc) · 1.29 KB
/
no-smaller-than-current.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
64
65
66
package main
import (
"fmt"
)
func main() {
nums := []int{8,1,2,3,2,4}
//sort the list with O(nlog(n))
nums_sorted := MergeSort(nums)
fmt.Printf("Sorted list:%d\n", nums_sorted)
fmt.Println()
m := make(map[int]int)
for i:=0; i<len(nums_sorted); i++ {
_, ok := m[nums_sorted[i]]
if ok == false {
m[nums_sorted[i]] = i
} else {
m[nums_sorted[i]] = m[nums_sorted[i]]
}
}
nums_out := []int{}
for i:=0; i<len(nums); i++ {
nums_out = append(nums_out, m[nums[i]])
}
fmt.Printf("Numbers in the current array: %d \n", nums)
fmt.Println()
fmt.Printf("Numbers smaller than current no: %d \n", nums_out)
}
func MergeSort(s []int) []int {
if len(s) <= 1 {
return s
}
n := len(s)/2
l := MergeSort(s[:n])
r := MergeSort(s[n:])
return Merge(l, r)
}
func Merge(l, r []int) []int {
ret := []int {}
for len(l) > 0 || len(r) > 0 {
if len(l) == 0 {
return append(ret, r...)
}
if len(r) == 0 {
return append(ret, l...)
}
if l[0] < r[0] {
ret = append(ret, l[0])
l = l[1:]
} else {
ret = append(ret, r[0])
r = r[1:]
}
}
return ret
}