SoFunction
Updated on 2025-03-04

JS commonly used sorting implementation code


<script>
= function(i, j)
{
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}

= function()
{
for (var i = - 1; i > 0; --i)
{
for (var j = 0; j < i; ++j)
{
if (this[j] > this[j + 1]) (j, j + 1);
}
}
}

= function()
{
for (var i = 0; i < ; ++i)
{
var index = i;
for (var j = i + 1; j < ; ++j)
{
if (this[j] < this[index]) index = j;
}
(i, index);
}
}

= function()
{
for (var i = 1; i < ; ++i)
{
var j = i, value = this[i];
while (j > 0 && this[j - 1] > value)
{
this[j] = this[j - 1];
--j;
}
this[j] = value;
}
}

= function()
{
for (var step = >> 1; step > 0; step >>= 1)
{
for (var i = 0; i < step; ++i)
{
for (var j = i + step; j < ; j += step)
{
var k = j, value = this[j];
while (k >= step && this[k - step] > value)
{
this[k] = this[k - step];
k -= step;
}
this[k] = value;
}
}
}
}

= function(s, e)
{
if (s == null) s = 0;
if (e == null) e = - 1;
if (s >= e) return;
((s + e) >> 1, e);
var index = s - 1;
for (var i = s; i <= e; ++i)
{
if (this[i] <= this[e]) (i, ++index);
}
(s, index - 1);
(index + 1, e);
}

= function()
{
var stack = [0, - 1];
while ( > 0)
{
var e = (), s = ();
if (s >= e) continue;
((s + e) >> 1, e);
var index = s - 1;
for (var i = s; i <= e; ++i)
{
if (this[i] <= this[e]) (i, ++index);
}
(s, index - 1, index + 1, e);
}
}

= function(s, e, b)
{
if (s == null) s = 0;
if (e == null) e = - 1;
if (b == null) b = new Array();
if (s >= e) return;
var m = (s + e) >> 1;
(s, m, b);
(m + 1, e, b);
for (var i = s, j = s, k = m + 1; i <= e; ++i)
{
b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++];
}
for (var i = s; i <= e; ++i) this[i] = b[i];
}

= function()
{
for (var i = 1; i < ; ++i)
{
for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1)
{
if (this[k] >= this[j]) break;
(j, k);
}
}
for (var i = - 1; i > 0; --i)
{
(0, i);
for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1)
{
if (k == i || this[k] < this[k - 1]) --k;
if (this[k] <= this[j]) break;
(j, k);
}
}
}

function generate()
{
var max = parseInt(), count = parseInt();
if (isNaN(max) || isNaN(count))
{
alert("The number and maximum values ​​must be an integer");
return;
}
var array = [];
for (var i = 0; i < count; ++i) ((() * max));
= ("\n");
= "";
}

function demo(type)
{
var array = == "" ? [] : ().split("\n");
for (var i = 0; i < ; ++i) array[i] = parseInt(array[i]);
var t1 = new Date();
eval("array." + type + "Sort()");
var t2 = new Date();
= () - ();
= ("\n");
}
</script>

<body onload=generate()>
<table style="width:100%;height:100%;font-size:12px;font-family:An An">
<tr>
<td align=right>
<textarea id=txtInput readonly style="width:100px;height:100%"></textarea>
</td>
<td width=150 align=center>
Random number <input id=txtCount value=500 style="width:50px"><br><br>
Maximum random number <input id=txtMax value=1000 style="width:50px"><br><br>
<button onclick=generate()>Regenerate</button><br><br><br><br>
Time taken (milliseconds): <label id=lblTime></label><br><br><br><br>
<button onclick=demo("bubble")>Bubble sort</button><br><br>
<button onclick=demo("selection")>Select sort</button><br><br>
<button onclick=demo("insertion")>Insert sort</button><br><br>
<button onclick=demo("shell")>Shell sort</button><br><br>
<button onclick=demo("quick")>Quick sort (recursive)</button><br><br>
<button onclick=demo("stackQuick")>Quick sort (stack)</button><br><br>
<button onclick=demo("merge")>Merge sort</button><br><br>
<button onclick=demo("heap")>Heap sort</button><br><br>
</td>
<td align=left>
<textarea id=txtOutput readonly style="width:100px;height:100%"></textarea>
</td>
</tr>
</table>
</body>