码迷,mamicode.com
首页 > 其他好文 > 详细

冒泡排序

时间:2014-07-24 22:23:02      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   art   for   re   c   

#include<stdio.h>
#include<time.h>
void bubbleSort1(int a[],int length);
void bubbleSort2(int a[],int length);
void main()
{  
    float start=0,finish=0;
    int a[10]={10,15,4,2,5,3,6,9,7,1};

    printf("普通冒泡排序\n");
    bubbleSort1(a,10);

    printf("有监视哨的冒泡排序\n");
    bubbleSort2(a,10);
}

 void bubbleSort1(int a[],int length)
 {
   int i,j,temp;
   for(i=0;i<length-1;i++)
       for(j=0;j<length-i-1;j++)
       {
           if(a[j]>a[j+1])
           {
               temp=a[j];
               a[j]=a[j+1];
               a[j+1]=temp;
           } 
   }
       for(i=0;i<length;i++)
           printf("%d ",a[i]);
       printf("\n");

 }



  void bubbleSort2(int a[],int length)
 {
   int i,j,temp,flag=1;
   for(i=0;i<length-1&&flag==1;i++)
   {
       flag=0;
       for(j=0;j<length-i-1;j++)
       {    
           if(a[j]>a[j+1])
           {
               temp=a[j];
               a[j]=a[j+1];
               a[j+1]=temp;
               flag=1;  //当flag=1,表示还需排序,当flag=0时,表示排序已完成,不需再扫描数组,退出循环。
           }
       }
        
   }      
       for(i=0;i<length;i++)
           printf("%d ",a[i]);
       printf("\n");

 }

冒泡排序,布布扣,bubuko.com

冒泡排序

标签:style   blog   color   io   art   for   re   c   

原文地址:http://www.cnblogs.com/thrive/p/3866229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!