<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>冒泡排序</title>
</head>
<body>
<script>
var CArray = function () {
this.dataStore = [10,8,3,2,9,4,5,7];
this.swap = swap;
this.bubbleSort = bubbleSort;
}
function swap(arr,index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
function bubbleSort() {
var data = this.dataStore;
numElements = data.length;
for(var outer = numElements;outer>=2;--outer){
for(var inner = 0;inner<=outer-1;inner++){
if(data[inner]>data[inner+1]){
this.swap(this.dataStore,inner,inner+1)
}
}
}
}
var mynums = new CArray();
mynums.bubbleSort(mynums);
console.log(mynums.dataStore);
</script>
</body>
</html>
得到结果:
分割线
博主为咯学编程:父母不同意学编程,现已断绝关系;恋人不同意学编程,现已分手;亲戚不同意学编程,现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏,金额不限。。。