数据结构之------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
",i+1,a[i]);
41     }
42     return 0;
43 }

运行结果:


  代码讲解:


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

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

End!

欢迎大家一起交流 ,分享程序员励志故事。   幸福的程序员 QQ群:幸福的程序员  
原文地址:https://www.cnblogs.com/1hua1ye/p/4008010.html