• 三种冒泡及性能比较


    冒泡排序是常见的排序方法,各种写法参差不齐,性能有优劣,也有不少是伪冒泡,总之能排出来吧。

    根据个人心得,冒泡就是像水中的气泡,从地下最小的,慢慢上升,越来越大,到最后冒出水面。这是个动态的过程,把握这个精髓,就理解了冒泡的概念了,具体的解释不多说,下面就比较一下各种写法的优劣 看截图

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define ARRAYCOUNT 10000
    void bubleSort(int *array,int count)
    {
    
        int changecount=0;
        int comparecount=0;     
        for(int i=0;i<count;i++){ 
            for(int j=count-1;j>i;j--){
                 if (array[j] < array[j - 1]) 
                 { //每次把最小的换下去,把更大的数换上来,这就是冒泡的过程
                    //缺点,交换的次序多 
                     int temp = array[j]; 
                     array[j] = array[j - 1]; 
                    array[j - 1] = temp; 
                    changecount++;
                } 
                comparecount++;
            }
        }
        printf("冒泡1 比较次数:%d  交换次数: %d
    ",comparecount,changecount);
    }
    
    void bubleSort2(int *array,int count)
    {
    
        int changecount=0;
        int comparecount=0; 
        for(int i=0;i<count;i++){ 
            for(int j=0;j<count-i-1;j++){
                 if (array[j] > array[j + 1]) 
                 { //每次从下面开始冒,把更大的数换上来,这就是模拟冒泡的过程
                    //缺点,交换的次序多 
                     int temp = array[j]; 
                     array[j] = array[j + 1]; 
                    array[j + 1] = temp; 
                    changecount++;
                } 
                comparecount++;
            }
        }
        
        printf("冒泡2 比较次数:%d  交换次数: %d
    ",comparecount,changecount);
    }
    
    void bubleSort3(int *array,int count)
    {
        int changecount=0;
        int comparecount=0; 
        for(int i=0;i<count;i++){ 
            for(int j=count-i;j>=0;j--){ //伪冒泡 
              if(array[j]>array[count-i])//只比较,不交换,偷了一下懒,等到比到最大的那个了,再交换,为下面埋下了伏笔 
                {
                    int temp=array[count-i];
                    array[count-i]=array[j];
                    array[j]=temp;
                    changecount++;
                }
                comparecount++; 
            }
        }
        printf("伪冒泡3 比较次数:%d  交换次数: %d
    ",comparecount,changecount);
    }
    
    void printArray(int a[],int count)
    {
        for(int i=0;i<count;i++){
            printf("%d ",a[i]);
        }
        printf("
    ");
    }
    void copyarray(int *s,int *d,int n)
    {
        for(int i=0;i<n;i++){
            d[i]=s[i];
        }
    }
    int main()
    {
        srand((unsigned)time(NULL)); 
        int *randAry=(int *)malloc(sizeof(int)*ARRAYCOUNT);
        for(int i=0;i<ARRAYCOUNT;i++)
        {
            randAry[i]=rand()%1000;
        }
    //    printArray(randAry,ARRAYCOUNT);
        
         int *c=(int *)malloc(sizeof(int)*ARRAYCOUNT);
         copyarray(randAry,c,ARRAYCOUNT);   
         bubleSort(c,ARRAYCOUNT);
        
        int *a=(int *)malloc(sizeof(int)*ARRAYCOUNT);;  
        copyarray(randAry,a,ARRAYCOUNT);
        bubleSort2(a,ARRAYCOUNT); 
       
        int *b=(int *)malloc(sizeof(int)*ARRAYCOUNT);;
        copyarray(randAry,b,ARRAYCOUNT);  
        bubleSort3(b,ARRAYCOUNT);
    
    //    printArray(a,ARRAYCOUNT);   
        return 0;
    }

    10个随机数的排序结果:

    100个随机数的排序结果

    1000个随机数的排序结果,看来偷懒的效果出来了

    1万个随机数的排序结果比较

    偷懒的做法到后面就是减少了交换的次数,但是比较次数也增多了,排序时间没有计算,这两者之间的效率问题还得请各位大牛指点一下。

  • 相关阅读:
    nginx负载均衡
    saltstack高效运维
    nginx入门与实战
    web service基础知识
    centos tomcat解压版安装
    centos yum 安装jdk1.7
    centos7安装opencv3.4.1(同样适用于最新版本4.2.0)
    正向代理与反向代理,正向代理与反向代理的应用
    MySQL日期 字符串 时间戳互转
    idea svn提交时,performing vcs refresh时间很长的解决办法
  • 原文地址:https://www.cnblogs.com/nonato/p/3458530.html
Copyright © 2020-2023  润新知