<html>
<head>
<title>选择排序</title>
<script type="text/javascript">
/* sort a[1..N], NB. 1 to N */
/* invariant: a[1..i-1] sorted and elements a[1..i-1] <= a[i..N] */
/* find smallest in a[i..N] */
/* a[smallest] is the least element in a[i..j-1] */
/* a[smallest] is the least element in a[i..j] */
/*swap*/
/* a[1..i] sorted and elements a[1..i] <= arr[i+1..N] */
/* a[1..N-1] sorted and elements a[1..N-1] <= a[N] */
/* i.e. a[1..N] sorted. */
function selection(arr)
{
var i, j, smallest, aSmallest, temp;
for(i=0; i < arr.length; i++){
smallest = i;
aSmallest = arr[i];
for(j=i+1; j <=arr.length; j++)
if(arr[j] < aSmallest){ smallest=j; aSmallest=arr[j]; }
temp=arr[i]; arr[i]=arr[smallest]; arr[smallest]=temp;
}
return arr;
}
var tmparr=selection([112,48,39,283,568,293,501,479,06,92]);
if(tmparr!=null)document.write(tmparr.join(",").toString());
</script>
</head>
<body>
</body>
</html>
11-07
11-07