Bubble is the most common algorithm to sort an array. It based on the idea of repeatedly comparing adjacent element and then swapping their value if exist in the wrong order
1 2 3 4 5 6 7 8
voidbubbleSort(vector<int> &nums){ for (int i = 0; i < nums.size(); ++i) { for (int j = i + 1; j < nums.size(); ++j) { if (nums[i] > nums[j]) swap(nums[i], nums[j]); } } }
Selection sort
The selection sort is based on finding the minimum or maximum in an unsorted array and putting it to a sorted array
1 2 3 4 5 6 7 8 9 10
voidselectionSort(vector<int> &nums){ for (int i = 0; i < nums.size(); ++i) { intmin = i; for (int j = i + 1; j < nums.size(); ++j) { if (nums[min] > nums[j]) min = j; } swap(nums[i], nums[min]); } }
Insertion sort
From left to right, find each element’s correct position
1 2 3 4 5 6 7 8 9 10 11 12 13
voidinsertSort(vector<int> &nums){ for (int i = 1; i < nums.size(); ++i) { int v = nums[i]; int j = i; while(j > 0) { nums[j] = nums[j - 1]; if (nums[j] <= v) break; --j; } nums[j] = v; } }
Merge sort
Merge sort is a divide-and-conquer algorithm based on repeatedly breaking down an array to two sub-array and then merge them
voidmerge(vector<int> &nums, int b, int e){ int m = (b + e) / 2; int q = m + 1, p = b; vector<int> v; for (int i = b; i <= e; ++i) { if (p > m) v.push_back(nums[q++]); elseif (q > e) v.push_back(nums[p++]); elseif (nums[q] > nums[p]) v.push_back(nums[p++]); else v.push_back(nums[q++]); }
for (auto val : v) nums[b++] = val; }
voidmergeSort(vector<int> &nums, int b, int e){ if (b >= e) return;
mergeSort(nums, b, (e + b) / 2); mergeSort(nums, (e + b) / 2 + 1, e); merge(nums, b, e); }
Quick sort
Quick sort is also a divide-and-conquer algorithm. But it reduces the space complexity and removes the use of auxiliary array that is used in merger sort One of the most important factors to influence performance is the pivot. I chose the pivot from the middle, front, and back of the array. Sometimes will improve performance
intpartion(vector<int> &nums, int b, int e){ int p = e; 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 piv = nums[m]; swap(nums[e--], nums[m]); while (b < e) { while (b < e && nums[b] < piv) ++b; while (b < e && nums[e] >= piv) --e; if (b >= e) break;
swap(nums[b], nums[e]); } swap(nums[p], nums[b]);
return b; }
voidquickSort(vector<int> &nums, int b, int e){ if (b >= e) return;
int p = partion(nums, b, e); quickSort(nums, b, p - 1); quickSort(nums, p + 1, e); }
Heap sort
Heap sort uses a structure called heap to sort the array. Heap is a complete binary tree. left sub-tree index = 2 * root index + 1 right sub-tree index = 2 * root index + 2