๐Ÿ™‡โ€โ™€๏ธ๋ถ„ํ•  ์ •๋ณต

๋ถ„ํ•  ์ •๋ณต์€ ํ›„์œ„ ์ˆœํšŒ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ์‹์ด๋‹ค.

์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๋‹ค ์ง€๋‚˜๊ณ  ์ž์‹ ์˜ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

void divide(int lt, int rt)
{
	int mid;
	int p1, p2, p3;
	if (lt < rt)
	{
		mid = (lt + rt) / 2;
		divide(lt, mid);
		divide(mid + 1, rt);

		p1 = lt;
		p2 = mid + 1;
		p3 = lt;
		while (p1 <= mid && p2 <= rt)
		{
			if (arr[p1] < arr[p2]) 
				temp[p3++] = arr[p1++];
			else 
				temp[p3++] = arr[p2++];
		}
		while (p1 <= mid)
		{
			temp[p3++] = arr[p1++];
		}
		while (p2 <= rt)
		{
			temp[p3++] = arr[p2++];
		}

		for (int i = lt; i <= rt; ++i)
		{
			arr[i] = temp[i];
		}

	}
}

์ด์ „์˜ 39๋ฒˆ ๋‘ ๋ฐฐ์—ดํ•ฉ์น˜๊ธฐ ๋ฌธ์ œ์™€ ๋น„์Šทํ•˜๋‹ค.

์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(nlogn)์ด๋ผ๊ณ  ํ•œ๋‹ค.

๋ถ„ํ•  ์ •๋ณต ์ƒ๊ฐ์•ˆ๋‚  ๋•Œ ๋งˆ๋‹ค ์™€์•ผ๊ฒ ๋‹ค.

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
int n;
int arr[100], temp[100];

void divide(int lt, int rt)
{
	int mid;
	int p1, p2, p3;
	if (lt < rt)
	{
		mid = (lt + rt) / 2;
		divide(lt, mid);
		divide(mid + 1, rt);

		p1 = lt;
		p2 = mid + 1;
		p3 = lt;
		while (p1 <= mid && p2 <= rt)
		{
			if (arr[p1] < arr[p2]) 
				temp[p3++] = arr[p1++];
			else 
				temp[p3++] = arr[p2++];
		}
		while (p1 <= mid)
		{
			temp[p3++] = arr[p1++];
		}
		while (p2 <= rt)
		{
			temp[p3++] = arr[p2++];
		}

		for (int i = lt; i <= rt; ++i)
		{
			arr[i] = temp[i];
		}

	}
}

void solve()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> arr[i];

	divide(1, n);

	for (int i = 1; i <= n; ++i)
		cout << arr[i] << " ";
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: