Quicksort 1 - Partition

  • + 0 comments

    My answer in Typescript, func to reusable

    function quickSort(arr: number[]): number[] {
        // for resusable
        const split = (arr: number[]): [number[], number[], number, number[]] => {
            let pivot = arr[0]
            let lh = []
            let rh = []
            for (let i in arr) {
                if (arr[i] < pivot) lh.push(arr[i])
                if (arr[i] > pivot) rh.push(arr[i])
            }
            return [[...lh, pivot, ...rh], lh, pivot, rh]
        }
    
        return split(arr)[0]
    }