排序算法总结
1 冒泡排序
//冒泡排序 #include <stdio.h> void bubbleSort(int data[],int length) { int i , j; for(i=0;i<length;i++) { for(j=0;j<length - i - 1;j++) { if(data[j] > data[j + 1]) { int temp = data[j + 1]; data[j + 1] = data[j]; data[j] = temp; } } } } int main() { int data[] = {4,6,3,8,10,2,23,2,1,-6,-5,-23}; int length = sizeof(data) / sizeof(int); bubbleSort(data,length); int i = 0; for(i=0;i<length;i++) { printf("%d ",data[i]); } printf("\n"); }
2 插入排序
//插入排序 #include <stdio.h> void insertSort(int data[],int length) { int i = 0,j,key; for(i=1;i<length;i++) { key = data[i]; j = i - 1; while(j >= 0 && data[j] > key) { data[j+1] = data[j]; j--; } data[j + 1] = key; } } int main() { int data[] = {4,6,3,8,10,2,23,2,1,-6,-5,-23}; int length = sizeof(data) / sizeof(int); insertSort(data,length); int i = 0; for(i=0;i<length;i++) { printf("%d ",data[i]); } printf("\n"); }
3 简单选择排序
//简单选择排序 #include <stdio.h> void selectSort(int data[],int length) { int i,j,min; for(i=0;i<length;i++) { min = i; for(j=i+1;j<length;j++) { if(data[j] < data[min]) { min = j; } } if(min != i) { int temp = data[i]; data[i] = data[min]; data[min] = temp; } } } int main() { int data[] = {4,6,3,8,10,2,23,2,1,-6,-5,-23}; int length = sizeof(data) / sizeof(int); selectSort(data,length); int i = 0; for(i=0;i<length;i++) { printf("%d ",data[i]); } printf("\n"); }
4 快速排序
//快速排序 #include <stdio.h> int quickpass(int data[],int left,int right) { int key = data[left]; int low = left,high = right; while(low < high) { while(low < high && data[high] > key) { high--; } if(low < high) { data[low] = data[high]; low++; } while(low < high && data[low] < key) { low++; } if(low < high) { data[high] = data[low]; high--; } } data[low] = key; return low; }
//简略版
int quickPass(int data[], int left, int right) { int low = left; int high = right; int key = data[low]; while (low < high) { while (low < high && data[high] >= key) { high--; } data[low] = data[high]; while (low < high && data[low] <= key) { low++; } data[high] = data[low]; } data[low] = key; return low; }
void quickSort(int data[],int left,int right)
{ if(left < right) { int pos = quickpass(data,left,right); quickSort(data,left,pos - 1); quickSort(data,pos + 1, right); } } int main() { int data[] = {4,6,3,8,10,2,23,2,1,-6,-5,-23}; int length = sizeof(data) / sizeof(int); quickSort(data,0,length-1); int i = 0; for(i=0;i<length;i++) { printf("%d ",data[i]); } printf("\n"); }
5 堆排序
//堆排序,利用大根堆建立对数组进行升序排序 #include <stdio.h> //调整堆 void sift(int data[], int k,int m) { int key = data[k]; int currentRoot = k; int maxChild = 2 * currentRoot + 1; int finish = 0; while(maxChild < m && !finish) { if(maxChild < m - 1 && data[maxChild] < data[maxChild + 1]) maxChild = maxChild + 1; if(key >= data[maxChild]) finish = 1; else { data[currentRoot] = data[maxChild]; currentRoot = maxChild; maxChild = 2 * maxChild + 1; } } data[currentRoot] = key; } //建初堆 void creatHeap(int data[],int length) { int i; for(i = (length / 2) - 1; i >= 0 ;i--) { sift(data,i,length); } } void heapSort(int data[],int length) { creatHeap(data,length); int i ; for(i = length - 1; i > 1 ; i--) { int temp = data[0]; data[0] = data[i]; data[i] = temp; sift(data,0,i - 1); } } int main() { int data[] = {4,6,3,8,10,2,23,2,1,-6,-5,-23}; int length = sizeof(data) / sizeof(int); heapSort(data,length); int i = 0; for(i=0;i<length;i++) { printf("%d ",data[i]); } printf("\n"); }
6 归并排序
//归并排序 #include <stdio.h> #include <malloc.h> void merge(int data[],int low,int middle,int high) { int leftLength = middle - low + 1; int rightLength = high - middle; int *left = (int *)malloc(sizeof(int) * leftLength); int *right = (int *)malloc(sizeof(int) * rightLength); int i,j,k; for(i=0;i<leftLength;i++) { left[i] = data[low + i]; } for(i=0;i<rightLength;i++) { right[i] = data[middle + i + 1]; } for(i=0,j=0,k=low;i<leftLength && j<rightLength;k++) { if(left[i] <= right[j]) { data[k] = left[i]; i++; } else { data[k] = right[j]; j++; } } if(i == leftLength) { for(;j<rightLength;j++,k++) { data[k] = right[j]; } } else if(j == rightLength) { for(;i<leftLength;i++,k++) { data[k] = left[i]; } } } void mergeSort(int data[],int low,int high) { if(low < high) { int middle = (low + high) / 2; mergeSort(data,low,middle); mergeSort(data,middle+1,high); merge(data,low,middle,high); } } int main() { int data[] = {4,6,3,8,10,2,23,2,1,-6,-5,-23}; int length = sizeof(data) / sizeof(int); mergeSort(data,0,length-1); int i = 0; for(i=0;i<length;i++) { printf("%d ",data[i]); } printf("\n"); }