数据结构之------C++指针冒泡排序算法

 

C++通过指针实现一位数组的冒泡排序算法。

 

冒泡排序



冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法



代码:

 
 
 1 /*
 2     Name:冒泡排序算法
 3     Copyright:Null
 4     Author:小X
 5     Date: 06-10-14 10:34
 6     Description:C++通过指针实现一维数组的冒泡排序
 7 */
 8 
 9 #include <iostream>
10 
11 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
16 void sort(int* a,int len)
17 {
18     int i,j,t;
19     for(i=0;i<len-1;i++)
20     {
21         for(j=0;j<len-1-i;j++)
22         {
23             if(a[j]>a[j+1])
24             {
25                 t=a[j];
26                 a[j]=a[j+1];
27                 a[j+1]=t;
28             }
29         }
30     }
31 }
32 
33 int main(int argc, char** argv)
34  {
35     int a[6]={1563,454,56,32,48456,455};
36     sort(a,6);
37     int i;
38     for(i=0;i<6;i++)
39     {
40         printf("第%d个数是%d\n",i+1,a[i]);
41     }
42     return 0;
43 }

 

运行结果:


 

 

  代码讲解:


 

一维数组的数组名代表的是数组的第一个元素的地址,我们在传递参数的时候需要两个参数,一个是首地址,另一个是数组元素的个数,

至少需要这两个参数才能确定一维数组。

 

End!

欢迎大家一起交流 ,分享程序员励志故事。   幸福的程序员 QQ群:  

转载于:https://www.cnblogs.com/1hua1ye/p/4008010.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值