-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort_slices.go
160 lines (150 loc) · 3.55 KB
/
bubble_sort_slices.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
@auhthor : Rishikesh Agrawani
@coded_on: 23/12/2016, Friday
*/
package ds
import "github.com/fatih/color"
import "errors"
//Defining type for int & float slices
type Float32Slice []float32
type Float64Slice []float64
type IntSlice []int
type Int8Slice []int8
type Int16Slice []int16
type Int32Slice []int32
type Int64Slice []int64
type UintSlice []uint
type Uint8Slice []uint8
type Uint16Slice []uint16
type Uint32Slice []uint32
type Uint64Slice []uint64
//A function that sorts a slice of float32
func (f32Slice Float32Slice) Sort() {
for i := len(f32Slice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if f32Slice[j] > f32Slice[j+1] {
f32Slice[j], f32Slice[j+1] = f32Slice[j+1], f32Slice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that sorts a slice of float64
func (f64Slice Float64Slice) Sort() {
for i := len(f64Slice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if f64Slice[j] > f64Slice[j+1] {
f64Slice[j], f64Slice[j+1] = f64Slice[j+1], f64Slice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that sorts a slice of int
func (iSlice IntSlice) Sort() {
for i := len(iSlice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if iSlice[j] > iSlice[j+1] {
iSlice[j], iSlice[j+1] = iSlice[j+1], iSlice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that sorts a slice of int8
func (i8Slice Int8Slice) Sort() {
for i := len(i8Slice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if i8Slice[j] > i8Slice[j+1] {
i8Slice[j], i8Slice[j+1] = i8Slice[j+1], i8Slice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that sorts a slice of int16
func (i16Slice Int16Slice) Sort() {
for i := len(i16Slice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if i16Slice[j] > i16Slice[j+1] {
i16Slice[j], i16Slice[j+1] = i16Slice[j+1], i16Slice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that sorts a slice of int32
func (i32Slice Int32Slice) Sort() {
for i := len(i32Slice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if i32Slice[j] > i32Slice[j+1] {
i32Slice[j], i32Slice[j+1] = i32Slice[j+1], i32Slice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that sorts a slice of int64
func (i64Slice Int64Slice) Sort() {
for i := len(i64Slice) - 2; i >= 0; i-- {
swapped := false
for j := 0; j <= i; j++ {
if i64Slice[j] > i64Slice[j+1] {
i64Slice[j], i64Slice[j+1] = i64Slice[j+1], i64Slice[j]
swapped = true
}
}
if !swapped {
break
}
}
}
//A function that checks type of slice and calls their related Sort() to sort it. It returns error as nil (on success) otherwise a valid error type
func BubbleSort(listOfNums interface{}) {
switch t := listOfNums.(type) {
case []int:
IntSlice(listOfNums.([]int)).Sort()
case []int8:
Int8Slice(listOfNums.([]int8)).Sort()
case []int16:
Int16Slice(listOfNums.([]int16)).Sort()
case []int32:
Int32Slice(listOfNums.([]int32)).Sort()
case []int64:
Int64Slice(listOfNums.([]int64)).Sort()
case []float32:
Float32Slice(listOfNums.([]float32)).Sort()
case []float64:
Float64Slice(listOfNums.([]float64)).Sort()
default:
err := errors.New("Got an unwanted slice to sort ")
color.Blue("[hygull/ds] Got => %v", t)
color.Blue("[hygull/ds] Type => %T", t)
color.Red("[hygull/ds] You have to pass any one among these => []int, []int8,[]int16,[]int32,[]int64,[]float32,[]float64")
panic(err)
}
}