{"ScriptPreparationCode":"function quickselect_median(arr) {\r\n const L = arr.length, halfL = L/2;\r\n if (L % 2 == 1)\r\n return quickselect(arr, halfL);\r\n else\r\n return 0.5 * (quickselect(arr, halfL - 1) \u002B quickselect(arr, halfL));\r\n}\r\n\r\nfunction quickselect(arr, k) {\r\n // Select the kth element in arr\r\n // arr: List of numerics\r\n // k: Index\r\n // return: The kth element (in numerical order) of arr\r\n if (arr.length == 1)\r\n return arr[0];\r\n else {\r\n const pivot = arr[0];\r\n const lows = arr.filter((e)=\u003E(e\u003Cpivot));\r\n const highs = arr.filter((e)=\u003E(e\u003Epivot));\r\n const pivots = arr.filter((e)=\u003E(e==pivot));\r\n if (k \u003C lows.length) // the pivot is too high\r\n return quickselect(lows, k);\r\n else if (k \u003C lows.length \u002B pivots.length)// We got lucky and guessed the median\r\n return pivot;\r\n else // the pivot is too low\r\n return quickselect(highs, k - lows.length - pivots.length);\r\n }\r\n}","TestCases":[{"Name":"arrTest1","Code":"[13, 13, 14, 14, 14, 15, 15, 16]","IsDeferred":false},{"Name":"arrTest2","Code":"[7,3,5, 2300,5,4,0,123,2,76,768,28]","IsDeferred":false}]}