算法分析-前七章排序算法全实现,为第八章线性排序使用。

  1 var A = [6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2];
  2 
  3 Array.prototype.swap = function (i, j) {
  4     var temp = this[j];
  5     this[j] = this[i];
  6     this[i] = temp;
  7 };
  8 
  9 Array.prototype.getMaxVal = function () {
 10     //可以用任意我们学过的排序方法写,我们学过冒泡,选择,归并,快排,堆排序。
 11     //当然这样我们只要取出最大值即可,不需要全部排序,但是为了学程序,我将给出我们已经学过的所有排序方法,并全部排序
 12     var r = this.length - 1;
 13     //   return this.BUBBLE_SORT()[r];
 14     //    return this.SELECT_SORT()[r];
 15     //    return this.QUICK_SORT(0, r)[r];
 16     //heap_size = r+1; return.this.HRAP_SORT()[r];
 17     // return this.MERGE_SORT(0,r)[r];
 18     return this.INSERT_SORT()[r];
 19 };
 20 //冒泡
 21 Array.prototype.BUBBLE_SORT = function () {
 22 
 23     //冒泡就是两两将换,
 24     for (var i = this.length - 1; i > 0; i--) {
 25         for (var j = 0; j < i; j++) {
 26             if (this[j] > this[j + 1]) {
 27                 this.swap(j, j + 1);
 28             }
 29         }
 30     }
 31     return this;
 32 };
 33 
 34 
 35 //选择排序
 36 
 37 Array.prototype.SELECT_SORT = function () {
 38     for (var i = 0; i < this.length - 1; i++) {
 39         var min = this[i];  //默认选择第一个最小;
 40         var index = i;  //记录最小值的坐标
 41 
 42         for (var j = i + 1; j < this.length; j++) {
 43             if (this[j] < min) {
 44                 min = this[j];
 45                 index = j;
 46             }
 47         }
 48         this.swap(index, i);
 49     }
 50     return this;
 51 };
 52 
 53 
 54 //随机算法-快排
 55 //思想就是分治,递归。
 56 //最主要的函数就是PARTITION,返回下标i,i的左边都比A[i]小,右边比他大;
 57 Array.prototype.PARTITION = function (p, r) {
 58     var i = p - 1;
 59     var x = this[r];
 60     for (var j = p; j <= r - 1; j++) {
 61         if (this[j] < x) {
 62             this.swap(++i, j);
 63 
 64         }
 65 
 66     }
 67     this.swap(++i, r);
 68     return i;
 69 };
 70 
 71 Array.prototype.RANDOM_PARTION = function (p, r) {
 72 
 73     var q = p + Math.round(Math.random() * (r - p));
 74     this.swap(q, r);
 75     return this.PARTITION(p, r);
 76 };
 77 
 78 Array.prototype.QUICK_SORT = function (p, r) {
 79     if (r > p) {
 80         var q = this.RANDOM_PARTION(p, r);
 81         arguments.callee.call(this, p, q - 1);
 82         arguments.callee.call(this, q + 1, r);
 83     }
 84     return this;
 85 };
 86 
 87 
 88 //堆排序
 89 //其核心函数在于MAX_HRAPTITY,总能让i位置是最大的。
 90 var heap_size = 0; //包含heap_size下标在内的,之后的下标都是排序好的。
 91 
 92 Array.prototype.HEAP_SORT = function () {
 93     this.BUILD_MAX_HEAP();
 94     while (heap_size > 1) {
 95         this.swap(0, --heap_size);
 96         this.MAX_HEAPTIFY(0);
 97     }
 98     heap_size = this.length; //最后复原
 99     return this;
100 };
101 
102 //生成最大堆
103 Array.prototype.BUILD_MAX_HEAP = function () {
104 
105     for (var i = Math.floor(this.length / 2) - 1; i >= 0; i--) {
106         this.MAX_HEAPTIFY(i);
107     }
108 };
109 
110 //生成以i为根节点,它为最大值
111 Array.prototype.MAX_HEAPTIFY = function (i) {
112     var largest = i;
113     var l = i * 2 + 1;
114     var r = i * 2 + 2;
115 
116     if (l < heap_size && this[l] > this[largest]) {
117 
118         largest = l;
119     }
120 
121     if (r < heap_size && this[r] > this[largest]) {
122 
123         largest = r;
124     }
125     if (largest !== i) {
126         this.swap(largest, i);
127         arguments.callee.call(this, largest);
128     }
129 };
130 
131 //heap_size = A.length;
132 //归并排序
133 //其核心思想是分治,递归
134 
135 Array.prototype.MERGE_SORT = function (start, end) {
136 
137     if (end > start) { //记住这里没有等号,不然会一直递归下去。
138         var middle = Math.floor((start + end) / 2);
139         arguments.callee.call(this, start, middle);
140         arguments.callee.call(this, middle + 1, end);
141         this.MEAGE(start, middle, end);
142     }
143     return this;
144 };
145 
146 Array.prototype.MEAGE = function (s, m, e) {
147     var A = this.slice(s, m + 1),
148         B = this.slice(m + 1, e + 1),
149         lenA = m + 1 - s,
150         lenB = e - m,
151         i = 0,
152         j = 0;
153 
154     while (i < lenA && j < lenB) {
155         A[i] < B[j] ? this[s++] = A[i++] : this[s++] = B[j++];
156     }
157     while (i < lenA) {
158         this[s++] = A[i++];
159     }
160     while (j < lenB) {
161         this[s++] = B[j++];
162     }
163 };
164 
165 
166 //插入排序
167 Array.prototype.INSERT_SORT = function () {
168     try {
169         if (this.length < 1) {
170             throw new Error("错误长度的数组");
171         }
172         for (var i = 1; i < this.length; i++) {
173             var j = i - 1,
174                 temp = this[i];
175             while (this[j] > temp && j >= 0) {
176                 this[j + 1] = this[j];
177                 j--;
178             }
179             this[j + 1] = temp;
180         }
181 
182     } catch (e) {
183         console.error(e);
184     } finally {
185         return this;
186     }
187 };

 

posted @ 2016-09-26 22:23  hdu胡恩超  阅读(214)  评论(0)    收藏  举报