首页 > 编程语言 > 详细

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

时间:2014-10-06 11:52:40      阅读:321      评论:0      收藏:0      [点我收藏+]

 

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

 

冒泡排序

bubuko.com,布布扣



冒泡排序(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 }

 

运行结果:


 

 

bubuko.com,布布扣

  代码讲解:


 

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

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

 

End!

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

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

原文:http://www.cnblogs.com/1hua1ye/p/4008010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!