{"record":{"id":"cba1fd2b4472e25b","repo":"TheAlgorithms/JavaScript","slug":"please-input-a-valid-list-or-array","errorCode":null,"errorMessage":"Please input a valid list or array.","messagePattern":"Please input a valid list or array\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Sorts/QuickSortRecursive.js","lineNumber":26,"sourceCode":"    do not need any other space to store the auxiliary array and the term\n    \"partition\" denotes that we split the list into two parts one is less\n    than the pivot and the other is greater than the pivot and repeats this\n    process recursively and breaks the problem into sub-problems and makes\n    it singular so that the behavior or \"divide and conquer\" get involved\n    too.\n\n    Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html\n*/\n\n/**\n * Partition in place QuickSort.\n * @param {number[]} inputList list of values.\n * @param {number} low lower index for partition.\n * @param {number} high higher index for partition.\n */\nconst quickSort = (inputList, low, high) => {\n  if (!Array.isArray(inputList)) {\n    throw new TypeError('Please input a valid list or array.')\n  }\n  if (low < high) {\n    // get the partition index.\n    const pIndex = partition(inputList, low, high)\n    // recursively call the quickSort method again.\n    quickSort(inputList, low, pIndex - 1)\n    quickSort(inputList, pIndex + 1, high)\n  }\n  return inputList\n}\n\n/**\n * Partition In Place method.\n * @param {number[]} partitionList list for partitioning.\n * @param {number} low lower index for partition.\n * @param {number} high higher index for partition.\n * @returns {number} `pIndex` pivot index value.\n */","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Sorts/QuickSortRecursive.js#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"Chaining from a function that may return null; spreading a Set/Map without Array.from; JSON field that is sometimes null.","solutions":["Ensure inputList is an array: if (!Array.isArray(list)) return []; before calling.","Coerce iterables: quickSort(Array.from(set)).","Always pass explicit low=0 and high=list.length-1 to avoid the silent no-op on missing indices."],"exampleFix":"// before\nquickSort(data, 0, data.length - 1)\n\n// after\nif (Array.isArray(data)) {\n  quickSort(data, 0, data.length - 1)\n}","handlingStrategy":"type-guard","validationCode":"function safeQuickSort(list, low, high) {\n  if (!Array.isArray(list)) {\n    throw new TypeError('Expected an array')\n  }\n  return quickSort(list, low ?? 0, high ?? list.length - 1)\n}","typeGuard":"function isNumberArray(v) {\n  return Array.isArray(v)\n}","tryCatchPattern":"try {\n  quickSort(list, 0, list.length - 1)\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('valid list or array')) {\n    return quickSort(Array.from(list), 0, list.length - 1)\n  }\n  throw e\n}","preventionTips":["Always pass explicit low=0 and high=list.length-1 to avoid silent no-ops.","Convert array-like iterables with Array.from before sorting.","Guard against null/undefined from upstream functions."],"tags":["type-check","sort","recursion","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}