// C = A + B, A >= 0, B >= 0 vector<int> add(vector<int> &A, vector<int> &B) { if (A.size() < B.size()) returnadd(B, A); vector<int> C; int t = 0; for (int i = 0; i < A.size(); i ++ ) { t += A[i]; if (i < B.size()) t += B[i]; C.push_back(t % 10); t /= 10; } if (t) C.push_back(t); return C; }
高精度减法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// C = A - B, 满足A >= B, A >= 0, B >= 0 vector<int> sub(vector<int> &A, vector<int> &B) { vector<int> C; for (int i = 0, t = 0; i < A.size(); i ++ ) { t = A[i] - t; if (i < B.size()) t -= B[i]; C.push_back((t + 10) % 10); if (t < 0) t = 1; else t = 0; }
// C = A * b, A >= 0, b > 0 vector<int> mul(vector<int> &A, int b) { vector<int> C; int t = 0; for (int i = 0; i < A.size() || t; i ++ ) { if (i < A.size()) t += A[i] * b; C.push_back(t % 10); t /= 10; } return C; }
高精度除以低精度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// A / b = C ... r, A >= 0, b > 0 vector<int> div(vector<int> &A, int b, int &r) { vector<int> C; r = 0; for (int i = A.size() - 1; i >= 0; i -- ) { r = r * 10 + A[i]; C.push_back(r / b); r %= b; } reverse(C.begin(), C.end()); while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
排序
冒泡排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream> #include<vector>
bool swap_check = true; // Bubble Sorting for (int i = 0; (i < n) && (swap_check); i++) { swap_check = false; for (int j = 0; j < n - 1 - i; j++) { if (numbers[j] > numbers[j + 1]) { swap_check = true; std::swap(numbers[j], numbers[j + 1]); // by changing swap location. // I mean, j. If the number is // greater than j + 1, then it // means the location. } } }
/** \brief * Insertion Sort Function * * @tparam T type of array * @param [in,out] arr Array to be sorted * @param n Size of Array */ template <typename T> voidinsertionSort(T *arr, int n){ for (int i = 1; i < n; i++) { T temp = arr[i]; int j = i - 1; while (j >= 0 && temp < arr[j]) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = temp; } }
/** Insertion Sort Function * * @tparam T type of array * @param [in,out] arr pointer to array to be sorted */ template <typename T> voidinsertionSort(std::vector<T> *arr){ size_t n = arr->size();
for (size_t i = 1; i < n; i++) { T temp = arr[0][i]; int32_t j = i - 1; while (j >= 0 && temp < arr[0][j]) { arr[0][j + 1] = arr[0][j]; j--; } arr[0][j + 1] = temp; } }
选择排序
1
快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14
voidquick_sort(int q[], int l, int r) { if (l >= r) return; int i = l - 1, j = r + 1, x = q[l]; while (i < j) { do i ++ ; while (q[i] < x); do j -- ; while (q[j] > x); if (i < j) swap(q[i], q[j]); elsebreak; } quick_sort(q, l, j), quick_sort(q, j + 1, r); }
归并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidmerge_sort(int q[], int l, int r) { if (l >= r) return; int mid = l + r >> 1; merge_sort(q, l, mid); merge_sort(q, mid + 1, r); int k = 0, i = l, j = mid + 1; while (i <= mid && j <= r) if (q[i] < q[j]) tmp[k ++ ] = q[i ++ ]; else tmp[k ++ ] = q[j ++ ]; while (i <= mid) tmp[k ++ ] = q[i ++ ]; while (j <= r) tmp[k ++ ] = q[j ++ ]; for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j]; }
// C++ program to sort an array using bucket sort #include<algorithm> #include<iostream> #include<vector>
// Function to sort arr[] of size n using bucket sort voidbucketSort(float arr[], int n){ // 1) Create n empty buckets std::vector<float> *b = new std::vector<float>[n];
// 2) Put array elements in different buckets for (int i = 0; i < n; i++) { int bi = n * arr[i]; // Index in bucket b[bi].push_back(arr[i]); }
// 3) Sort individual buckets for (int i = 0; i < n; i++) std::sort(b[i].begin(), b[i].end());
// 4) Concatenate all buckets into arr[] int index = 0; for (int i = 0; i < n; i++) for (int j = 0; j < b[i].size(); j++) arr[index++] = b[i][j]; delete[] b; }