반응형
//------------------------------------------------------------------------------
// Filename : Boublesort.c  
// Date  : 2003/3/14
//------------------------------------------------------------------------------
unsigned short a[10]= { 10, 2, 6, 1, 13, 17, 37, 30, 12, 10 } ;
 
int main( )
{
  static char n=10;
  unsigned char temp,i,j;
 
  for(i=0; i<=n-1; i++)
    for(j=i+1; j<=n-1; j++)
      if(a[i] > a[j])
      {   temp=a[j];    a[j]=a[i];    a[i]=temp;   } 

  return 0;
}


/* 버블 소트 2 */
# include <stdio.h>
#define swap(a,b){temp=a; a=b; b=temp;}

int bubble_sort(int list[], int cnt)
{
int i, j, temp;
if (sizeof(list) / sizeof(int) < 1)
return -1;  // 원소 개수가 1 이하이면 소트 없음

for (i = cnt; i >= 0; i--) 
for(j = 0; j <= i-2; j++ ) 
if (list[j] > list[j+1]) 
swap(list[j], list[j + 1]);     // temp=list[j]; list[j]=list[j+1]; list[j+1]=temp;
return 0;
}

int main(void)
{
int i = 0, count;
static int test[] = { 5,8,10,-1,-10,100,50,90,45,64,7,9,-20,-10,-1,0, 255, 500, -300, -256 };
count = sizeof(test) / sizeof(int);

for (i = 0; i < count; i++)
printf("%i ", test[i]);

printf("\n\nBubble sorting...\n\n");
bubble_sort(test, count);

for (i = 0; i < count; i++)
printf("%i ", test[i]);

return 0;
}


반응형

+ Recent posts