Từ Trường THPT chuyên Thoại Ngọc Hầu, An Giang
Thông tin
void quickSort(int a[], int left, int right) { int i = left; int j = right; int pivot = a[left + (right - left) / 2];
while (i <= j) {
while (a[i] < pivot) i++;
while (a[j] > pivot) j--;
if (i <= j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
if (left < j) {
quickSort(a, left, j);
}
if (i < right) {
quickSort(a, i, right);
}
}
void bubbleSort(int a[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { // Hoán đổi hai phần tử liền kề int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } }
void factors(long n, long pr[], long e[], long& np) { long p = 2, c = 0; np = 0; double sqrtN = sqrt(n); while(n > 1) { if(n % p == 0) { if(c == 0) { pr[np] = p; np++; } c++; e[np - 1] = c; n /= p; } else { p++; c = 0; if(p > sqrtN) { pr[np] = n; e[np] = 1; np++; break; } } } }
int ucln(int a, int b) { while (b != 0) { int r = a % b; a = b; b = r; }
return a;
}
void sapxeptang(int a[][7], int d, int c) { for (int i = 0; i < d * c; i++) { for (int j = 0; j < d * c; j++) { if (a[i / c][i % c] < a[j / c][j % c]) { int tmp = a[i / c][i % c]; a[i / c][i % c] = a[j / c][j % c]; a[j / c][j % c] = tmp; } } } }