生无涯

吾生也有涯,而知也无涯,以无涯随有涯,乐以忘忧,生亦无涯矣www.cnblogs.com/shengwuya
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

a program a day 21(ds,bubble sort)

Posted on 2010-10-17 00:42  生无涯  阅读(183)  评论(0)    收藏  举报

/**
*bubble Sort
**/
#define keyType int
#include<stdio.h>
//base bubble sort
int bubbleSort(keyType array[],int n)
{
 keyType tmp;
 for(int i = 1;i <= n-1;i++)
 {
  for(int j = 0;j < n-i;j++)
  {
   if(array[j] > array[j+1])
   {
    tmp = array[j];
    array[j] = array[j+1];
    array[j+1] = tmp;
   }
  }
 }
 return 0;
}
//advanced bubble sort
int adBubSort(keyType array[],int n)
{
 keyType tmp;
 int tag = 1;
 for(int i = 1;i <= n-1 && 1 == tag;i++)// if tag is not set then the array is already orderly.
 {
     tag = 0;
  for(int j = 0;j < n-i;j++)
  {
   if(array[j] > array[j+1])
   {
    tmp = array[j];
    array[j] = array[j+1];
    array[j+1] = tmp;
    tag = 1;
   }
  }
 }
 return 0;
}
int main()
{
 keyType arr[10] = {6,4,72,10,4,51,23,100,47,0};
 printf("the array before sorted is :\n");
 for(int i = 0;i < 10;i++)
    printf("%d ",arr[i]);
 adBubSort(arr,10);
    printf("\nthe result of insertion sorting is :\n");
 for(int j = 0;j < 10;j++)
    printf("%d ",arr[j]);
 printf("\n");
 return 0;
}