公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

在 C 编程语言中,冒泡排序是最简单的排序技术,也称为交换排序。

冒泡排序过程

    将第一个元素与列表中的其余元素进行比较,如果它们不按顺序进行交换(交换)。

    对列表中的其他元素重复相同的操作列表,直到所有元素都已排序。

    算法

    下面给出的是一种算法,通过使用冒泡排序技术 –

    第 1 步 – 开始

    第 2 步 – 获取列表(数组),num

    第 3 步− readlist(list,num)

    第 4 步− printlist(list,num)

    第5步 – bub_sort(list,num)

    第6步 – printlist(list,num)

    readlist (list, num)

    登录后复制

    第7步 − 停止

    1. for j = 0 to num
    2. read list[j].

    登录后复制

    打印列表(列表,数字)

    1. for j =0 to num
    2. write list[j].

    登录后复制

    bub_sort(列表,数字)

    1. for i = 0 to num
    2. for j =0 to (num – i)
    3. if( list[j] > list[j+1])
    4. swapList( address of list[j], address of list[j+1])

    登录后复制

    swapList( list[j]的地址, list[j+1]的地址)

    1. temp = value at list[j]
    2. value at list[j] = value at list[j+1]
    3. value at list[j+1] = temp

    登录后复制

    示例

    以下是用冒泡排序技术对给定数字列表进行升序排序的C程序

    演示

    #include <stdio.h>
    #define MAX 10
    void swapList(int *m,int *n){
       int temp;
       temp = *m;
       *m = *n;
       *n = temp;
    }
    /* Function for Bubble Sort */
    void bub_sort(int list[], int n){
       int i,j;
       for(i=0;i<(n-1);i++)
          for(j=0;j<(n-(i+1));j++)
             if(list[j] > list[j+1])
                swapList(&list[j],&list[j+1]);
       }
       void readlist(int list[],int n){
       int j;
       printf("

    Enter the elements:

    "); for(j=0;j<n;j++) scanf("%d",&list[j]); } /* Showing the contents of the list */ void printlist(int list[],int n){ int j; for(j=0;j<n;j++) printf("%d\t",list[j]); } void main(){ int list[MAX], num; printf(" Enter the number of elements

    "); scanf("%d",&num); readlist(list,num); printf("

    Elements in the list before sorting are:

    "); printlist(list,num); bub_sort(list,num); printf("

    Elements in the list after sorting are:

    "); printlist(list,num); }

    登录后复制

    输出

    执行上述程序时,会产生以下结果 –

    Enter the number of elements
    10
    
    Enter the elements:
    11
    23
    45
    1
    3
    
    6
    35
    69
    10
    22
    
    
    Elements in the list before sorting are:
    11 23 45 1 3 6 35 69 10 22
    
    Elements in the list after sorting are:
    1 3 6 10 11 22 23 35 45 69

    登录后复制

    以上就是使用冒泡排序算法对给定的数字列表进行升序排序的C程序的详细内容,更多请关注www.xfxf.net其它相关文章!

分享到:
标签:C程序 冒泡排序 升序排序
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定