Created
August 1, 2020 10:25
-
-
Save yasuoza/9f2e58c1e207bb3c76c611a354cdf903 to your computer and use it in GitHub Desktop.
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" | |
) | |
func main() { | |
arr := []int{10, 7, 3, 4, 6, 7, 0, 9} | |
arr = sort(arr) | |
fmt.Println(arr) | |
} | |
func sort(arr []int) []int { | |
if len(arr) <= 1 { | |
return arr | |
} | |
p := (len(arr) - 1) / 2 | |
fmt.Println("arr", arr, "p", p, "pivot", arr[p]) | |
var left []int | |
var right []int | |
for i, e := range arr { | |
if e < arr[p] { | |
left = append(left, e) | |
} else if i != p && e >= arr[p] { | |
right = append(right, e) | |
} | |
} | |
return append(append(sort(left), arr[p]), sort(right)...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment