728x90
728x90
01 λ¬Έμ
02 μκ³ λ¦¬μ¦
- μ½μ μ λ ¬κ³Ό λ²λΈμ λ ¬μ λνμ¬ μ€λ¦μ°¨μ μ λ ¬μ νλ€.
03 μ½λ
1_ sort.h
void insertion_sort(int N, int *num);
void bubble_sort(int N, int *num);
void print_num(int N, int *num);
void insertion_sort(int N, int *num)
{
int i, j;
for (i = 1; i < N; i++)
{
int key = i;
for (j = i-1; j >= 0; j--)
{
if (num[key] < num[j])
{
int tmp = num[key];
num[key] = num[j];
num[j] = tmp;
key = j;
}
print_num(N, num);
}
}
}
void bubble_sort(int N, int *num)
{
int i, j;
for (i = 0; i < N; i++) // iλ λ°λ³΅μ μν κ²
{
// λ²λΈ μ λ ¬μ μ€λ₯Έμͺ½ λμμ μμ
for (j = N - 1; j >= 0; j--)
{
if (num[j] < num[j-1])
{
int tmp = num[j];
num[j] = num[j-1];
num[j-1] = tmp;
}
print_num(N, num);
}
}
}
void print_num(int N, int *num)
{
int i;
printf("-> ");
for (i = 0; i < N; i++)
{
printf("%d ", num[i]);
}
printf("\n");
}
2_ bubble_sort.cpp
#include "stdafx.h"
#include "sort.h"
//#include <stdio.h>
int main(void)
{
int i, N;
int num[1000] = { 0, };
scanf("%d", &N);
for (i = 0; i < N; i++)
scanf("%d", &num[i]);
print_num(N, num);
bubble_sort(N, num);
print_num(N, num);
return 0;
}
3_ insertion.sort.cpp
#include "stdafx.h"
#include "sort.h"
//#include <stdio.h>
int main(void)
{
int i, N;
int num[1000] = { 0, };
scanf("%d", &N);
for (i = 0; i < N; i++)
scanf("%d", &num[i]);
print_num(N, num);
insertion_sort(N, num);
print_num(N, num);
return 0;
}
728x90
728x90
'π Algorithm > μκ³ λ¦¬μ¦-Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μκ³ λ¦¬μ¦] Day3 - 2309λ² μΌκ³± λμμ΄ (0) | 2018.08.08 |
---|---|
[μκ³ λ¦¬μ¦] Day3 - 1260λ² DFSμ BFS (0) | 2018.08.08 |
[μκ³ λ¦¬μ¦] Day2 - 2775λ² λΆλ νμ₯μ΄ λ ν μΌ (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - κ΄νΈ (0) | 2018.08.04 |
[μκ³ λ¦¬μ¦] Day1 - μ€ν (0) | 2018.08.04 |