穩定的快排
阿新 • • 發佈:2017-07-28
div ret cto logs tab ble nbsp turn end
int stable_partition(vector<int>& a, int s, int e) { if (s >= e) return s; vector<int> b(a.size()); int pa1, pa2, pb1, pb2; pa1 = pb1 = s; pa2 = pb2 = e; int start = s, end = e; int key = a[s]; while (pa1 < pa2) { while (pa1 < pa2 && a[pa1] < key) { a[s++]= a[pa1++]; } while (pa1 < pa2 && a[pa2] >= key) { a[e--]= a[pa2--] ; } if (pa1 < pa2) { b[pb1++]= a[pa1++]; b[pb2--]= a[pa2--] ; } } int i; for (i = pb2 + 1; i <= end; i++) a[s++] = b[i];int idx = pa1; for (i = start; i < pb1; i++) a[s++] = b[i]; return idx; }
穩定的快排