zoukankan      html  css  js  c++  java
  • 数据结构之------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群:幸福的程序员  
  • 相关阅读:
    sitemap.xml生成方法(asp和php)(转)
    浏览器缓存机制(转)
    JavaScript在IE浏览器和Firefox浏览器中的差异总结(转)
    JavaScript的数组及其操作
    Google Maps和GIS开发资源收集
    js 字符串转换成数字(转)
    关于构造函数
    C#类型转化
    .net framework
    C#变量
  • 原文地址:https://www.cnblogs.com/1hua1ye/p/4008010.html
Copyright © 2011-2022 走看看