A program execution session includes a built-in clock, so why not use the built-in clock as a defacto-sort (for long-integers less than 2^64)
By assigning each element into an associative hash table, the algorithm could sort the entire array in O(2N)
In lieu of direct-access to this, this example implementation substitutes a while-loop
let myUnsortedArray = [0,0,192,1928329,48,37]
function ClockSort(myUnsortedArray){
let MAXVAL = --(2 ** 64)
let myNewSortedArray = []
let mySortedArrayCache = {}
myUnsortedArray.map((val,idx,me)=>{
if(val in mySortedArrayCache) mySortedArrayCache[val]++
else (MAXVAL - val > 0) ? (mySortedArrayCache[val] = 1) : Error(`${val} exceeds 2^64 (MAX VALUE ${MAXVAL})!`) ;
return val
}) ;
let i = 0
let I = myUnsortedArray.length
let clk = 0
while(i<I){
mySortedArrayCache[clk] && (myNewSortedArray.push(clk)) && (i++) && (--clk)
clk++
}
return myNewSortedArray
}
I'm not sure how a clock would replace that while loop, could you expound? My understanding of a CPU clock, is that it's meant to ensure instructions run on a set interval. It's a very low-level concept, that I'm not really sure you can get direct access to, even from an assembly program.
As it is currently implemented, this looks like pideonhole sorting, which is a sorting algorithm that's really only suitable when the number of elements you want to sort is around the same as the number of possible elements that could exist. So, this sort, as it currently stands, isn't O(2N)
, it's O(N+M)
, where M is the number of possible numbers (i.e. 2^64), which is an unacceptable amount constant time for most use cases. I'm not sure how you envisioned this while loop getting replaced with clock logic, but I doubt there's any way to replace it that would reduce this constant factor to appropriate levels.
I'm not sure what you mean by a clock here, but anything that exposes a non-deterministic source such as a wall clock, or other counter influenced by execution external to the JS program is not suitable for use in an implicit and effectively undeniable API.