Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA中优化快速排序(如果已排序,则在前面检查)_Java_Arrays_Sorting_Quicksort - Fatal编程技术网

在JAVA中优化快速排序(如果已排序,则在前面检查)

在JAVA中优化快速排序(如果已排序,则在前面检查),java,arrays,sorting,quicksort,Java,Arrays,Sorting,Quicksort,在使用快速排序对数组进行排序之前,我想检查数组是否已排序。 我总是在第77行得到stackoverflow,或者在第65行得到数组索引越界错误。 我的基本想法是检查前两个数字是否排序,然后检查第二个和第三个,依此类推。如果未对它们进行排序,则整个while循环应取消,并使用最后一个正确的排序值作为比较值,通过快速排序开始排序 public class customQuickSort { Runtime runtime = new Runtime(); private int[

在使用快速排序对数组进行排序之前,我想检查数组是否已排序。 我总是在第77行得到stackoverflow,或者在第65行得到数组索引越界错误。 我的基本想法是检查前两个数字是否排序,然后检查第二个和第三个,依此类推。如果未对它们进行排序,则整个while循环应取消,并使用最后一个正确的排序值作为比较值,通过快速排序开始排序

public class customQuickSort 
{
    Runtime runtime = new Runtime();

    private int[] a;
    private int n;
    boolean isSorted = true;
    int arraySortCount = 0;
    int x = 0;

    public customQuickSort(int[] unsorted)
    {
        sort(unsorted);
    }
    @Override
    public void sort(int[] a)
    {
        this.a=a;
        n=a.length;
        runtime.start();
        quicksort(0, n-1);
        runtime.end(getCounter());
    }

    private void quicksort (int lo, int hi)
    {
        int i=lo, j=hi;
        while(isSorted = true && arraySortCount < a.length-1)
        {
            if(a[arraySortCount] <= a[(arraySortCount+1)])
            {
                if(arraySortCount == a.length-2)
                {
                    System.out.println("array sorted ascending");
                }
            }
            else if(a[arraySortCount] >= a[(arraySortCount+1)])
            {
                if(arraySortCount == a.length-2)
                {
                    System.out.println("array sorted descending");
                }
            }
            else
            {
                isSorted = false;
                x=a[c(arraySortCount)];
                System.out.println("unsorted");
            }
            arraySortCount++;
        }
        if(isSorted == false)
        {

            while (i<=j)
            {    
                while (a[i]<x)
                {
                    i++; 
                }
                while (a[j]>x) 
                {
                    j--; 
                }
                if (i<=j)
                {
                    exchange(i, j);
                    i++; j--;
                }
            }

            if (lo<j) quicksort(lo, j);
            if (i<hi) quicksort(i, hi);
        }
    }

    private void exchange(int i, int j)
    {
        int t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
}
公共类customQuickSort
{
运行时=新运行时();
私人机构【】a;
私人int n;
布尔值isSorted=true;
int arraySortCount=0;
int x=0;
公共自定义快速排序(int[]未排序)
{
排序(未排序);
}
@凌驾
公共无效排序(int[]a)
{
这个a=a;
n=a.长度;
runtime.start();
快速排序(0,n-1);
end(getCounter());
}
私有void快速排序(int-lo,int-hi)
{
int i=低,j=高;
while(isSorted=true&&arraySortCount
Arrays.sort(T[], Comparator<? super T> c)

Arrays.sort(T[],comparator什么是第65行?第77行是什么?你在java中得到了stackoverflow吗?第65行是,而(a[j]>x)第77行是如果(我可能这是家庭作业或其他什么,他需要手工完成)。是的,这是一种家庭作业,我们必须了解如何优化我们自己的算法,所以这对我没有帮助:(好吧,很公平:)但是下次请说明确切的要求,这样更容易回答。