Guest User

Untitled

a guest
Dec 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. )
  7.  
  8. func Qsort(arr []int, left, right int) {
  9. if left + 1 == right {
  10. if arr[left] > arr[right] {
  11. arr[left], arr[right] = arr[right], arr[left]
  12. }
  13. return
  14. }
  15. if right < left + 1 {
  16. return
  17. }
  18.  
  19. pivot_index := left + rand.Intn(right - left + 1)
  20. arr[left], arr[pivot_index] = arr[pivot_index], arr[left]
  21. pivot := arr[left]
  22. index := left + 1
  23.  
  24. for i := left + 1; i <= right; i++ {
  25. if arr[i] < pivot {
  26. arr[i], arr[index] = arr[index], arr[i]
  27. index++
  28. }
  29. }
  30. fmt.Printf("%d\n", index)
  31. arr[left], arr[index - 1] = arr[index - 1], arr[left]
  32. Qsort(arr, left, index - 1)
  33. Qsort(arr, index, right)
  34. }
  35.  
  36. func main() {
  37. arr := []int{2, 1, 1, 5, 3, 11, 1, 1}
  38.  
  39. Qsort(arr, 0, len(arr) - 1)
  40.  
  41. fmt.Println(arr)
  42. }
Add Comment
Please, Sign In to add comment