λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
🐍 Algorithm/μ•Œκ³ λ¦¬μ¦˜-Python

[μ•Œκ³ λ¦¬μ¦˜] Day2 - 2750번 수 μ •λ ¬ν•˜κΈ°

by Danna 2018. 8. 4.
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