voidshellSort(vector<int> &v){ int h = 0; while (h < v.size() / 3) h = h * 3 + 1; while (h >= 1) { for (int j = h; j < v.size(); ++j) { for (int i = j; i >= h && v[i] < v[i - h]; i -= h) swap(v[i], v[i - h]); } h /= 3; } }
bottom-up sort
bottom-up sort is a down-top method based on merge sort.
voidquickSort(vector<int> &nums, int b, int e){ if (b >= e) return;
// if element too little, insertSort more effective than quickSort if (e - b < QS_MIN_LENGTH) insertSort(nums, b, e);
// meduim of three can get more effetive pivot int m = (b + e) / 2; if (nums[b] > nums[m]) swap(nums[b], nums[m]); if (nums[b] > nums[e]) swap(nums[b], nums[e]); if (nums[m] > nums[e]) swap(nums[m], nums[e]);
int pivot = nums[m]; // skip elements equal pivot int lt = b + 1, eq = lt, gt = e - 1; while (eq <= gt) { if (nums[eq] < pivot) swap(nums[lt++], nums[eq++]); elseif (nums[eq] > pivot) swap(nums[eq], nums[gt--]); else eq++; }