bubble sort

pull/2/head
0xAX 10 years ago
commit e29348a588

26
.gitignore vendored

@ -0,0 +1,26 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
sorting/bubble_sort

@ -0,0 +1,37 @@
package main
/*
* Bubble sort, sometimes referred to as sinking sort,
is a simple sorting algorithm that works by repeatedly
stepping through the list to be sorted, comparing
each pair of adjacent items and swapping them if
they are in the wrong order. The pass through
the list is repeated until no swaps are needed,
which indicates that the list is sorted.
Complexity - О(n2)
*/
import "fmt"
import "github.com/IoSync/go-alghoritms"
func main() {
arr := utils.RandArray(10)
fmt.Println("Initial array is:", arr)
fmt.Println("")
tmp := 0
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr) - 1; j++ {
if arr[j] > arr[j + 1] {
tmp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = tmp
}
}
}
fmt.Println("Sorted array is: ", arr)
}

@ -0,0 +1,13 @@
package utils
import "math/rand"
func RandArray(n int) []int {
arr := make([]int, n)
for i := 0; i <= n - 1; i++ {
arr[i] = rand.Intn(n)
}
return arr
}
Loading…
Cancel
Save