{"ScriptPreparationCode":"function randomNumber(min, max) {\r\n\treturn Math.floor(Math.random() * (max \u002B 1 \u002B Math.abs(min))) \u002B min\r\n}\r\nfunction newArr(lengths = 5, min = 0, max = 10) {\r\n\tlet newArr = []\r\n for (let i = 0; i \u003C lengths; i\u002B\u002B) {\r\n \tnewArr.push(randomNumber(min, max))\r\n }\r\n return newArr\r\n}\r\n\r\n\r\nfunction insertionSort(arr, left, right) {\r\n for (let i = left \u002B 1; i \u003C= right; i\u002B\u002B) {\r\n let temp = arr[i];\r\n let j = i - 1;\r\n while (j \u003E= left \u0026\u0026 arr[j] \u003E temp) {\r\n arr[j \u002B 1] = arr[j];\r\n j--;\r\n }\r\n arr[j \u002B 1] = temp;\r\n }\r\n}\r\n\r\nfunction merge(arr, l, m, r) {\r\n let len1 = m - l \u002B 1;\r\n let len2 = r - m;\r\n let left = new Array(len1);\r\n let right = new Array(len2);\r\n for (let i = 0; i \u003C len1; i\u002B\u002B) left[i] = arr[l \u002B i];\r\n for (let i = 0; i \u003C len2; i\u002B\u002B) right[i] = arr[m \u002B 1 \u002B i];\r\n\r\n let i = 0;\r\n let j = 0;\r\n let k = l;\r\n\r\n while (i \u003C len1 \u0026\u0026 j \u003C len2) {\r\n if (left[i] \u003C= right[j]) {\r\n arr[k] = left[i];\r\n i\u002B\u002B;\r\n } else {\r\n arr[k] = right[j];\r\n j\u002B\u002B;\r\n }\r\n k\u002B\u002B;\r\n }\r\n\r\n while (i \u003C len1) {\r\n arr[k] = left[i];\r\n k\u002B\u002B;\r\n i\u002B\u002B;\r\n }\r\n\r\n while (j \u003C len2) {\r\n arr[k] = right[j];\r\n k\u002B\u002B;\r\n j\u002B\u002B;\r\n }\r\n}\r\n\r\nfunction timSort(arr) {\r\n const MIN_RUN = 32;\r\n const n = arr.length;\r\n\r\n for (let i = 0; i \u003C n; i \u002B= MIN_RUN) {\r\n insertionSort(arr, i, Math.min(i \u002B MIN_RUN - 1, n - 1));\r\n }\r\n\r\n for (let size = MIN_RUN; size \u003C n; size = 2 * size) {\r\n for (let left = 0; left \u003C n; left \u002B= 2 * size) {\r\n let mid = left \u002B size - 1;\r\n let right = Math.min(left \u002B 2 * size - 1, n - 1);\r\n\r\n if (mid \u003C right) {\r\n merge(arr, left, mid, right);\r\n }\r\n }\r\n }\r\n\r\n return arr;\r\n}\r\n\r\nfunction quickSort(arr, left = 0, right = arr.length - 1) {\r\n if (left \u003C right) {\r\n const pivotIndex = partition(arr, left, right);\r\n\r\n quickSort(arr, left, pivotIndex - 1);\r\n quickSort(arr, pivotIndex \u002B 1, right);\r\n }\r\n return arr;\r\n}\r\n\r\nfunction partition(arr, left, right) {\r\n const pivot = arr[right];\r\n let i = left;\r\n\r\n for (let j = left; j \u003C right; j\u002B\u002B) {\r\n if (arr[j] \u003C pivot) {\r\n [arr[i], arr[j]] = [arr[j], arr[i]];\r\n i\u002B\u002B;\r\n }\r\n }\r\n\r\n [arr[i], arr[right]] = [arr[right], arr[i]];\r\n return i;\r\n}","TestCases":[{"Name":"Tim Sort","Code":"const sortedArr = timSort(newArr(200,-5000,5000))","IsDeferred":false},{"Name":"Quick Sort","Code":"const sortedArr = quickSort(newArr(200,-5000,5000))","IsDeferred":false}]}