Computer Science/Data Structure, Algorithm

Algorithm] Quick Sort(퀵정렬)

TwinParadox 2015. 7. 11. 12:54
728x90

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");

}

728x90