标签:
#include<stdio.h> int *mp_sort(int *a,int len)//冒泡排序 { if(a== NULL|| len <= 0) return NULL; int i = 1, j = 1; for (i; i < len - 1;++i) for (j = 0; j <len- i; ++j) { int temp; if (a[j]>a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } return a; } int Bin_search(int *a, int len,int key)//二分查找 { if (a == NULL || len <= 0) return NULL; int low = 0, high = len - 1; while (low<=high) { int middle = (low + high) / 2; if (a[middle] == key) return middle; else if (a[middle] < key) low = middle + 1; else high = middle - 1; } return -1; } int *ch_sort(int *a, int len) //选择排序 { if (a == NULL || len <= 0) return NULL; int i, j; for (i = 0; i < len - 1;++i) for (j = i + 1; j < len;++j) if (a[i]>a[j]) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } return a; } int partition(int a[], int len, int low, int high)//利用分治法进行一趟快速排序 { if (a == NULL || len <= 0 || low < 0 || high >= len) return -1; int start = low; while (low < high) { if (a[high] <a[start]) { int temp; temp = a[start]; a[start] = a[high]; a[high] = temp; start = high; low++; if (a[low]>a[start]) { temp = a[low]; a[low] = a[start]; a[start] = temp; start = low; high--; } } else high--; } return start; } int *quik_sort(int a[], int len, int low, int high)//快速排序 { if (low==high) return NULL; int p = partition(a, len, low, high); quik_sort(a, len, low, p - 1); quik_sort(a, len, p + 1, high); return a; } int main() { int a[] = { 10,2,9,4,5,6,7,8,3}; int i = 0; //int *p = sort(a, sizeof(a)/sizeof(a[0])); /*int i = 0; if (p!=NULL) /*for (i; i < sizeof(a) / sizeof(a[0]); ++i) printf("%d ", *(a + i));*/ /*int m = Bin_search(a, (sizeof(a) / sizeof(a[0])),1); if (m) printf("%d\n", a[m]);*/ int *p=quik_sort(a, 9, 0, 8); for (i; i < sizeof(a) / sizeof(a[0]); ++i) printf("%d ",a[i]); printf("\n"); return 0; }
标签:
原文地址:http://blog.csdn.net/yangshuangtao/article/details/46504537