JavaScript版几种常见排序算法
1.冒泡排序(最慢)
2.选择排序
3.插入排序(比冒泡快)
4.希尔排序
5.归并排序
6.快速排序(非常快的排序方式)
function CArry(numElements){ this.dataStore = []; this.gaps = [5,3,1]; this.pos = 0; this.numElements = numElements; this.toString = toString; this.setData = setData; this.insert = insert; this.clear = clear; this.swap = swap; this.bubbleSort = bubbleSort; this.selectionSort = selectionSort; this.insertionSort = insertionSort; this.shellsort = shellsort; this.setGaps = setGaps; this.mergeSort = mergeSort; this.mergeArrays = mergeArrays; //创建长度为100的数组 for(var i=0;i<numElements;++i){ this.dataStore[i] = i; } } /*-------------------函数--------------------*/ //生成numElements个随机数 function setData(){ for(var i=0;i<this.numElements;++i){ this.dataStore[i] = Math.floor(Math.random() * (this.numElements + 1)); } } //显示dataStore中的随机数,每行显示10个 function toString(){ var str = ""; try{ return this.dataStore.reduce(function(cur,pre,index,item){ if(index>0 && (index+1)%10===0){ return cur + " " + pre + " "; }else{ return cur + " " + pre; } }) }catch(e){ console.log("数组长度为零"); } }; //insert方法() function insert(element){ //this.dataStore[this.pos++] = element; this.dataStore[this.numElements++] = element; } //清空函数 function clear(){ for ( var i = 0; i < this.dataStore.length; ++i ) { this.dataStore[i] = 0; } //这种清空让数组全空,需要在toString里面捕获错误,而上面的是让所有数据为0,但是数组长度不变,不用捕获错误 //this.dataStore.length = 0; } //swap交换函数 function swap(arr,index1,index2){ var temp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = temp; } //冒泡排序(最慢) function bubbleSort(){ var len = this.dataStore.length; for(var i=len;i>=2;i--){ for(var j=0;j<i-1;j++){ if(this.dataStore[j] > this.dataStore[j+1]){ this.swap(this.dataStore,j,j+1); } } } } //选择排序 function selectionSort(){ var min=0; var len = this.dataStore.length; for(var i=0;i<len;i++){ min = i; for(var j=i+1;j<len;j++){ if(this.dataStore[min]>=this.dataStore[j]){ min = j; } } this.swap(this.dataStore,i,min); } } //插入排序 function insertionSort() { var temp; var len = this.dataStore.length; for (var i = 1; i < len; ++i) { temp = this.dataStore[i]; for(var j=i;this.dataStore[j-1]>=temp;j--){ this.dataStore[j] = this.dataStore[j - 1]; } this.dataStore[j] = temp; } } //希尔排序 function shellsort() { 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(this.toString()) } } } //设置gaps间隔序列 function setGaps(arr) { this.gaps = arr; } //归并排序的辅助函数 function mergeArrays(arr, startLeft, stopLeft, startRight, stopRight) { var rightArr = new Array(stopRight - startRight + 1); var leftArr = new Array(stopLeft - startLeft + 1); k = startRight; for (var i = 0; i < (rightArr.length-1); ++i) { rightArr[i] = arr[k]; ++k; } k = startLeft; for (var i = 0; i < (leftArr.length-1); ++i) { leftArr[i] = arr[k]; ++k; } rightArr[rightArr.length-1] = Infinity; leftArr[leftArr.length-1] = Infinity; var m = 0; var n = 0; for (var k = startLeft; k < stopRight; ++k) { if (leftArr[m] <= rightArr[n]) { arr[k] = leftArr[m]; m++; } else { arr[k] = rightArr[n]; n++; } } console.log("left array - ", leftArr); console.log("right array - ", rightArr); } //归并排序 function mergeSort() { if (this.dataStore.length < 2) { return; } var step = 1; var left, right; while (step < this.dataStore.length) { left = 0; right = step; while (right + step <= this.dataStore.length) { mergeArrays(this.dataStore, left, left+step, right, right+step); left = right + step; right = left + step; } if (right < this.dataStore.length) { mergeArrays(this.dataStore, left, left+step, right, this.dataStore.length); } step *= 2; } } //快速排序 function qSort(arr) { if(arr.length === 0) return []; var left = []; var right = []; var pivot = arr[0]; for(var i=1;i<arr.length;i++){ if(arr[i]<pivot) left.push(arr[i]); else right.push(arr[i]); } return qSort(left).concat(pivot,qSort(right)); } //测试 var oCArry = new CArry(10); oCArry.setData(); //oCArry.insert(0); //oCArry.clear(); //console.log(oCArry.dataStore); //console.log(oCArry.toString()); //测试三种排序快慢 /*var start1 = new Date().getTime(); oCArry.bubbleSort(); var stop1 = new Date().getTime(); console.log("bubbleSort时间:"+(stop1-start1)); //305 var start2 = new Date().getTime(); oCArry.selectionSort(); var stop2 = new Date().getTime(); console.log("selectionSort时间:"+(stop2-start2)); //199 var start3 = new Date().getTime(); oCArry.insertionSort(); var stop3 = new Date().getTime(); console.log("selectionSort时间:"+(stop3-start3)); //1*/ /*console.log("希尔排序前:"+oCArry.toString()); console.log("希尔排序中:"+oCArry.shellsort()); console.log("希尔排序后:"+oCArry.toString());*/ /*console.log(oCArry.toString()); oCArry.mergeSort(); console.log(oCArry.toString());*/ console.log(oCArry.toString()); console.log(qSort([oCArry.dataStore]));
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: C++使用变量作为数组长度
- 下一篇: ES6--函数的扩展