比較しないソートの作成
Posted feedbacks - JavaScript
JavaScript。クイックソート、かな?
効率悪そう。
効率悪そう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Array.prototype.sort2 = function(minVal, maxVal) {
if (this.length <= 1) return this;
var mid = (minVal + maxVal) / 2;
var lt = [], eq = [], gt = [];
for (var i=0; i < this.length; i++) {
if (this[i] > mid)
gt.push(this[i]);
else if (this[i] < mid)
lt.push(this[i]);
else
eq.push(this[i]);
}
return lt.sort2(minVal, mid).concat(eq, gt.sort2(mid, maxVal));
};
[-1,9,4,8,9,6,3,9,5,2].sort2(-1,10)
|
バケツソート。無理矢理感有り。
JScript(WSH)。
-------------------------------
C:\temp>cscript /nologo bucketsort.js
-1 2 3 4 5 6 8 9 9 9
JScript(WSH)。
-------------------------------
C:\temp>cscript /nologo bucketsort.js
-1 2 3 4 5 6 8 9 9 9
1 2 3 4 5 6 7 | function bucketsort(data,min,max,size){
var result=[];
eval( data.replace(/(-?\d+)/g,'result[$1-min]+=",$1,";') );
return result.join('').split(/[^0-9-]+/).join(' ');
}
WScript.Echo(bucketsort('-1 9 4 8 9 6 3 9 5 2','-1','10','10'));
|


sweetie089 #6628() Rating3/3=1.00
[ reply ]