js的排序方法集合

a

  1 Array.prototype.swap = function(i, j)
2 {
3 var temp = this[i];
4 this[i] = this[j];
5 this[j] = temp;
6 }
7 //冒泡排序 时间复杂度O(n^2) 空间复杂度O(1)
8 Array.prototype.bubbleSort = function()
9 {
10 for (var i = this.length - 1; i > 0; --i)
11 {
12 for (var j = 0; j < i; ++j)
13 {
14 if (this[j] > this[j + 1]) this.swap(j, j + 1);
15 }
16 }
17 }
18 //选择排序 时间复杂度O(n^2) 空间复杂度O(1)
19 Array.prototype.selectionSort = function()
20 {
21 for (var i = 0; i < this.length; ++i)
22 {
23 var index = i;
24 for (var j = i + 1; j < this.length; ++j)
25 {
26 if (this[j] < this[index]) index = j;
27 }
28 this.swap(i, index);
29 }
30 }
31 //插入排序 时间复杂度O(n^2) 空间复杂度O(1)
32 Array.prototype.insertionSort = function()
33 {
34 for (var i = 1; i < this.length; ++i)
35 {
36 var j = i, value = this[i];
37 while (j > 0 && this[j - 1] > value)
38 {
39 this[j] = this[j - 1];
40 --j;
41 }
42 this[j] = value;
43 }
44 }
45 //希尔排序
46 Array.prototype.shellSort = function()
47 {
48 for (var step = this.length >> 1; step > 0; step >>= 1)
49 {
50 for (var i = 0; i < step; ++i)
51 {
52 for (var j = i + step; j < this.length; j += step)
53 {
54 var k = j, value = this[j];
55 while (k >= step && this[k - step] > value)
56 {
57 this[k] = this[k - step];
58 k -= step;
59 }
60 this[k] = value;
61 }
62 }
63 }
64 }
65 //快速排序 时间复杂度O(n*log(n))
66 Array.prototype.quickSort = function(s, e)
67 {
68 if (s == null) s = 0;
69 if (e == null) e = this.length - 1;
70 if (s >= e) return;
71 this.swap((s + e) >> 1, e);
72 var index = s - 1;
73 for (var i = s; i <= e; ++i)
74 {
75 if (this[i] <= this[e]) this.swap(i, ++index);
76 }
77 this.quickSort(s, index - 1);
78 this.quickSort(index + 1, e);
79 }
80 //栈快速排序
81 Array.prototype.stackQuickSort = function()
82 {
83 var stack = [0, this.length - 1];
84 while (stack.length > 0)
85 {
86 var e = stack.pop(), s = stack.pop();
87 if (s >= e) continue;
88 this.swap((s + e) >> 1, e);
89 var index = s - 1;
90 for (var i = s; i <= e; ++i)
91 {
92 if (this[i] <= this[e]) this.swap(i, ++index);
93 }
94 stack.push(s, index - 1, index + 1, e);
95 }
96 }
97 //归并排序 时间复杂度O(n*log(n)) 空间复杂度O(n)
98 Array.prototype.mergeSort = function(s, e, b)
99 {
100 if (s == null) s = 0;
101 if (e == null) e = this.length - 1;
102 if (b == null) b = new Array(this.length);
103 if (s >= e) return;
104 var m = (s + e) >> 1;
105 this.mergeSort(s, m, b);
106 this.mergeSort(m + 1, e, b);
107 for (var i = s, j = s, k = m + 1; i <= e; ++i)
108 {
109 b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++];
110 }
111 for (var i = s; i <= e; ++i) this[i] = b[i];
112 }
113 //堆排序 时间复杂度O(n*log(n))
114 Array.prototype.heapSort = function()
115 {
116 for (var i = 1; i < this.length; ++i)
117 {
118 for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1)
119 {
120 if (this[k] >= this[j]) break;
121 this.swap(j, k);
122 }
123 }
124 for (var i = this.length - 1; i > 0; --i)
125 {
126 this.swap(0, i);
127 for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1)
128 {
129 if (k == i || this[k] < this[k - 1]) --k;
130 if (this[k] <= this[j]) break;
131 this.swap(j, k);
132 }
133 }
134 }

冒泡排序,插入排序,选择排序的时间复杂度是O(n^2)
归并排序,堆排序,快速排序的时间复杂度都是O(n*log(n))
空间复杂度冒泡排序,插入排序,选择排序都是O(1)
归并排序为O(n)

原文地址:https://www.cnblogs.com/405464904/p/2167780.html