1. func quickSort(arr []int) []int {
    2. length := len(arr) - 1
    3. stack := [][]int{{0, length}}
    4. for len(stack) > 0 {
    5. position := stack[len(stack)-1]
    6. stack = stack[:len(stack)-1]
    7. left := position[0]
    8. right := position[1]
    9. index := partition(arr, left, right)
    10. if left < index - 1 {
    11. stack = append(stack, []int{left, index-1})
    12. }
    13. if right > index + 1 {
    14. stack = append(stack, []int{index + 1, right})
    15. }
    16. }
    17. return arr
    18. }
    19. func partition(arr []int, left, right int) int {
    20. indexData := arr[left]
    21. OUT:
    22. for left < right {
    23. for arr[right] >= indexData {
    24. if left >= right {
    25. break OUT
    26. }
    27. right--
    28. }
    29. arr[left] = arr[right]
    30. for arr[left] <= indexData {
    31. if left >= right {
    32. break OUT
    33. }
    34. left++
    35. }
    36. arr[right] = arr[left]
    37. }
    38. arr[right] = indexData
    39. return right
    40. }

    image.png
    image.png
    image.png