Created
January 28, 2018 19:34
-
-
Save squaredice/5ba95f9eef17ceb35b8a8c58330d7be3 to your computer and use it in GitHub Desktop.
Few sorting algoritms on Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"runtime" | |
"time" | |
) | |
//RandomArray create random array//////////// | |
func RandomArray(n int) []int { // | |
arr := make([]int, n) // | |
for i := int(0); i <= n-1; i++ { // | |
arr[i] = rand.Intn(n) // | |
} // | |
return arr // | |
} // | |
///////////////////////////////////////////// | |
//quickSort | |
func quickSort(arr []int) []int { | |
if len(arr) <= 1 { | |
return arr | |
} | |
median := arr[rand.Intn(len(arr))] | |
low_part := make([]int, 0, len(arr)) | |
high_part := make([]int, 0, len(arr)) | |
middle_part := make([]int, 0, len(arr)) | |
for _, item := range arr { | |
switch { | |
case item < median: | |
low_part = append(low_part, item) | |
case item == median: | |
middle_part = append(middle_part, item) | |
case item > median: | |
high_part = append(high_part, item) | |
} | |
} | |
low_part = quickSort(low_part) | |
high_part = quickSort(high_part) | |
low_part = append(low_part, middle_part...) | |
low_part = append(low_part, high_part...) | |
return low_part | |
} | |
//selectionSort | |
func selectionSort(n int) { | |
arr := RandomArray(n) | |
min := 0 | |
tmp := 0 | |
for i := 0; i < len(arr); i++ { | |
min = i | |
for j := i + 1; j < len(arr); j++ { | |
if arr[j] < arr[min] { | |
min = j | |
} | |
} | |
tmp = arr[i] | |
arr[i] = arr[min] | |
arr[min] = tmp | |
} | |
} | |
//insertionSort | |
func insertionSort(n int) { | |
arr := RandomArray(n) | |
for out := 1; out < len(arr); out++ { | |
temp := arr[out] | |
in := out | |
for ; in > 0 && arr[in-1] >= temp; in-- { | |
arr[in] = arr[in-1] | |
} | |
arr[in] = temp | |
} | |
} | |
//shellSort | |
func shellSort(n int) { | |
arr := RandomArray(n) | |
for d := int(len(arr) / 2); d > 0; d /= 2 { | |
for i := d; i < len(arr); i++ { | |
for j := i; j >= d && arr[j-d] > arr[j]; j -= d { | |
arr[j], arr[j-d] = arr[j-d], arr[j] | |
} | |
} | |
} | |
} | |
//bubbleSort | |
func bubbleSort(n int) { | |
arrayzor := RandomArray(n) | |
swapped := true | |
for swapped { | |
swapped = false | |
for i := int(0); i < int(len(arrayzor)-1); i++ { | |
if arrayzor[i+1] < arrayzor[i] { | |
Swap(arrayzor, i, i+1) | |
swapped = true | |
} | |
} | |
} | |
} | |
//Swap making swap | |
func Swap(arrayzor []int, i, j int) { | |
tmp := arrayzor[j] | |
arrayzor[j] = arrayzor[i] | |
arrayzor[i] = tmp | |
} | |
func main() { | |
numb := runtime.NumCPU() | |
runtime.GOMAXPROCS(numb) | |
fmt.Printf("Enter array length: ") | |
var num int | |
fmt.Scanf("%d", &num) | |
t := time.Now() | |
go shellSort(num) | |
fmt.Println("Shell sort: ", time.Since(t)) | |
go bubbleSort(num) | |
fmt.Println("Bubble sort: ", time.Since(t)) | |
go insertionSort(num) | |
fmt.Println("Insertion sort: ", time.Since(t)) | |
go selectionSort(num) | |
fmt.Println("Selection sort: ", time.Since(t)) | |
go quickSort(RandomArray(num)) | |
fmt.Println("Quick sort: ", time.Since(t)) | |
time.Sleep(60 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment