Bubble Sort [내림차순]

BubbleSort.c


/- Bubble Sort 내림차순으로 정렬 *-


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define A_Size 10 // 배열 크기 10

void bubbleSort(int list[]);
void inArray (int list[]);
void printOut(int list[]);

int main(void)
{
 int arr[A_Size];
 
 inArray(arr);

 printOut(arr);

 bubbleSort(arr);

 printOut(arr);
 return 0;
}
void inArray (int list[])
{
 int loop;
 srand(time(NULL));

 for(loop = 0 ; loop < A_Size; loop++)
  list[loop] = rand()%100; // 0~99 까지의 값 입력
}

void printOut(int list[])
{
 int loop;
 for (loop = 0; loop < A_Size; loop++)
  printf("%4d", list[loop]);
 puts(""); // \n
}

void bubbleSort(int list[])
{
 int temp;
 int sorted; // 정렬이 다 되어있는지 확인
 int current;
 int walker;

 for(current = 0, sorted = 0; current <= A_Size && !sorted; current++)
 {
  for(walker = A_Size, sorted = 1; walker > current; walker--)
  {
   if(list[walker] > list[walker - 1])
   {
    sorted = 0; // 정렬이 안되있음
    temp = list[walker]; // Walker와 Walker-1를 swap
    list[walker] = list[walker-1];
    list[walker-1] = temp;
   }
  }
 } 
}

by -S군- | 2011/06/09 17:14 | Data Structure [C] | 트랙백 | 덧글(0)

트랙백 주소 : http://sbszz.egloos.com/tb/1908918
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶