zoukankan      html  css  js  c++  java
  • 冒泡与比较排序

    未经过优化的冒泡和比较排序,有时间再学习如何优化效率;

     1 #include<stdio.h>
     2 int main()
     3 {
     4     int a[10];
     5     printf("plz enter 10 numbers:
    ");
     6     for(int i=0;i<10;i++)
     7     {
     8     scanf("%d",&a[i]);
     9     }
    10     void sort_f(int*k,int n); 
    11     void sort_c(int *k,int n);
    12     sort_f(a,10);
    13     sort_c(a,10);
    14     return 0;
    15 }
    16 
    17 void sort_f(int *k,int n)              //冒泡排序 
    18 {
    19     int t;
    20     for(int i=0;i<n-1;i++)                //n个数,外层for控制趟数,共(n-1)趟 
    21     for(int j=0;j<n-1-i;j++)              //内层for控制每一趟的比较次数,为(n-1-i)次 
    22     {
    23         if(k[j]<k[j+1])                   //这里表示的是小数沉底,即从左到右为从大到小 
    24         {t=k[j]; k[j]=k[j+1];k[j+1]=t;    //三变量交换两个数 
    25         } 
    26     }
    27     printf("the ans of sort_f is
    ") ;
    28     for(int i=0;i<n;i++)
    29     {if(i<9)
    30     printf("%3d",k[i]); 
    31     else printf("%3d
    ",k[i]);
    32     }                                     //打印,为了好看在末尾加了一个换行符 
    33 }
    34 
    35 void sort_c(int *k,int n)                 //比较排序 
    36 {
    37     
    38     for(int i=0;i<n-1;i++)                //外层for同样控制(n-1)趟 
    39     {
    40         int p=i; int t;                   //假设当前第一个数(下标为i)为最大数,用p变量存储最大数下标 
    41         for(int j=i+1;j<n;j++)            //从i+1开始一直遍历到n, 
    42         {
    43             if(k[p]<k[j])                 //若之后出现比原来假设的数大的话,更新 p 
    44             p=j;
    45         }
    46         if(p!=i) {t=k[i];k[i]=k[p];k[p]=t; //关键:因为p是当前循环最大数的下标,判断p和第一个数下标(i)是否相等, 
    47         }                                  //若不等,则之后出现了比第一个数大的数,三变量交换之,若相等,则第一个数 
    48                                            //就是最大数 。 
    49     } 
    50     printf("the ans of sort_c is
    ");      //退出内循环进入下一轮外循环,确保每一趟开头那个数是最大数 
    51     for(int i=0;i<n;i++)
    52     printf("%3d",k[i]);                    //打印 
    53     
    54 }

    exmple:

    I'm wanted for addiction

    She's feeling for a thrill

    We've been scream for attention

    And any little thing to numb this hell

    Oh every now and then I like to get me some to get me some

    Oh even though it's just a face

    Oh know I feel like I've been in a hit and run this shit ain't fun

    I'm still here and you've got my aim

    I've got whiskey and morphine

    Rushing through me

    The stairs are moving quicker than I'm running, And I'm tripping over absolutely nothing.

    Thanks to Whiskey and morphine

    I'm up and away up and away up and away.

    I'll finish every bottle

    Till I'm empty on the floor

    Then do it all again tomorrow Mmm

    And any little thing to numb this hell

     

  • 相关阅读:
    Mysql数据优化--DBA梳理珍藏篇
    Spring缓存注解@Cacheable
    Spring常用知识点
    谈一谈对MySQL InnoDB的认识及数据库事物处理的隔离级别
    MySQL主从复制的实现过程
    List 与 数组 互转
    位运算实现小正整数乘法
    最大的K个数
    Java 编码与字符(2)
    JSP工作原理
  • 原文地址:https://www.cnblogs.com/wuruofeng/p/8079419.html
Copyright © 2011-2022 走看看