1 console.log("锋叔子的选择排序");
2 //选择排序
3 function selectSort(myArray)
4 {
5 var k = 0;
6 var tmp = 0;
7 //每次循环找出最大的值
8 for(var i = 0; i < myArray.length - 1; i++) {
9 k = i;
10 //找出最大值
11 for(var j = i; j < myArray.length; j++) {
12 if(myArray[j] < myArray[k]) {
13 k = j;
14 }
15 }
16 tmp = myArray[i];
17 myArray[i] = myArray[k];
18 myArray[k] = tmp;
19 }
20 return myArray;
21 }
22
23 //测试排序
24 function testSort()
25 {
26 var myArray=initArray();
27 var myArrayAfterSort=selectSort(myArray);
28 for (var i = 0; i < myArrayAfterSort.length; i++) {
29 console.log(myArrayAfterSort[i]);
30 };
31 }
32 //初始化数组
33 function initArray()
34 {
35 var myArray=[9,3,8,7];
36 return myArray;
37 }
38
39 testSort();
![](//images0.cnblogs.com/blog/425198/201401/06145148-3dbadf4203ea4966a7a1f4a3556a0abd.jpg)