image.png
charu.gif
image.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>希爾排序</title>
</head>
<body>
<script>
var CArray = function () {
this.dataStore = [10,8,3,2,5,9,4,7,35,47,20];
this.shellsort = shellsort;
this.gaps = [5,3,1];
this.swap = swap;
this.dynamiSort = dynamiSort;
};
function shellsort(arr,index1,index2) {
for(var g = 0;g<this.gaps.length;g++){
for(var i = this.gaps[g];i<this.dataStore.length;i++){
var temp = this.dataStore[i];
for(var j=i;j>=this.gaps[g]&&this.dataStore[j-this.gaps[g]]>temp;j-=this.gaps[g]){
this.dataStore[j] = this.dataStore[j-this.gaps[g]];
}
this.dataStore[j] = temp;
}
console.log("調(diào)換后",this.dataStore);
}
}
//交換數(shù)組
function swap(arr,index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
function dynamiSort() {
var N = this.dataStore.length;
var h = 1;
while (h<N/3){
h =3*h+1;
}
while (h>=1){
for(var i = h;i<N;i++){
for(var j = i;j>=h && this.dataStore[j]<this.dataStore[j-h];j=j-h){
this.swap(this.dataStore,j,j-h);
}
}
h = (h-1)/3;
}
}
var mynums = new CArray();
// mynums.shellsort();
mynums.dynamiSort();
console.info(mynums.dataStore);
</script>
</body>
</html>
得到結(jié)果:
image.png
分割線
博主為咯學編程:父母不同意學編程立叛,現(xiàn)已斷絕關系;戀人不同意學編程负敏,現(xiàn)已分手;親戚不同意學編程,現(xiàn)已斷絕來往;老板不同意學編程,現(xiàn)已失業(yè)三十年囚巴。原在。。彤叉。庶柿。。如果此博文有幫到你歡迎打賞秽浇,金額不限浮庐。。。