JavaScript实践数据结构和算法——选择排序
原创约 268 字小于 1 分钟...
JavaScript实践数据结构和算法——选择排序
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选择排序</title>
</head>
<body>
<script>
var CArray = function () {
this.dataStore = [1,8,3,2,9,5,4,7];
this.swap = swap;
this.selectSort = selectSort;
}
function swap(arr,index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
function selectSort() {
var min;
for(var outer=0;outer<this.dataStore.length-2;++outer){
min = outer;
for(var inner = outer+1;inner<=this.dataStore.length-1;++inner){
if(this.dataStore[inner]<this.dataStore[min]){
min = inner;
}
}
this.swap(this.dataStore,outer,min);
}
}
var mynums = new CArray();
mynums.selectSort(mynums);
console.log(mynums.dataStore);
</script>
</body>
</html>
- 得到结果:

分割线
相关信息
以上就是我关于 JavaScript实践数据结构和算法——选择排序 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。
Powered by Waline v2.15.4