-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.go
More file actions
43 lines (38 loc) · 899 Bytes
/
bubble_sort.go
File metadata and controls
43 lines (38 loc) · 899 Bytes
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
package main
import (
"fmt"
"math/rand/v2"
)
func GenerateArray(size, minNumber, maxNumber int) []int {
array := make([]int, size);
for index := range array {
array[index] = rand.IntN(maxNumber - minNumber + 1) + minNumber;
}
return array
}
func BubbleSort(array []int) {
for i := 0; i < len(array); i++ {
for j := i+1; j < len(array); j++ {
if array[i] > array[j] {
array[i], array[j] = array[j], array[i]
}
}
}
}
// Using Effective and modern golang https://go.dev/doc/effective_go#arrays
func ModernSyntaxBubbleSort(array []int) {
for i := range array {
for j := range array{
if i != j && array[i] < array[j] {
array[i], array[j] = array[j], array[i]
}
}
}
}
func main(){
numbers := GenerateArray(10, 0, 100)
fmt.Println("Original Array: ", numbers)
// BubbleSort(numbers)
ModernSyntaxBubbleSort(numbers)
fmt.Println("Sorted Array: ", numbers)
}