#include <stdio.h>
#include <stdlib.h>
#define swap(type, x, y) \
do \
{ \
type t = x; \
x = y; \
y = t; \
} while (0)
void bubble(int a[], int n)
{
int k = 0, b = n - 1;
while (k < b)
{
int j;
int last = b;
for (j = b; j > k; j--)
if (a[j - 1] > a[j])
{
swap(int, a[j - 1], a[j]);
last = j;
}
k = last;
for (j = k; j < b; j++)
if (a[j] > a[+1])
{
swap(int, a[j], a[j + 1]);
last = j + 1;
}
b = last;
}
}
int main(void)
{
int i, nx;
int *x;
puts("버블 정렬");
printf("요소 개수 : ");
scanf("%d", &nx);
x = calloc(nx, sizeof(int));
for (i = 0; i < nx; i++)
{
printf("x[%d] : ", i);
scanf("%d", &x[i]);
}
bubble(x, nx);
free(x);
return 0;
}
'TEST > 자료구조와 함께 배우는 알고리즘 입문(C언어)' 카테고리의 다른 글
<6> Q8. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.23 |
---|---|
<6> Q7. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.23 |
<6> Q5. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.22 |
<6> Q4. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.21 |
<6> Q3. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.21 |