ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Quick 정렬
    알고리즘 2022. 3. 31. 15:45

    피봇을 설정한 후 그 피봇보다 작은 수, 큰 수 들로 나누어 배열 만든 후 다시 그 배열 안에서 피봇 설정하고 같은 과정 반복하는 분할 정복 알고리즘

    public class QuickSort {
        public void quickSort(int[] arr, int left, int right) {
            int pl = left;
            int pr = right;
            int pivot = arr[(pl+pr)/2];
            while (pl <= pr) {
                while (pl > pivot) pl++;
                while (pr < pivot) pr--;
                if (pl <= pr) {
                    int tmp = arr[pl];
                    arr[pl] = arr[pr];
                    arr[pr] = tmp;
                }
            }
    
            if (pl < right) quickSort(arr, pl, right);
            if (pr > left) quickSort(arr, left, pr);
        }
    }

    '알고리즘' 카테고리의 다른 글

    Union-Find(합집합 찾기)알고리즘  (0) 2022.04.14
    Merge sort  (0) 2022.03.31
    Shell 정렬  (0) 2022.03.31
    삽입 정렬  (0) 2022.03.31
    선택 정렬  (0) 2022.03.31
Designed by Tistory.