C/C++ 언어 1차원 배열을 인자로 사용한 bubble sort 예제 프로그램


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 1차원 배열을 인자로 사용한 bubble sort 예제 프로그램 */
 
#include <stdio.h>
 
#define SIZE 10
void bubbleSort(int *int);
 
int main(void) {
  int i, array[SIZE]={2,6,4,8,10,12,89,68,45,37};
 
  printf("소팅 전의 데이터 :");
  for (i = 0; i < SIZE; i++)
    printf("%4d", array[i]);
  printf("\n");
  bubbleSort(array, SIZE);
  printf("오름차순 소팅 후의 데이터 :");
  for (i=0; i < SIZE; i++)
    printf("%4d", array[i]);
  printf("\n");
  return 0;
}
 
void bubbleSort(int *a, int size) {
  int pass, j;
  void swap(int *int *);
 
  for (pass = 1; pass < size; pass++)
    for (j = 0; j < size - 1; j++)
      if (a[j] > a[j+1])
        swap(a+j, a+j+1);
}
 
void swap(int *element1Ptr, int *element2Ptr) {
  int temp;
 
  temp = *element1Ptr;
  *element1Ptr = *element2Ptr;
  *element2Ptr = temp;
}
 
cs


C/C++ 언어 1차원 배열을 인자로 사용한 bubble sort 예제 프로그램



+ Recent posts