{"ScriptPreparationCode":"var diff = (a, b) =\u003E (a - b);\r\nvar bigArray = (new Array(1000)).fill(null).map(() =\u003E Math.floor(Math.random() * 200));\r\n\r\nfunction countSort(originalArray, smallestElement = undefined, biggestElement = undefined) {\r\n // Init biggest and smallest elements in array in order to build number bucket array later.\r\n let detectedSmallestElement = smallestElement || 0;\r\n let detectedBiggestElement = biggestElement || 0;\r\n\r\n if (smallestElement === undefined || biggestElement === undefined) {\r\n originalArray.forEach((element) =\u003E {\r\n // Visit element.\r\n this.callbacks.visitingCallback(element);\r\n\r\n // Detect biggest element.\r\n if (this.comparator.greaterThan(element, detectedBiggestElement)) {\r\n detectedBiggestElement = element;\r\n }\r\n\r\n // Detect smallest element.\r\n if (this.comparator.lessThan(element, detectedSmallestElement)) {\r\n detectedSmallestElement = element;\r\n }\r\n });\r\n }\r\n\r\n // Init buckets array.\r\n // This array will hold frequency of each number from originalArray.\r\n const buckets = Array(detectedBiggestElement - detectedSmallestElement \u002B 1).fill(0);\r\n\r\n originalArray.forEach((element) =\u003E {\r\n // Visit element.\r\n this.callbacks.visitingCallback(element);\r\n\r\n buckets[element - detectedSmallestElement] \u002B= 1;\r\n });\r\n\r\n // Add previous frequencies to the current one for each number in bucket\r\n // to detect how many numbers less then current one should be standing to\r\n // the left of current one.\r\n for (let bucketIndex = 1; bucketIndex \u003C buckets.length; bucketIndex \u002B= 1) {\r\n buckets[bucketIndex] \u002B= buckets[bucketIndex - 1];\r\n }\r\n\r\n // Now let\u0027s shift frequencies to the right so that they show correct numbers.\r\n // I.e. if we won\u0027t shift right than the value of buckets[5] will display how many\r\n // elements less than 5 should be placed to the left of 5 in sorted array\r\n // INCLUDING 5th. After shifting though this number will not include 5th anymore.\r\n buckets.pop();\r\n buckets.unshift(0);\r\n\r\n // Now let\u0027s assemble sorted array.\r\n const sortedArray = Array(originalArray.length).fill(null);\r\n for (let elementIndex = 0; elementIndex \u003C originalArray.length; elementIndex \u002B= 1) {\r\n // Get the element that we want to put into correct sorted position.\r\n const element = originalArray[elementIndex];\r\n\r\n // Visit element.\r\n this.callbacks.visitingCallback(element);\r\n\r\n // Get correct position of this element in sorted array.\r\n const elementSortedPosition = buckets[element - detectedSmallestElement];\r\n\r\n // Put element into correct position in sorted array.\r\n sortedArray[elementSortedPosition] = element;\r\n\r\n // Increase position of current element in the bucket for future correct placements.\r\n buckets[element - detectedSmallestElement] \u002B= 1;\r\n }\r\n\r\n // Return sorted array.\r\n return sortedArray;\r\n}","TestCases":[{"Name":"count sort","Code":"countSort(bigArray)","IsDeferred":false},{"Name":"Native","Code":"bigArray.sort(diff)","IsDeferred":false}]}