排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void bubble_sort(int arr[],int n)//冒泡排序接口 { for(int i = 0;i < n;i++) { int flag = 0; for(int j = 0;j < n-i-1;j++) { if(arr[j] > arr[j+1]) { swap(arr[j],arr[j+1]); flag = 1; } } if(!flag) break; } } void insert_sort(int arr[],int n)//插入排序接口 { for(int i = 1;i < n;i++) { int t = arr[i],j; for(j = i;j > 0 && arr[j-1] > t;j--) arr[j] = arr[j-1]; arr[j] = t; } } void select_sort(int arr[],int n)//选择排序接口 { for(int i = 0;i < n;i++) { int t = i; for(int j = t+1;j < n;j++) { if(arr[t] > arr[j]) t = j; } swap(arr[i],arr[t]); } } void shell_sort(int arr[],int n)//希尔排序接口 { for(int gap = n/2;gap > 0;gap /= 2) { for(int i =gap;i < n;i++) { int t = arr[i],j; for(j = i-gap;j >= 0 && arr[j] > t;j -= gap) arr[j+gap] = arr[j]; arr[j+gap] = t; } } } void merge(int a[],int first,int mid,int last,int temp[])//合并 { int i = first,j = mid+1,k = 0; while (i <= mid && j <= last) { if(a[i] < a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while (i <= mid) temp[k++] = a[i++]; while (j <= last) temp[k++] = a[j++]; for(i = 0;i < k;i++) a[first+i] = temp[i]; } void _merge_sort(int arr[],int temp[],int first,int last)//归并 { if(first < last) { int mid = (first+last)/2; _merge_sort(arr,temp,first,mid); _merge_sort(arr,temp,mid+1,last); merge(arr,first,mid,last,temp); } } void merge_sort(int arr[],int n)//归并排序接口 { int* temp = (int*)malloc(n*sizeof(int)); if(temp == NULL) { printf("内存不足\n"); return; } _merge_sort(arr,temp,0,n-1); free(temp); } void percdown(int arr[],int p,int n)//调整最大堆 { int Parent,Child,x = arr[p]; for(Parent = p;Parent*2+1 < n;Parent = Child) { Child = Parent*2+1; if(Child != n-1 && arr[Child] < arr[Child+1]) Child++; if(x >= arr[Child]) break; else arr[Parent] = arr[Child]; } arr[Parent] = x; } void heap_sort(int arr[],int n)//堆排序接口 { for(int i = n/2-1;i >= 0;i--) percdown(arr,i,n); for(int i = n-1;i > 0;i--) { swap(arr[0],arr[i]); percdown(arr,0,i); } } void _quick_sort(int arr[],int l,int r)//快速排序 { if(l < r) { int i = l,j = r,x = arr[l]; while(i < j) { while(i < j && arr[j] >= x) j--; if(i < j) arr[i++] = arr[j]; while(i < j && arr[i] < x) i++; if(i < j) arr[j--] = arr[i]; } arr[i] = x; _quick_sort(arr,l,i-1); _quick_sort(arr,i+1,r); } } void quick_sort(int arr[],int n)//快速排序接口 { _quick_sort(arr,0,n-1); } int main() { int arr[]={3,6,1,9,4,2,0,5,8,7}; int len=sizeof(arr)/sizeof(arr[0]); quick_sort(arr,len); int i=0; for(i=0;i<len;i++){ printf("%d ",arr[i]); } printf("\n"); return 0; }