排序练习
#include <stdio.h> /*二分非递归*/ int HalfSearch(int a[], int low, int high, int key) { while(low <= high) { int mid = (low + high) / 2; if(a[mid] > key) { high = mid - 1; } else if(a[mid] < key) { low = mid + 1; } else { return mid; } } } /*二分递归*/ int HalfSearch2(int a[], int low, int high, int key) { int mid = (low + high) / 2; if (low >= high) { return mid; } if (a[mid] > key) mid = HalfSearch2(a, low, mid - 1, key); else if(a[mid] < key) mid = HalfSearch2(a, mid + 1, high, key); } /*选择排序*/ void SelectSort(int a[], int n) { int i, j, index, temp; for (i = 0; i < n - 1; i++) { index = i; for (j = i + 1; j < n; j++) { if (a[j] < a[index]) { index = j; } } if (index != i) { temp = a[index]; a[index] = a[i]; a[i] = temp; } } } /*插入排序*/ void InsertSort(int a[], int n) { int i, j, temp; for(i = 1; i < n; i ++) { temp = a[i]; if(temp < a[i - 1]) { for(j = i - 1; (j >= 0) && (temp < a[j]); j--) { a[j + 1] = a[j]; a[j] = temp; } } } } /*冒泡排序*/ void BubbleSort(int a[], int n) { int i, j, temp; for(i = 0; i < n; i ++) { for(j = i + 1; j < n - 1; j ++) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } /*快速排序*/ void QuickSort(int a[], int low, int high) { int i = low; int j = high; int temp = a[low]; if (low > high) { return; } while (i < j) { while((i < j) && (a[j] > temp)) { j--; } if (i < j) { a[i] = a[j]; i++; } while((i < j) && (a[i] < temp)) { i++; } if (i < j) { a[j] = a[i]; j--; } } a[i] = temp; QuickSort(a, low, i - 1); QuickSort(a, i + 1, high); } /*归并合并*/ void MergeArray(int a[], int low, int mid, int high, int temp[]) { int t = 0; int k = 0; int i = low; int m = mid; int n = mid + 1; int j = high; while ((i <= m) && (n <= j)) { if (a[i] < a[n]) { temp[t++] = a[i++]; } else if (a[i] > a[n]) { temp[t++] = a[n++]; } } while(i <= m) { temp[t++] = a[i++]; } while(n <= j) { temp[t++] = a[n++]; } for (k = 0; k < t; k++) { a[low + k] = temp[k]; } } /*归并排序*/ void MergeSort(int a[], int low, int high, int temp[]) { if (low < high) { int mid = (low + high)/2; MergeSort(a, low, mid, temp); MergeSort(a, mid + 1, high, temp); MergeArray(a, low, mid, high, temp); } } /*堆增加节点*/ void HeapInsert(int a[], int n, int data) { a[n] = data; int i = n; while ((i/2 > 0) && (a[i] > a[i/2])) { int temp = a[i]; a[i] = a[i/2]; a[i/2] = temp; i = i/2; } } /*从上到下处理堆节点*/ void HeapHandler(int a[], int n, int i) { while (1) { int max = i; if (((i*2) <= n) && (a[i] < a[i*2])) { max = i * 2; } if (((i*2 + 1) <= n) && (a[max] < a[i*2 + 1])) { max = i * 2 + 1; } if (max == i) { break; } int temp = a[i]; a[i] = a[max]; a[max] = temp; i = max; } } /*堆删除*/ void HeapDelete(int a[], int n) { a[1] = a[n]; n = n - 1; HeapHandler(a, n, 1); } /*创建堆*/ void HeapCreat(int a[], int n) { int i; for(i = (n/2); i >= 1; i--) { HeapHandler(a, n, i); } // int k; // for (k = 0; k <= n; k++) // { // printf("a[%d]:%d\n", k, a[k]); // } } /*堆排序*/ void HeapSort(int a[], int n) { HeapCreat(a, n); int i = n; while (i > 1) { int temp = a[i]; a[i] = a[1]; a[1] = temp; i = i - 1; HeapHandler(a, i, 1); } } int main() { int i = 0; int a[10] = {2, 9, 4, 1, 3, 55, 46, 15, 10, 32}; int b[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int c[10]; int d[11] = {0, 2, 9, 4, 1, 3, 55, 46, 15, 10, 32}; //MergeSort(a, 0, 9, c); HeapSort(d, 10); for (i = 0; i < 11; i++) { printf("d[%d]:%d\n", i, d[i]); } //printf("%d\n", HalfSearch2(b, 0, 9, 7)); return 0; }
堆排序参考: