JSFiddle - React, Tailwind, and code Playground

by fliptheweb

JavaScript

const myArray = [101,5,2,34,0,74,1,3,12,6,8,9,15,32]
const elementsPerPage = 2

const swap = (items, leftIndex, rightIndex) => {
    var temp = items[leftIndex]
    items[leftIndex] = items[rightIndex]
    items[rightIndex] = temp
}

const partition = (items, left, right) => {
    var pivot = items[Math.floor((right + left) / 2)]   
    var i = left
    var j = right
    
    while (i <= j) {
        while (items[i] < pivot) {
            i++
        }
        while (items[j] > pivot) {
            j--
        }
        if (i <= j) {
            swap(items, i, j);
            i++
            j--
        }
    }
    return i;
}

const quickSort = (items, left, right) => {
    var index
    if (items.length > 1) {
        index = partition(items, left, right)
        if (left < index - 1) {
            quickSort(items, left, index - 1)
        }
        if (index < right) {
            quickSort(items, index, right)
        }
    }
    return items
}


console.log(quickSort(myArray, 0, myArray.length - 1))