insertionSort:function(){ var length = this.array.length; var j; var temp; for(var i = 1; i < length; i++){ temp = this.array[i]; j = i; while(j > 0 && this.array[j - 1] > temp){ this.array[j] = this.array[j - 1]; j--; } this.array[j] = temp; } }
1.1.4 归并排序
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。时间复杂度为O(nlogn),空间复杂度为O(n)。
//快速排序,参考http://www.ruanyifeng.com/blog/2011/04/quicksort_in_javascript.html quickSort:function(){ functionsort(array){ if (array.length <= 1) { return array; } var pivotIndex = Math.floor(array.length/2); var pivot = array.splice(pivotIndex,1)[0]; var left = []; var right = []; for(var i = 0; i < array.length; i++){ if (array[i] < pivot) { left.push(array[i]); }else{ right.push(array[i]); } } return sort(left).concat([pivot],sort(right)); }
functionArrayList() { this.array = []; } ArrayList.prototype = { constructor: ArrayList, insert: function(item) { this.array.push(item); }, toString: function() { returnthis.array.join(); }, swap: function(index1, index2) { var aux = this.array[index2]; this.array[index2] = this.array[index1]; this.array[index1] = aux; }, //冒泡排序 bubbleSort: function() { var length = this.array.length; for (var i = 0; i < length; i++) { for (var j = 0; j < length - 1 - i; j++) { if (this.array[j] > this.array[j + 1]) { this.swap(j, j + 1); } } } }, //选择排序 selectionSort: function() { var length = this.array.length; var indexMin; for (var i = 0; i < length - 1; i++) { indexMin = i; for (var j = i; j < length; j++) { if (this.array[indexMin] > this.array[j]) { indexMin = j; } } if (indexMin !== i) { this.swap(indexMin, i); } } }, //插入排序 insertionSort: function() { var length = this.array.length; var j; var temp; for (var i = 1; i < length; i++) { temp = this.array[i]; j = i; while (j > 0 && this.array[j - 1] > temp) { this.array[j] = this.array[j - 1]; j--; } this.array[j] = temp; } }, //归并排序 mergeSort: function() { functionmergeSortRec(array) { var length = array.length; if (length === 1) { return array; } var mid = Math.floor(length / 2); var left = array.slice(0, mid); var right = array.slice(mid, length); return merge(mergeSortRec(left), mergeSortRec(right)); }
functionmerge(left, right) { var result = []; var il = 0; var ir = 0; while (il < left.length && ir < right.length) { if (left[il] < right[ir]) { result.push(left[il++]); } else { result.push(right[ir++]); } } while (il < left.length) { result.push(left[il++]); } while (ir < right.length) { result.push(right[ir++]); } return result; } this.array = mergeSortRec(this.array); }, //快速排序,参考http://www.ruanyifeng.com/blog/2011/04/quicksort_in_javascript.html quickSort:function(){ functionsort(array){ if (array.length <= 1) { return array; } var pivotIndex = Math.floor(array.length/2); var pivot = array.splice(pivotIndex,1)[0]; var left = []; var right = []; for(var i = 0; i < array.length; i++){ if (array[i] < pivot) { left.push(array[i]); }else{ right.push(array[i]); } } return sort(left).concat([pivot],sort(right)); }