Quick Sort(퀵 정렬, 퀵소트)
#include <stdio.h>
void Swap(int arr[], int idx1, int idx2)
{
int temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
int Partition(int arr[], int left, int right)
{
int piv = arr[left];
int low = left + 1, high = right;
while (low <= high)
{
while (piv > arr[low])
low++;
while (piv < arr[high])
high--;
if (low <= high)
Swap(arr, low, high);
}
Swap(arr, left, high);
return high;
}
void QuickSort(int arr[], int left, int right)
{
if (left <= right)
{
int piv = Partition(arr, left, right);
QuickSort(arr, left, piv - 1);
QuickSort(arr, piv + 1, right);
}
}
void main()
{
int arr[7] = { 3, 2, 4, 1, 7, 6, 5 };
int len = sizeof(arr) / sizeof(int);
int i;
QuickSort(arr, 0, sizeof(arr) / sizeof(int) - 1);
for (i = 0; i < len; i++)
printf("%d ", arr[i]);
printf("\n");
}
'Computer Science > Data Structure, Algorithm' 카테고리의 다른 글
Jungol] 1178 : 정수의 곱과 자릿수 (0) | 2015.07.20 |
---|---|
Algorithm] Insertion Sort(삽입정렬) (0) | 2015.07.11 |
Algorithm] Selection Sort(선택정렬) (0) | 2015.05.26 |
Algorithm] Bubble Sort(버블정렬, 거품정렬) (0) | 2015.05.26 |
Jungol] 1141 : 불쾌한 날 (0) | 2015.04.19 |