quicksort

39 阅读1分钟

static void quickSort(int[]arr,int left,int right) {

        int l = left;
        int r = right;
        int temp = 0;
        int pivot = arr[(left+right)/2];
        while (l < r) {
            while (arr[l]<pivot)
            {
                l++;
            }
            while (arr[r] > pivot)
            {
                r--;
            }
            if (l>=r) {
                break;
            }
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            if (arr[l]==pivot) {
                r--;
            }
            if (arr[r] == pivot) {
                l++;
            }
        }
        if (l == r)
        {
            r--;
            l++;
        }

        //left iteration
        if (left < r)
        {
            quickSort(arr, left, r);
        }
        //right iteration
        if (l < right)
        {
            quickSort(arr, l, right);
        }
        
    }