使用冒泡排序算法对给定的数字列表进行升序排序的C程序

使用冒泡排序算法对给定的数字列表进行升序排序的c程序

在 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 num2. read list[j].

打印列表(列表,数字)

1. for j =0 to num2. write list[j].

bub_sort(列表,数字)

1. for i = 0 to num2. 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 #define MAX 10void 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 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("%dt",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 elements10Enter the elements:11234513635691022Elements in the list before sorting are:11 23 45 1 3 6 35 69 10 22Elements in the list after sorting are:1 3 6 10 11 22 23 35 45 69

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:41:47
下一篇 2025年12月17日 22:42:13

相关推荐

发表回复

登录后才能评论
关注微信