TheAlgorithms/JavaScript · error · TypeError

Please input a valid list or array.

Error message

Please input a valid list or array.

What it means

Thrown as a TypeError by quickSort() when inputList is not an array. The function immediately indexes and partitions inputList, so non-arrays would fail inside partition(). Note that low and high are NOT validated — they default to undefined and only the low < high branch executes, so misuse of indices silently no-ops rather than throwing.

Source

Thrown at Sorts/QuickSortRecursive.js:26

    do not need any other space to store the auxiliary array and the term
    "partition" denotes that we split the list into two parts one is less
    than the pivot and the other is greater than the pivot and repeats this
    process recursively and breaks the problem into sub-problems and makes
    it singular so that the behavior or "divide and conquer" get involved
    too.

    Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
*/

/**
 * Partition in place QuickSort.
 * @param {number[]} inputList list of values.
 * @param {number} low lower index for partition.
 * @param {number} high higher index for partition.
 */
const quickSort = (inputList, low, high) => {
  if (!Array.isArray(inputList)) {
    throw new TypeError('Please input a valid list or array.')
  }
  if (low < high) {
    // get the partition index.
    const pIndex = partition(inputList, low, high)
    // recursively call the quickSort method again.
    quickSort(inputList, low, pIndex - 1)
    quickSort(inputList, pIndex + 1, high)
  }
  return inputList
}

/**
 * Partition In Place method.
 * @param {number[]} partitionList list for partitioning.
 * @param {number} low lower index for partition.
 * @param {number} high higher index for partition.
 * @returns {number} `pIndex` pivot index value.
 */

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Ensure inputList is an array: if (!Array.isArray(list)) return []; before calling.
  2. Coerce iterables: quickSort(Array.from(set)).
  3. Always pass explicit low=0 and high=list.length-1 to avoid the silent no-op on missing indices.

Example fix

// before
quickSort(data, 0, data.length - 1)

// after
if (Array.isArray(data)) {
  quickSort(data, 0, data.length - 1)
}
Defensive patterns

Strategy: type-guard

Validate before calling

function safeQuickSort(list, low, high) {
  if (!Array.isArray(list)) {
    throw new TypeError('Expected an array')
  }
  return quickSort(list, low ?? 0, high ?? list.length - 1)
}

Type guard

function isNumberArray(v) {
  return Array.isArray(v)
}

Try / catch

try {
  quickSort(list, 0, list.length - 1)
} catch (e) {
  if (e instanceof TypeError && e.message.includes('valid list or array')) {
    return quickSort(Array.from(list), 0, list.length - 1)
  }
  throw e
}

Prevention

When it happens

Trigger: Calling quickSort(null), quickSort(undefined), quickSort('cba'), quickSort(123), or quickSort({0:'c',1:'b',2:'a'}). The most common native failure this preempts is partition() receiving a non-array.

Common situations: Chaining from a function that may return null; spreading a Set/Map without Array.from; JSON field that is sometimes null.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/cba1fd2b4472e25b. Report an issue: GitHub.