Java 快速排序分区调整

Java 快速排序分区调整,java,arrays,algorithm,sorting,quicksort,Java,Arrays,Algorithm,Sorting,Quicksort,但是我想弄清楚我怎么做,所以我可以选择我想要的轴心点,比如说在这个整数列表上,8,7,1,9,11,5,6,我希望在我的代码中选择说键6作为轴心点。或者如果我想选择9或者其他什么。我如何将其写入代码中?非常感谢您的帮助 package quicksort; public class quicky { private static void quicksort(int[] arr, int left, int right) { int index = partition(

但是我想弄清楚我怎么做,所以我可以选择我想要的轴心点,比如说在这个整数列表上,8,7,1,9,11,5,6,我希望在我的代码中选择说键6作为轴心点。或者如果我想选择9或者其他什么。我如何将其写入代码中?非常感谢您的帮助

package quicksort;

public class quicky {
    private static void quicksort(int[] arr, int left, int right) {
        int index = partition(arr, left, right);
    
        if(left < index - 1)
            quicksort(arr, left, index - 1);
        if(index < right)
            quicksort(arr, index, right);
    }
    private static int partition (int[] arr, int left, int right) {
        int pivot = arr[(left + right) / 2];
        while(left<= right) {
            while(arr[left] < pivot) left++;
            while(arr[right]> pivot) right--;
        
            if(left<= right) {
                int tmp = arr[left];
                arr[left] =arr[right];
                arr[right] = tmp;
            
                left++;
                right--;
            }
        }
        return left;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] array = new int [] { 8, 7, 1, 9, 11, 5, 6};
        quicksort(array, 0 , array.length-1);
        for(int i = 0; i <array.length; i++)
            System.out.print(array[i]+ " ");        
        }        
    }
}
包快速排序;
快速公开课{
私有静态void快速排序(int[]arr,int左,int右){
int index=分区(arr、左、右);
如果(左<索引-1)
快速排序(arr,左,索引-1);
如果(索引<右)
快速排序(arr,索引,右侧);
}
私有静态int分区(int[]arr,int左,int右){
int pivot=arr[(左+右)/2];
而(左枢轴)右--;
如果(左在代码中

private static int partition (int[] arr, int left, int right) {
/*
Here in below code only you need to make your changes and that needs to be through out the same as you are calling this from quicksort also you need to make use of the left and right for that, as everytime you will be passing that otherwise it will be static value. 

like you can do 
int pivot = arr[left] // for left most as pivot, if you want the last one then have arr[right] as pivot, right now you have mid element as pivot.
*/        
int pivot = arr[(left + right) / 2];
        

        while(left<= right) {
            while(arr[left] < pivot) left++;
            while(arr[right]> pivot) right--;
        
            if(left<= right) {
                int tmp = arr[left];
                arr[left] =arr[right];
                arr[right] = tmp;
            
                left++;
                right--;
            }
        }
        return left;
    }

私有静态int分区(int[]arr,int左,int右){
/*
在下面的代码中,您只需要进行更改,这需要通过与从快速排序调用此代码相同的方式完成。此外,您还需要使用左右键,因为每次您都要传递该值,否则它将是静态值。
就像你能做的一样
int pivot=arr[left]//对于最左边的作为轴,如果希望最后一个作为轴,则使用arr[right]作为轴,现在使用mid元素作为轴。
*/        
int pivot=arr[(左+右)/2];
而(左枢轴)右--;
如果(左简单方法:

  • 检查数组中是否存在值
  • 如果是,则将值与默认透视索引中的值交换
  • 继续使用相同的代码/lomuto分区

  • 如果所需的轴值不在数组中,会发生什么情况?请说明为什么要这样做?此外,还有一种称为随机快速排序的快速排序变体。您是否希望执行类似的操作?我希望能够显示轴点的工作方式,因此如果我想显示数组的一半是如何工作的基于轴心点排序。