Created
February 13, 2021 10:55
-
-
Save harkce/1625c3b1814d001f9bbba65e848e9942 to your computer and use it in GitHub Desktop.
Quicksort
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" | |
| func quicksort(A []int) []int { | |
| if len(A) < 2 { | |
| return A | |
| } | |
| left := 0 | |
| right := len(A) - 1 | |
| for i := 0; i < len(A)-1; i++ { | |
| if A[i] < A[right] { | |
| A[i], A[left] = A[left], A[i] | |
| left++ | |
| } | |
| } | |
| A[left], A[right] = A[right], A[left] | |
| quicksort(A[:left]) | |
| quicksort(A[left+1:]) | |
| return A | |
| } | |
| func main() { | |
| unsorted := []int{5, 3, 2, 8, 6, 1, 4} | |
| fmt.Printf("Unsorted: %v\n", unsorted) | |
| fmt.Printf("Sorted : %v\n", quicksort(unsorted)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment