[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/AllAlgorithms/javascript/master/algorithms/sorting/quickSort.js [Back]  [Original]

function quickSort (recievedArray) {
  const sortedArray = Object.assign([], recievedArray)

  function partition (array, begin, end) {
    const pivot = array[Math.floor((end + begin) / 2)]
    let i = begin
    let j = end

    while (i  pivot) {
        j--
      }

      if (i  1) {
      index = partition(array, begin, end)

      if (begin < index - 1)
        sort(array, begin, index - 1)

      if (index < end)
        sort(array, index, end)
    }

    return array
  }

  function swap (array, firstIndex, secondIndex) {
    const temp = array[firstIndex]
    array[firstIndex] = array[secondIndex]
    array[secondIndex] = temp
  }

  return sort(sortedArray, 0, sortedArray.length - 1)
}

const array = [3, 2, 9, 7, 24, 11]

const sortedArray = quickSort(array)

console.log('array', array)
console.log('sortedArray', sortedArray)

Web Proxy Viewer  |  New URL  |  Original Page