Last active
October 1, 2019 06:17
-
-
Save okamos/f30643678c25def17978dfbd907dbc98 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" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
arr := make([]int, 10) | |
for i := range arr { | |
arr[i] = rand.Intn(100) | |
} | |
fmt.Printf("%+v\n", arr) | |
for j := len(arr) - 1; j > 0; j-- { | |
for i := 0; i < j; i++ { | |
fmt.Printf("%d ", i) | |
if arr[i] > arr[i+1] { | |
arr[i], arr[i+1] = arr[i+1], arr[i] | |
} | |
} | |
fmt.Println("") | |
} | |
fmt.Printf("%+v\n", arr) | |
} |
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 main() { | |
arr := makeArr(10) | |
fmt.Printf("%+v\n", arr) | |
// fmt.Printf("%+v", QuickSort2(arr)) | |
fmt.Printf("%+v", QuickSort3(arr)) | |
} | |
func QuickSort3(a []int) []int { | |
quickSort(a, 0, len(a)-1) | |
return a | |
} | |
func quickSort(a []int, left, right int) { | |
if left < right { | |
i, j := left, right | |
pivot := a[i] | |
for { | |
for ; a[i] < pivot; i++ { | |
} | |
for ; pivot < a[j]; j-- { | |
} | |
if i >= j { | |
break | |
} | |
a[i], a[j] = a[j], a[i] | |
i++ | |
j-- | |
} | |
quickSort(a, left, i-1) | |
quickSort(a, j+1, right) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment