1309 백종천
힘듦 힘듦;;
머리 안 아프게 정렬하자!
정렬? 분류?
분류 ▶
정렬
▼
Deep Dark Fantasy
깊게 깊게
정렬?
원소들을 일정한 순서로 열거(전산학, 수학)
2) 입력을 재배열하여 만든 순열이다.
순열(치환) : 순서가 부여된 임의의 집합을 다른 순서로 뒤섞는 연산
비교
교환
수면에 올라오는 거품
static void Main(string[] args)
{
int exchangeNumber = 0;
int compareNumber = 0;
int[] elements = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };
Console.Write("정렬 전 : ");
foreach (int i in elements)
Console.Write(i + " ");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < elements.Length - 1; i++)
{
for (int j = 0; j < elements.Length - 1 - i; j++)
{
compareNumber++;
if (elements[j] > elements[j + 1])
{
int temp = elements[j];
elements[j] = elements[j + 1];
elements[j + 1] = temp;
exchangeNumber++;
}
}
}
stopwatch.Stop();
Console.Write("\n정렬 후 : ");
foreach (int i in elements)
Console.Write(i + " ");
Console.WriteLine($"\n비교 횟수 {compareNumber} 교환 횟수 {exchangeNumber}");
Console.WriteLine($"수행 시간 {stopwatch.ElapsedTicks}Ticks(=100나노초)");
Console.ReadKey(true);
}
static void Main(string[] args)
{
int exchangeNumber = 0;
int compareNumber = 0;
int[] elements = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };
Console.Write("정렬 전 : ");
foreach (int i in elements)
Console.Write(i + " ");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < elements.Length - 1; i++)
{
int indexMin = i;
for (int j = i + 1; j < elements.Length; j++)
{
compareNumber++;
if (elements[j] < elements[indexMin])
{
exchangeNumber++;
indexMin = j;
}
}
int temp = elements[indexMin];
elements[indexMin] = elements[i];
elements[i] = temp;
}
stopwatch.Stop();
Console.Write("\n정렬 후 : ");
foreach (int i in elements)
Console.Write(i + " ");
Console.WriteLine($"\n비교 횟수 {compareNumber} 교환 횟수 {exchangeNumber}");
Console.WriteLine($"수행 시간 {stopwatch.ElapsedTicks}Ticks(=100나노초)");
Console.ReadKey(true);
}
직접 계산도 해볼 수 있다!
밀당!
static void Main(string[] args)
{
int exchangeNumber = 0;
int compareNumber = 0;
int[] elements = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };
Console.Write("정렬 전 : ");
foreach (int i in elements)
Console.Write(i + " ");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int index = 1; index < elements.Length; index++)
{
int temp = elements[index];
int aux = index - 1;
while ((aux >= 0) && (elements[aux] > temp))
{
compareNumber++;
elements[aux + 1] = elements[aux];
aux--;
}
elements[aux + 1] = temp;
exchangeNumber++;
}
stopwatch.Stop();
Console.Write("\n정렬 후 : ");
foreach (int i in elements)
Console.Write(i + " ");
Console.WriteLine($"\n비교 횟수 {compareNumber} 교환 횟수 {exchangeNumber}");
Console.WriteLine($"수행 시간 {stopwatch.ElapsedTicks}Ticks(=100나노초)");
Console.ReadKey(true);
}
static void Main(string[] args)
{
int exchangeNumber = 0;
int compareNumber = 0;
int[] elements = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };
Console.Write("정렬 전 : ");
foreach (int i in elements)
Console.Write(i + " ");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
QuickSort(ref elements, 0, elements.Length - 1, ref exchangeNumber, ref compareNumber);
stopwatch.Stop();
Console.Write("\n정렬 후 : ");
foreach (int i in elements)
Console.Write(i + " ");
Console.WriteLine($"\n비교 횟수 {compareNumber} 교환 횟수 {exchangeNumber}");
Console.WriteLine($"수행 시간 {stopwatch.ElapsedTicks}Ticks(=100나노초)");
Console.ReadKey(true);
}
static void QuickSort(ref int[] arr, int left, int right, ref int exchangeNumber, ref int compareNumber)
{
int i, j, pivot, tmp;
if (left < right)
{
i = left;
j = right;
pivot = arr[left];
//분할 과정
while (i < j)
{
while (arr[j] > pivot)
{
j--;
compareNumber++;
}
// 이 부분에서 arr[j-1]에 접근해서 익셉션 발생가능함
while (i < j && arr[i] <= pivot)
{
i++;
compareNumber++;
}
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
arr[left] = arr[i];
arr[i] = pivot;
compareNumber++;
exchangeNumber++;
//정렬 과정
QuickSort(ref arr, left, i - 1, ref exchangeNumber, ref compareNumber);
QuickSort(ref arr, i + 1, right, ref exchangeNumber, ref compareNumber);
}
public static void MergeSort(ref int[] input, int low, int high)
{
if (low < high)
{
int middle = (low / 2) + (high / 2);
MergeSort(ref input, low, middle);
MergeSort(ref input, middle + 1, high);
Merge(ref input, low, middle, high);
}
}
public static void MergeSort(ref int[] input)
{
MergeSort(ref input, 0, input.Length - 1);
}
private static void Merge(ref int[] input, int low, int middle, int high)
{
int left = low;
int right = middle + 1;
int[] tmp = new int[(high - low) + 1];
int tmpIndex = 0;
while ((left <= middle) && (right <= high))
{
if (input[left] < input[right])
{
tmp[tmpIndex] = input[left];
left = left + 1;
}
else
{
tmp[tmpIndex] = input[right];
right = right + 1;
}
tmpIndex = tmpIndex + 1;
compareNumber++;
}
if (left <= middle)
{
while (left <= middle)
{
tmp[tmpIndex] = input[left];
left = left + 1;
tmpIndex = tmpIndex + 1;
compareNumber++;
}
}
if (right <= high)
{
while (right <= high)
{
tmp[tmpIndex] = input[right];
right = right + 1;
tmpIndex = tmpIndex + 1;
compareNumber++;
}
}
for (int i = 0; i < tmp.Length; i++)
{
input[low + i] = tmp[i];
exchangeNumber++;
}
}
빠른 순(슬라이드) : 병합 < 퀵 < 버블 < 선택 < 삽입
빠른 순(실제) : 버블 = 선택 < 삽입 < 퀵 = 병합
은근 이번에 배운 수학 개념이...
사실 프로그래밍 영역 ㅎ;;
그냥 정렬합시다..!