You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So, I've pretty much determined that depending on Array.prototype.sort for consistent results is a lost cause. Implementations aren't required to deliver consistent results, which sucks! No biggie, This should do just fine. Initial testing showed that the worst case never went over O^2. Here's the algorithm.
a=[3,4,3,2,6,2,4,6,2,3,4,5,3,2,43,7,87,3,423,344,4,3,2346,1,4,4,45624576245,7,23457,245,72,45,72,457,2,457,245,72,457,245,7,2457,2,1];
compare=function(a,b){
if(a>b)return 1;
};
function sort(array, compare){
var len = array.length;
var max = len*len;
var count=0;
var i=1;
var successfulIndex=i-1;
var regressed=false;
for(;i<len;i++){
if(count===max)break;
count++;
if(compare(array[i-1],array[i])===1){
array.splice(i,0,array.splice(i-1, 1)[0]);
i-=2;
if(i<0)i=0;
regressed=true;
} else if(regressed){
regressed=false;
i=successfulIndex;
} else {
successfulIndex++;
}
}
}
b=Date.now();sort(a,compare);console.log(Date.now()-b);
No description provided.
The text was updated successfully, but these errors were encountered: