快速排序

来源:互联网 发布:单片机将来工作 编辑:程序博客网 时间:2024/06/05 11:24
/**
     * 快速排序
     * @param arr
     * @param low
     * @param high
     */
    public void quickSort(int[] arr, int low, int high) {
        if (low > high) {
            return;
        }
        
        
        int left = low;
        int right = high;
        int key = arr[low];
        
        while (left < right) {
            
            while (arr[right] >= key && left < right) {
                right --;
            }
            
            while (arr[left] <= key && left < right) {
                left ++;
            }
            
            if (left < right) {
                int tmp = arr[right];
                arr[right] = arr[left];
                arr[left] = tmp;
            }
        }
        
        arr[low] = arr[left];
        arr[left] = key;
        
        quickSort(arr, low, right - 1);
        quickSort(arr, left + 1, high);
    }
0 0
原创粉丝点击