WEB开发网
开发学院软件开发C++ a program a day 21(ds,bubble sort) 阅读

a program a day 21(ds,bubble sort)

 2010-10-17 08:05:02 来源:WEB开发网   
核心提示:/***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++
/**
*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;
}

Tags:program day ds

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接