// bubble 정렬
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# include <time.h>
# define LENGH 10
typedef struct {
int arr[LENGH];
}bubble;
// 함수 원형 ________________
void _init_(bubble* p); // func (1)
void _valueInsert_(bubble* p); // func (2)
void _bubbleSort_(bubble* p); // func (3)
// _________________________
int main(void) { // main function
bubble sNode;
// 데이터 초기화
_init_(&sNode);
_valueInsert_(&sNode);
_bubbleSort_(&sNode);
return 0;
} // end of main function
// _________________________
void _init_(bubble* p) { // func (1)
int i; // index
for (i = 0; i < LENGH; i++)
p->arr[i] = 0x0;
} // end of _init_ function
// _________________________
void _valueInsert_(bubble* p) { // func (2)
int i; // index
srand((unsigned)time(NULL));
for (i = 0; i < LENGH; i++)
p->arr[i] = rand() % 20 + 1;
// check
for (i = 0; i < LENGH; i++) {
printf("%d", p->arr[i]);
if (i != LENGH - 1) {
printf("=>");
}
}
printf("\n"); // 개행
} // end of _valueInsert_ function
// _________________________
void _bubbleSort_(bubble* p) { // func (3)
int i, j, k;
int temp = 0x0;
int num = 0x1;
bool flag;
for (i = 0; i < LENGH - 1; i++) {
flag = true;
for (j = 0; j < LENGH - 1; j++) {
if (p->arr[j] > p->arr[j + 1]) {
flag = false;
temp = p->arr[j];
p->arr[j] = p->arr[j + 1];
p->arr[j + 1] = temp;
printf("[%02d] : ", num);
for (k = 0; k < LENGH; k++) {
printf("[%02d]", p->arr[k]);
if (k != LENGH - 1) {
printf("=>");
}
else {
printf("\n");
}
}
++num;
}
} // endfor
if (flag) {
break;
}
} // endfor
} // end of _bubbleSort_ function
'언어 > c언어' 카테고리의 다른 글
더블릿 풀이 c언어 pie 문제 (0) | 2018.11.18 |
---|---|
선택정렬 (0) | 2018.11.18 |
c언어 네이버 지식인 풀이 (0) | 2018.11.17 |
네이버 지식인 풀이 (0) | 2018.11.14 |
네이버 지식인 답변 (0) | 2018.11.12 |