본문 바로가기
알고리즘 풀이

수 정렬하기

by 남생이야 2024. 8. 7.

https://www.acmicpc.net/problem/2750

 

 

 

 

버블정렬로 간단하게 풀 수 있느 문제였다. O(n^2)인 시간복잡도이기 때문에 속도는 느리지만 구현은 매우 간단한 정렬기법이다. 

 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



int main()
{
	vector<int> arr;
	int n; 
	cin >> n; 

	for (int i = 0; i < n; i++)
	{
		int num; 
		cin >> num;;
		arr.push_back(num); 
	}


	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (arr[i] < arr[j])
			{
				int temp = arr[i]; 
				arr[i] = arr[j]; 
				arr[j] = temp;
			}
		}
	}


	for (int i = 0; i < n; i++)
		cout << arr[i] << endl; 
	
	return 0;
}

'알고리즘 풀이' 카테고리의 다른 글

커트라인  (0) 2024.08.07
대표값2  (0) 2024.08.07
N과 M (1)  (0) 2024.08.07
N과 M (3)  (0) 2024.08.07
너의 평점은  (0) 2024.07.30