| C++ sort |
| 1 | void bubbleSort(vector <int> &v, int n) { bool swapped = true; int i = 0; while (i < n - 1 && swapped) { // keep going while we swap in the unordered part swapped = false; for (int j = n - 1; j > i; j--) { // unordered part if (v[j] < v[j - 1]) { swap(v[j], v[j - 1]); swapped = true; } } i++; } } |
| 2 | void insertionSort(vector <int> &v, int n) { int current, pos; for (int i = 1; i < n; i++) { current = v[i]; pos = i; // limit of the ordered part, pos not included // we make space while (pos > 0 && v[pos - 1] > current) { v[pos] = v[pos - 1]; pos--; } // we move the current value to its position if (pos != i) v[pos] = current; } } |
Комментарии