mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-21 10:27:26 +08:00
* v2.1.0 [omit consensus and adjacent] - this commit will be amended with the full release after the file copy is complete * 2.1.0 main node rollup
53 lines
1006 B
Go
53 lines
1006 B
Go
// -*- go -*-
|
|
//
|
|
// Copyright (c) 2021 Markku Rossi
|
|
//
|
|
// All rights reserved.
|
|
//
|
|
|
|
// Package sort implements array sorting functions.
|
|
package sort
|
|
|
|
// Reverse reverses the argument slice.
|
|
func Reverse(arr []int) []int {
|
|
tmp := arr[0]
|
|
for i := 0; i < len(arr)/2; i++ {
|
|
tmp = arr[i]
|
|
arr[i] = arr[len(arr)-1-i]
|
|
arr[len(arr)-1-i] = tmp
|
|
}
|
|
return arr
|
|
}
|
|
|
|
// Sort sorts the argument slice in ascending order.
|
|
func Slice(arr []int) []int {
|
|
return bitonicSort(arr, 0, len(arr), true)
|
|
}
|
|
|
|
func bitonicSort(a []int, lo, n int, dir bool) []int {
|
|
if n > 1 {
|
|
m := n / 2
|
|
a = bitonicSort(a, lo, m, !dir)
|
|
a = bitonicSort(a, lo+m, n-m, dir)
|
|
a = bitonicMerge(a, lo, n, dir)
|
|
}
|
|
return a
|
|
}
|
|
|
|
func bitonicMerge(a []int, lo, n int, dir bool) []int {
|
|
if n > 1 {
|
|
m := floorPow2(n - 1)
|
|
tmp := a[0]
|
|
for i := lo; i < lo+n-m; i++ {
|
|
if dir == (a[i] > a[i+m]) {
|
|
tmp = a[i]
|
|
a[i] = a[i+m]
|
|
a[i+m] = tmp
|
|
}
|
|
}
|
|
a = bitonicMerge(a, lo, m, dir)
|
|
a = bitonicMerge(a, lo+m, n-m, dir)
|
|
}
|
|
return a
|
|
}
|