본문 바로가기
Mobile Dev.

[Java]Quicksort

by prelude618 2024. 3. 17.

static void quickSort(int[] ar) {
    quickSort(ar, 0, ar.length - 1);
}

static void quickSort(int[] ar, int st, int de) {
    if(st >= de) {
        return;
    }

    int pivot = st;
    int end = de;
    int i = st + 1;
    while(i <= end){
        if(ar[i] <= ar[pivot]){
            int temp = ar[i];
            ar[i] = ar[pivot];
            ar[pivot] = temp;
            pivot = i;
            i++;
        } else {
            int temp = ar[end];
            ar[end] = ar[i];
            ar[i] = temp;
            end--;
        }
    }

    quickSort(ar, st, pivot - 1);
    quickSort(ar, pivot + 1, de);
}
반응형

댓글