[JS]js的函数排序之选择排序
[JS]js的函数排序之选择排序
[JS]js的函数排序之选择排序
- function SelectionSort(array) {
- var length = array.length;
- for (var i = 0; i < length; i++) { //缩小选择的范围
- var min = array[i]; //假定范围内第一个为最小值
- var iindex = i; //记录最小值的下标
- for (var j = i + 1; j < length; j++) { //在范围内选取最小值
- if (array[j] < min) {
- min = array[j];
- index = j;
- }
- }
- if (index != i) { //把范围内最小值交换到范围内第一个
- var temp = array[i];
- array[i] = array[index];
- array[index] = temp;
- }
- console.log(array);
- console.log("---------------------");
- }
- return array;
- }
- var arr = [ 1, 10, 100, 90, 65, 5, 4, 10, 2, 4 ];
- var result = SelectionSort(arr);
- console.log(result);
- /*
- [ 1, 10, 100, 90, 65, 5, 4, 10, 2, 4 ]
- ---------------------
- [ 1, 2, 100, 90, 65, 5, 4, 10, 10, 4 ]
- ---------------------
- [ 1, 2, 4, 90, 65, 5, 100, 10, 10, 4 ]
- ---------------------
- [ 1, 2, 4, 4, 65, 5, 100, 10, 10, 90 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 65, 100, 10, 10, 90 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 10, 100, 65, 10, 90 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 10, 10, 65, 100, 90 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 10, 10, 65, 100, 90 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]
- ---------------------
- [ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]
- */
热门文章推荐
- [JS]window.location获取url各项参数详解
- [JS]jQuery,javascript获得网页的高度和宽度
- [JS]视频弹窗视频弹出层videoLightBox(含三种播放器的用法)
- [JS]JS提交中文encodeURI两次转码
- [JS]js版方面encodeURI转码和decodeURI解码的用法实例
- [JS]js取当前机子的时间戳实例
- [JS]AES加密(基于crypto-js)PHP后端解密
- [JS]data:image/png;base64写法的用途及说明
请稍候...