数据结构的几种经典排序算法

#include <stdio.h>
#include <stdlib.h>
/**
* 排序算法
*/
//伪版冒泡排序
void bubblesort(int k[],int n){
    int i,j,temp,count1=0,count2=0;
    for(i=0;i<n-1;i++){
        for(j=i+1;j<n;j++){
            count1++;
            if(k[i]>k[j]){
                count2++;
                temp = k[j];
                k[j] = k[i];
                k[i] = temp;
            }
        }
    }
    printf("总共进行了%d次比较,进行了%d次移动!",count1,count2);
}
//冒泡排序
void bubblesortplus(int k[],int n){
    int i,j,temp,flag=1,count1=0,count2=0;
    for(i=0;i<n-1&&flag;i++){
        for(j=n-1;j>i;j--){
            flag = 0;
            count1++;
            if(k[j-1]>k[j]){
                count2++;
                flag = 1;
                temp = k[j-1];
                k[j-1] = k[j];
                k[j] = temp;
            }
        }
    }
    printf("总共进行了%d次比较,进行了%d次移动!",count1,count2);
}
//选择排序
void selectsort(int k[],int n){
    int i,j,min,temp,count1=0,count2=0;
    for(i=0;i<n-1;i++){
        min = i;
        for(j=i+1;j<n;j++){
            count1++;
            if(k[j]<k[min]){
                min = j;
            }
        }
        if(min != j){
            count2++;
            temp = k[min];
            k[min] = k[i];
            k[i] = temp;
        }
    }
    printf("总共进行了%d次比较,进行了%d次移动!",count1,count2);
}
//直接插入排序
void insertsort(int k[],int n){
    int i,j,temp;
    for(i=1;i<n;i++){
        if(k[i]<k[i-1]){  //后面的比前面的小要重新排序
            temp = k[i];
            for(j=i-1;k[j]>temp;j--){  //找位置
                k[j+1] = k[j];//向后退
            }
            k[j+1] = temp;
        }
    }
}
//希尔排序
void shellsort(int k[],int n){
    int i,j,temp;
    int gap = n;
    do{
        gap = gap/3 + 1;
        for(i=gap;i<n;i++){
        if(k[i]<k[i-gap]){  //后面的比前面的小要重新排序
            temp = k[i];
            for(j=i-gap;k[j]>temp;j-=gap){  //找位置
                k[j+gap] = k[j];//向后退
            }
            k[j+gap] = temp;
        }
    }
    }while(gap >1);
}
//堆排序
void swap(int k[],int i,int j){
    int temp;
    temp = k[i];
    k[i] = k[j];
    k[j] = temp;
}
void heapadjust(int k[],int s,int n){//调整二叉树构成大顶堆
    int i,temp;
    temp = k[s];
    for(i=2*s;i<=n;i*=2){  //指向下一个结点
        if(i<n && k[i]<k[i+1]){
            i++;//判断左右大小
        }
        if(temp >= k[i]){
            break;
        }
        k[s] = k[i];
        s = i;
    }
    k[s] = temp;
}
void heapsort(int k[],int n){
    int i;
    for(i=n/2;i>0;i--){
        heapadjust(k,i,n);
    }
    for(i=n;i>1;i--){
        swap(k,1,i);
        heapadjust(k,1,i-1);
    }
}
//归并排序(递归)
void merging(int *list1,int list1_size,int *list2,int list2_size){
    int i=0,j=0,k=0,m=0,temp[10];
    while(i < list1_size && j < list2_size){
        if(list1[i] < list2[j]){
            temp[k++] = list1[i++];
        }else{
            temp[k++] = list2[j++];
        }
    }
    while(i < list1_size){
        temp[k++] = list1[i++];
    }
    while(j < list2_size){
        temp[k++] = list2[j++];
    }
    //实现归并,把最后的数据存到list1里
    for(m=0;m<(list1_size+list2_size);m++){
        list1[m] = temp[m];
    }
}
void mergesort(int k[],int n){
    if(n > 1){
        int *list1 = k;
        int list1_size = n/2;  //一半
        int *list2 = k + n/2;
        int list2_size = n - list1_size;  //另一半
        mergesort(list1,list1_size);  //递归
        mergesort(list2,list2_size);
        merging(list1,list1_size,list2,list2_size);
    }
}
//归并排序(迭代)
void MergeSort(int k[],int n){
    int i,next,left_min,left_max,right_min,right_max;
    int *temp = (int *)malloc(n * sizeof(int));//申请内存
    for(i=1;i<n;i*=2){  //步长,2,4,8
        for(left_min=0;left_min<n-i;left_min=right_max){
            right_min = left_max = left_min + i;
            right_max = left_max +i;
            if(right_max>n){
                right_max = n;
            }
            next = 0;
            while(left_min<left_max && right_min<right_max){
                if(k[left_min] < k[right_min]){
                    temp[next++] = k[left_min++];
                }else{
                    temp[next++] = k[right_min++];
                }
            }
            while(left_min<left_max){
                k[--right_min] = k[--left_min];
            }
            while(next > 0){  //还原为
                k[--right_min] = temp[next--];
            }
        }
    }
}
//快速排序
int partition(int k[],int low,int high){
    int point;
    /*
    *改进代码1,添加如下
    int m = low + (high-low)/2;
    if(k[low]>k[high]){
        swap(k,low,high);
    }
    if(k[m]>k[high]){
        swap(k,m,high);
    }
    if(k[m]>k[low]){
        swap(k,m,low);
    }
    */
    point = k[low];
    while(low<high){
        while(low<high && k[high] >= point){
            high--;
        }
        swap(k,low,high);
        /*改进代码2
        k[low] = k[high];//改变不必要的交换
        */
        while(low<high && k[low] <= point){
            low++;
        }
        swap(k,low,high);
        /*改进代码2
        k[high] = k[low];//改变不必要的交换
        */
    }
    /*改进代码2
    k[low] = point;//改变不必要的交换
    */
    return low;  //返回中间点
}
void isort(int k[],int low,int high){  //改进代码3的部分函数
    insertsort(k+low,high-low+1);
}
void qusort(int k[],int low,int high){
    int point;
    if(low < high){
        point = partition(k,low,high);
        qusort(k,low,point-1);
        qusort(k,point+1,high);
    }
    /*改进代码3
    if(high-low>7){
        point = partition(k,low,high);
        qusort(k,low,point-1);
        qusort(k,point+1,high);
    }else{
        isort(k,low,high);  //调用插入
    }
    */
    /*改进代码4,伪递归
    if(high-low>7){
        while(low<high){
            point = partition(k,low,high);
            if(ponit-low<high-point){
                qusort(k,low,point-1);
                low = point + 1;
            }else{
                qusort(k,point+1,high);
                high = point - 1;
            }
        }
    }else{
        isort(k,low,high);  //调用插入
    }
    */
}
void quicksort(int k[],int n){
    qusort(k,0,n-1);
}
//快速排序的优化

int main()
{
    int i;
    //1,0,2,3,4,5,6,7,8,9
    //-1,3,5,4,1,8,7,9,6,0 堆排序时第一个字符不排序应该为不排序字符
    int a[10]={2,3,5,4,1,8,7,9,6,0};
    //bubblesort(a,10);
    //bubblesortplus(a,10);
    //selectsort(a,10);
    //insertsort(a,10);
    //shellsort(a,10);
    //heapsort(a,10);
    //mergesort(a,10);
    //MergeSort(a,10);
    quicksort(a,10);
    printf("排序后的结果是:\n");
    for(i=0;i<10;i++){
        printf("%d",a[i]);
    }
    return 0;
}


发布了65 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/heroybc/article/details/103462644