N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.
어차피 기본적인 정렬 문제라서 가장 시간복잡도가 괜찮은 퀵정렬을 구현해보겠다.
using System.Text;
namespace BaekJoon
{
public class P2750
{
int partition(int[] list, int left, int right)
{
int pivot, temp;
int low, high;
low = left;
high = right;
pivot = list[left];
while (low < high)
{
while (low <= right && list[low] < pivot)
{
low++;
}
while (high >= left && list[high] > pivot)
{
high--;
}
if (low < high)
{
temp = list[low];
list[low] = list[high];
list[high] = temp;
if (list[low] == list[high])
high--;
}
}
return high;
}
void quickSort(int[] list, int left, int right)
{
if(left < right)
{
int q = partition(list, left, right);
quickSort(list, left, q - 1);
quickSort(list, q + 1, right);
}
}
static void Main(string[] args)
{
P2750 p2750 = new ();
int n = int.Parse(Console.ReadLine());
int[] list = new int[n];
for(int i = 0; i < n; i++)
{
list[i]= int.Parse(Console.ReadLine());
}
p2750.quickSort(list, 0, n - 1);
for (int i = 0; i < n; i++)
Console.WriteLine(list[i]);
}
}
}