C언어 퀵정렬 구현

1 개요[ | ]

C 퀵정렬 구현
#include <stdio.h>
void quick_sort(int a[], int L, int R) {
    int left, right, pivot=a[L];
    for(left=L,right=R; left<right; right--) {
        while( a[right]>=pivot && left<right ) right--;
        if( left<right ) a[left] = a[right];
        while( a[left]<=pivot && left<right ) left++;
        if( left>=right ) break;
        a[right] = a[left];
    }
    a[left]=pivot;
    if(left>L) quick_sort(a, L, left-1);
    if(left<R) quick_sort(a, left+1, R);
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	quick_sort(arr, 0, size-1);
	for(int i=0; i<size; i++) printf("%d ", arr[i]);
	// -1 0 1 1 4 9 10 22 22 100 
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}