赞
踩
1.快速排序
- void quicksort(int* a,int left,int right){
- if(left>=right)return ;
- int begin=left,end=right;
- int pivot=begin,key=a[begin];
-
-
- while(begin<end){
- while(begin<end&&a[end]>=key){
- end--;
- }
- a[pivot]=a[end];
- pivot=end;
- while(begin<end&&a[begin]<=key){
- begin++;
- }
- a[pivot]=a[begin];
- pivot=begin;
- }
- a[pivot]=key;
- quicksort(a,left,pivot-1);
- quicksort(a,pivot+1,right);
- }
2.冒泡排序
- void bubble_sort(int* a,int len){
- int i,j,temp;
- for(i=0;i<len-1;i++){
- int cnt=0;
- for(int j=0;j<len-1-i;j++){
- if(a[j]>a[j+1]){
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- cnt=1;
- }
- }
- //一趟下来没有交换任何位置,则证明已排好序
- if(cnt==0){
- break;
- }
- }
- }
3.插入排序
- void insert_sort(int* a,int len){
- int i,j,temp;
- for(int i=1;i<len;i++){
- temp=a[i];
- j=i-1;
- while(j>=0&&a[j]>temp){
- a[j+1]=a[j];
- j--;
- }
- a[j+1]=temp;
- }
- }
4.希尔排序
- void shell_sort(int* a,int len){
- int i,j;
- int gap=0;
- int temp=0,k;
- for(int gap=len/2;gap>0;gap/=2){
- for(i=0;i<gap;i++){
- for(j=i+gap;j<len;i+=gap){
- temp=a[j];
- k=j-gap;
- while(k>=0&&a[k]>temp){
- a[k+gap]=a[k];
- k-=gap;
- }
- a[k+gap]=temp;
- }
- }
- }
- }
5.选择排序
- void select_sort(int* a,int len){
- int i,j,min,index;
- int temp=0;
- for(int i=0;i<len-1;i++){
- min=a[i];
- index=i;
- for(j=i+1;j<len;j++){
- if(a[j]<min)
- {
- min=a[j];
- index=j;
- }
- }
- if(index!=i)
- {
- temp=a[i];
- a[i]=a[index];
- a[index]=temp;
- }
- }
- }
6.归并排序
- void merge(int* a,int left,int mid,int right){
- int i=left,j=mid+1,index=0;
- int* newarr=(int* )malloc((right-left+1)*sizeof(int));
- if(newarr==NULL)
- {
- printf("malloc error");
- exit(1);
- }
- while(i<=mid&&j<=right){
- if(a[i]<a[j])newarr[index++]=a[i++];
- else newarr[index++]=a[j++];
- }
- while(i<=mid)
- newarr[index++]=a[i++];
- while(j<=right)
- newarr[index++]=a[j++];
- for(i=left,index=0;index<(right-left+1);i++,index++){
- a[i]=newarr[index++];
- }
- free(newarr);
- }
- void merge_sort(int* a,int low,int high){
- if(low>=high)return ;
- int mid=(high-low)/2;
- merge_sort(a,low,mid);
- merge_sort(a,mid+1,high);
- merge(a,low,mid,high);
- }
7.基数排序
- int get_max(int a[], int n) {
- int i, max;
- max = a[0];
- for (i = 1; i < n; i++) {
- if (a[i] > max) {
- max = a[i];
- }
- }
- return max;
- }
-
-
- void count_sort(int a[], int n, int exp) {
- int *output = (int *)malloc(n*sizeof(int));
- int i, buckets[10] = { 0 };
- for (i = 0; i < n; i++) {
- buckets[(a[i] / exp) % 10]++;
- }
- for (i = 1; i < 10; i++) {
- buckets[i] += buckets[i - 1];
- }
- for (i = n - 1; i >= 0; i--) {
- output[buckets[(a[i] / exp) % 10] - 1] = a[i];
- buckets[(a[i] / exp) % 10]--;
- }
-
- for (i = 0; i < n; i++) {
- a[i] = output[i];
- }
- }
-
- void radix_sort(int a[], int n) {
- int exp;
- int max = get_max(a, n);
- for (exp = 1; max / exp > 0; exp *= 10) {
- count_sort(a, n, exp);
- }
- }
-
8 堆排序
- void swap(int* a, int* b) {
- int tmp = *a;
- *a = *b;
- *b = tmp;
- }
-
-
- void maxheap_sort(int *a, int start,int end) {
- int c = start;
- int l = 2 * c + 1;
- int tmp = a[c];
- for (; l <= end; c = l, l = 2 * l + 1) {
- if (l < end && a[l] < a[l + 1]) {
- l++;
- }
- if (tmp >= a[l]) {
- break;
- }
- else {
- a[c] = a[l];
- a[l] = tmp;
- }
- }
- }
-
-
- void heap_sort_asc(int a[], int n) {
- int i;
- for (i = n / 2 - 1; i >= 0; i--) {
- maxheap_sort(a, i, n - 1);
- }
- for (i = n - 1; i > 0; i--) {
- swap(&a[0], &a[i]);
- maxheap_sort(a, 0, i - 1);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。