Data structure — quick sort

Time:2021-10-18

Quick sort is a sort algorithm with high efficiency. Its idea is mainly recursion

package main

import "fmt"

func QuickSort(left int, right int, arr *[6]int) {
    l := left
    r := right
    pivot := arr[(left + right) / 2]
    for ; l < r; {
        for ; arr[l] < pivot; {
            l++
        }
        for ; arr[r] > pivot; {
            r--
        }
        if l >= r {
            break
        }
        arr[r], arr[l] = arr[l], arr[r]
        if arr[l] == pivot {
            r--
        }
        if arr[r] == pivot {
            l++
        }
    }
    if l==r {
        l++
        r--
    }
    if left < r {
        QuickSort(left, r, arr)
    }
    if right > l {
        QuickSort(l, right, arr)
    }
}

func main(){
    arr := [6]int{-9, 78, 0, 23, -57, 70}
    fmt.Println(arr)
    QuickSort(0, len(arr) - 1, &arr)
    fmt.Println(arr)

}

Output:

[-9 78 0 23 -57 70]
[-57 -9 0 23 70 78]

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]