《数据结构与算法分析——c语言描述》 第七章
插入排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; void insertionSort(int *a, int n) { int j, p; int temp; for (p = 1; p < n; p++) { temp = a[p]; for (j = p; j > 0 && temp < a[j - 1]; j--) a[j] = a[j - 1]; a[j] = temp; } } #define N 1000 int main() { getRandomInt(a, N); insertionSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("ERROR"); }
希尔排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; void shellSort(int *a, int n) { int i, j, increment; int temp; for (increment = n / 2; increment > 0; increment /= 2) { for (i = increment; i < n; i++) { temp = a[i]; for (j = i; j >= increment; j -= increment) { if (temp < a[j - increment]) a[j] = a[j - increment]; else break; } a[j] = temp; } } } #define N 10000000 int main() { getRandomInt(a, N); shellSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("ERROR"); }
堆排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; #define leftChild(i) (2*(i)+1) void percDown(int *a, int i, int n) { int child = leftChild(i); int temp; for (temp = a[i]; leftChild(i) < n; i=child) { child = leftChild(i); if (child != n - 1 && a[child] < a[child + 1]) child++; if (temp < a[child]) a[i] = a[child]; else break; } a[i] = temp; } void heapSort(int *a, int n) { for (int i = (n-1) / 2; i >= 0; i--) { percDown(a, i, n); } for (int i = n - 1; i > 0; i--) { int temp = a[0]; a[0] = a[i]; a[i] = temp; percDown(a, 0, i); } } #define N 10000 int main() { getRandomInt(a, N); heapSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("error"); }
归并排序
#include<stdlib.h> #include<stdio.h> #include"fatal.h" int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; void merge(int a[], int tempArray[], int left, int right, int rightEnd) { int i, j, k; int leftEnd; for (i = left, j = right, k = left, leftEnd = right - 1; i <= leftEnd && j <= rightEnd; k++) { if (a[i] < a[j]) { tempArray[k] = a[i]; i++; } else { tempArray[k] = a[j]; j++; } } while (i <= leftEnd) { tempArray[k++] = a[i++]; } while (j <= rightEnd) { tempArray[k++] = a[j++]; } for (int i = left; i <= rightEnd; i++) a[i] = tempArray[i]; } void mSort(int a[], int tempArray[], int left, int right) { if (left < right) { int center = (left + right) / 2; mSort(a, tempArray, left, center); mSort(a, tempArray, center + 1, right); merge(a, tempArray, left, center + 1, right); } } void mergeSort(int a[], int n) { int *tempArray = malloc(sizeof(int)*n); if (tempArray == NULL) Error("OUT OF MEMORY"); mSort(a, tempArray, 0, n - 1); } #define N 10000000 int main() { getRandomInt(a, N); mergeSort(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else Error("error"); /*printf("%d ", a[i]);*/ }
快速排序
#include<stdlib.h> #include<stdio.h> int RandInt(int i, int j) { int temp; temp = (int)(i + (1.0*rand() / RAND_MAX)*(j - i)); return temp; } void getRandomInt(int *A, int n) { for (int i = 0; i < n; i++) { A[i] = i + 1; } for (int i = 1; i < n; i++) { //std::swap(A[i], A[RandInt(0, i)]); int randAdrr = RandInt(0, i); int t = A[i]; A[i] = A[randAdrr]; A[randAdrr] = t; } } int a[99999999]; typedef int ElementType; void insertionSort(int *a, int n) { int j, p; int temp; for (p = 1; p < n; p++) { temp = a[p]; for (j = p; j > 0 && temp < a[j - 1]; j--) a[j] = a[j - 1]; a[j] = temp; } } void swap_my(ElementType *a, ElementType *b) { ElementType temp; temp = *a; *a = *b; *b = temp; } ElementType median3(ElementType a[], int left, int right) { int center = (left + right) / 2; if (a[left] > a[center]) swap_my(&a[left], &a[center]); if (a[left] > a[right]) swap_my(&a[left], &a[right]); if (a[center] > a[right]) swap_my(&a[center], &a[right]); swap_my(&a[center], &a[right - 1]); return a[right - 1]; } #define CUTOFF (3) void qsort_mya(ElementType a[], int left, int right) { if (left + CUTOFF <= right) { int i, j; ElementType pivot; pivot = median3(a, left, right); i = left; j = right - 1; while (1) { while (a[++i] < pivot) {} while (a[--j] > pivot) {} if (i < j) swap_my(&a[i], &a[j]); else break; } swap_my(&a[i], &a[right - 1]); qsort_mya(a, left, i - 1); qsort_mya(a, i + 1, right); } else insertionSort(a + left, right - left + 1); } void quickSort_my(ElementType a[], int n) { qsort_mya(a, 0, n - 1); } #define N 22 int main() { getRandomInt(a, N); quickSort_my(a, N); int cnt = 1; for (int i = 0; i < N; i++) if (cnt == a[i]) cnt++; else printf("error"); /*printf("%d ", a[i]);*/ }