zoukankan      html  css  js  c++  java
  • 各种冒泡排序法

      普通版1:

    #include<stdio.h>
    int main()
    {
        int a[100];
        int i,j,n,des;
        printf("please enter n:");
        scanf("%d",&n);
        printf("please enter the numbers:");
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(j=0;j<n;j++)
            for(i=0;i<n-j;i++)    // 这里是递增的
              if(a[i]>a[i+1])
               {
                     des=a[i];
                     a[i]=a[i+1];
                     a[i+1]=des;
               }
        for(i=0;i<n;i++)
            printf("%d ",a[i]);
        printf("
    ");
        return 0;
    }

      普通版2:

    #include<stdio.h>
    int main()
    {
        int a[10],i=0,j,temp;
        printf("please enter 10 numbers:
    ");
        while(i<10)
            scanf("%d",&a[i++]);
        for(i=0;i<10;i++)
            for(j=9;j>=i;j--)    // 这里是递减的
                if(a[j]<a[j-1])
                    temp=a[j],a[j]=a[j-1],a[j-1]=temp;
        for(i=0;i<10;i++)
            printf("%d ",a[i]);
        printf("
    ");
        return 0;
    }

      指针版:

    #include<stdio.h>
    #define N 10
    int main()
    {
        int a[N],*p=a,*var=a,x;
        printf("请输入10个整数:
    ");
        for(p=a;p<a+N;p++)
            scanf("%d",p);
        for(var=a+1;var<a+N;var++){
            for(p=a+N-1;p>=a;p--){
                if(*p>*(p-1))
                    x=*(p-1),*(p-1)=*p,*p=x;
            }
        }
        printf("
    对10个整数进行由大到小排序:
    ");
        for(p=a;p<a+N;p++)
            printf("%d ",*p);
        printf("
    ");
        return 0;
    }

      对字符串进行排序:

    #include<stdio.h>
    #include<string.h>
    #define N 20
    #define n 20
    int main()
    {
        char c[N][n],cont[n];
        int i,j;
        printf("Please enter the names of all the strings:
    ");
        for(i=0;i<N;i++){
            scanf("%s",c[i]);   
        }
        for(i=1;i<N;i++){
            for(j=0;j<N-i;j++){
                if(strcmp(c[j+1],c[j])<0)
                    strcpy(cont,c[j]);strcpy(c[j],c[j+1]);strcpy(c[j+1],cont);
            }
        }
        for(i=0;i<N;i++){
            if(i%5==0)
                printf("
    ");
            printf("%s ",c[i]);
        }
        printf("
    ");
        return 0;
    }

      函数版:

    #include<stdio.h>
    void Print(int *num, int n)
    {
        int i;
        for(i = 0; i < n; i++)
            printf("%d ", num[i]);
        puts("
    ");
        return;
    }
    void Bubble_Sort(int *num, int n)
    {
        int i, j;
        for(i = 0; i < n; i++)
        {
            for(j = 0; i + j < n - 1; j++)
            {
                if(num[j] > num[j + 1])
                {
                    int temp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                }
                Print(num, n);
            }
        }
        return;
    }
    int main()
    {
        int num[8] = {5,3,1,4,2};
        Bubble_Sort(num, 5);
        return 0;
    }

    ---------------------------------补充 2017 9.29 23:18:07--------------------------------

      链表版冒泡排序:点击这里

  • 相关阅读:
    配置ssl(阿里云+腾讯云)
    使用Nativefier将web页面打包为桌面应用
    临时切换淘宝源下载包
    ajax实现异步上传多图并且预览
    通过phpexcel插件导出报表功能实现
    ThinkPHP5.0自定义命令行的使用
    Longest Substring Without Repeating Characters
    计算机操作系统巡回置换算法
    HDU 2041 DP
    HDU 2044 DP (fibonacci)
  • 原文地址:https://www.cnblogs.com/darkchii/p/6781905.html
Copyright © 2011-2022 走看看