Last active
November 3, 2018 05:22
-
-
Save hephyr/f51686603adb484b5aaa3bcdc311d863 to your computer and use it in GitHub Desktop.
Basic Sort Algorithm in Go
This file contains 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" | |
"time" | |
) | |
func MaxHeaplify(a []int, i, len int) { | |
left := 2*i + 1 | |
right := 2*i + 2 | |
latest := i | |
if left < len && a[left] > a[latest] { | |
latest = left | |
} | |
if right < len && a[right] > a[latest] { | |
latest = right | |
} | |
if latest != i { | |
temp := a[i] | |
a[i] = a[latest] | |
a[latest] = temp | |
MaxHeaplify(a, latest, len) | |
} | |
} | |
func BuildMaxHeap(a []int) { | |
len := len(a) | |
for i := len / 2; i >= 0; i-- { | |
MaxHeaplify(a, i, len) | |
} | |
} | |
func HeapSort(a []int) { | |
BuildMaxHeap(a) | |
for i := len(a) - 1; i >= 0; i-- { | |
max := a[0] | |
a[0] = a[i] | |
a[i] = max | |
MaxHeaplify(a, 0, i) | |
} | |
} | |
func main() { | |
a := []int{} | |
r := rand.New(rand.NewSource(time.Now().Unix())) | |
for i := 0; i < 20; i++ { | |
a = append(a, r.Intn(200)) | |
} | |
fmt.Println(a) | |
HeapSort(a) | |
fmt.Println(a) | |
} |
This file contains 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" | |
"time" | |
) | |
func InsertSort(a []int) { | |
n := len(a) | |
for i := 1; i < n; i++ { | |
v := a[i] | |
for j := i - 1; j >= 0 && a[j] > v; j-- { | |
a[j+1] = a[j] | |
a[j] = v | |
} | |
} | |
} | |
func main() { | |
a := []int{} | |
r := rand.New(rand.NewSource(time.Now().Unix())) | |
for i := 0; i < 100; i++ { | |
a = append(a, r.Intn(200)) | |
} | |
fmt.Println(a) | |
InsertSort(a) | |
fmt.Println(a) | |
} |
This file contains 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" | |
"time" | |
) | |
const MAX = 201 | |
func MergeSort(a []int, p, r int) { | |
if p < r { | |
q := (p + r) / 2 | |
MergeSort(a, p, q) | |
MergeSort(a, q+1, r) | |
Merge(a, p, q, r) | |
} | |
} | |
func Merge(a []int, p, q, r int) { | |
n1 := q - p + 1 | |
l1 := make([]int, n1+1, n1+1) | |
n2 := r - q | |
l2 := make([]int, n2+1, n2+1) | |
for i := 0; i < n1; i++ { | |
l1[i] = a[p+i] | |
} | |
for i := 0; i < n2; i++ { | |
l2[i] = a[q+i+1] | |
} | |
l1[n1] = MAX | |
l2[n2] = MAX | |
i, j := 0, 0 | |
for k := p; k <= r; k++ { | |
if l1[i] > l2[j] { | |
a[k] = l2[j] | |
j++ | |
} else { | |
a[k] = l1[i] | |
i++ | |
} | |
} | |
} | |
func main() { | |
a := []int{} | |
r := rand.New(rand.NewSource(time.Now().Unix())) | |
for i := 0; i < 10; i++ { | |
a = append(a, r.Intn(MAX-1)) | |
} | |
fmt.Println(a) | |
MergeSort(a, 0, len(a)-1) | |
fmt.Println(a) | |
} |
This file contains 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" | |
"time" | |
) | |
func QuickSort(a []int, p, r int) { | |
if p < r { | |
// q := Partition(a, p, r) | |
q := RandomizedPartition(a, p, r) | |
temp := a[q] | |
a[q] = a[p] | |
a[p] = temp | |
QuickSort(a, p, q-1) | |
QuickSort(a, q+1, r) | |
} | |
} | |
func Partition(a []int, p, r int) int { | |
q := p | |
for i := p + 1; i <= r; i++ { | |
if a[i] < a[p] { | |
q++ | |
temp := a[i] | |
a[i] = a[q] | |
a[q] = temp | |
} | |
} | |
return q | |
} | |
func RandomizedPartition(a []int, p, r int) int { | |
ra := rand.New(rand.NewSource(time.Now().Unix())) | |
i := ra.Intn(r-p) + p | |
temp := a[i] | |
a[i] = a[p] | |
a[p] = temp | |
return Partition(a, p, r) | |
} | |
func main() { | |
a := []int{} | |
r := rand.New(rand.NewSource(time.Now().Unix())) | |
for i := 0; i < 20; i++ { | |
a = append(a, r.Intn(200)) | |
} | |
fmt.Println(a) | |
QuickSort(a, 0, len(a)-1) | |
fmt.Println(a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment