Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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_Algorithm_Pivot_Quicksort_Divide And Conquer - Fatal编程技术网

Java 使用第一个元素作为轴心的快速排序

Java 使用第一个元素作为轴心的快速排序,java,algorithm,pivot,quicksort,divide-and-conquer,Java,Algorithm,Pivot,Quicksort,Divide And Conquer,我通过选择第一个元素作为枢轴实现了快速排序。它对于一般的测试用例来说是很好的,但是考虑一个例子,当数组被反向排序时,例如实例5,4,3,2,1。我知道它在哪里抛出运行时错误。但是我不能正确地修理它。第一个元素作为pivot的实现是否正确?请建议修改 public static void quicksort(int low,int high) { if(low<high) { int temp=0; int pivot=a[low];

我通过选择第一个元素作为枢轴实现了快速排序。它对于一般的测试用例来说是很好的,但是考虑一个例子,当数组被反向排序时,例如实例5,4,3,2,1。我知道它在哪里抛出运行时错误。但是我不能正确地修理它。第一个元素作为pivot的实现是否正确?请建议修改

 public static void quicksort(int low,int high)
  {

   if(low<high) 
   {
    int temp=0;
    int pivot=a[low];                     
    int large_index=low+1;
    int small_index=high;

    while(large_index<=small_index)
    {
      while(a[small_index]>pivot)
      small_index--;

      while(a[large_index]<pivot) 
      large_index++; 

      if(large_index<=small_index)
       {
       temp = a[large_index];
       a[large_index]= a[small_index];
       a[small_index]= temp;
       large_index++;
       small_index--;
      }
     }

     temp = a[small_index];
     a[small_index]= a[low];
     a[low]= temp;


    quicksort(low,small_index-1);
    quicksort(small_index+1,high);
   }

   }
publicstaticvoidquicksort(int-low,int-high)
{

如果(低以下循环在
high
上方继续,则
大索引
已在轴上方开始,并且轴恰好大于位于轴之后的每个元素:

while(a[large_index]<pivot) 
large_index++; 

而[大索引]存在多个缺陷:

a) 循环外不必要的交换

温度=一个[小指数];一个[小指数]=一个[低];一个[低]=温度

b) 大索引的初始化不正确

c) 调用递归函数而不检查 小索引和大索引

我已经纠正了,

现在,函数将如下所示:

if (low < high) {
            int temp = 0;
            int pivot = a[low];
            int large_index = low;
            int small_index = high;

            while (large_index <= small_index) {
                while (a[small_index] > pivot)
                    small_index--;

                while (a[large_index] < pivot)
                    large_index++;

                if (large_index <= small_index) {
                    temp = a[large_index];
                    a[large_index] = a[small_index];
                    a[small_index] = temp;
                    large_index++;
                    small_index--;
                }
            }

            if(low < small_index)
            {
                quicksort(low, small_index);
            }
            if(large_index < high)
            {
                quicksort(large_index, high);
            }
        }
if(低<高){
内部温度=0;
int pivot=a[低];
int大_指数=低;
int小_指数=高;
while(大索引轴)
小_指数--;
while(一个[大索引]
导致边界的索引排列

这表明您对大索引的初始化是错误的。

它应该是:大指数=低;而不是低+1


希望这能有所帮助。

经过一些尝试,我能够实现快速排序功能,现在它运行良好。@Batshray我仍然在代码中使用了小的_index=low+1。所以我想这不是一个错误。最初的快速排序算法遵循了这个机制。有关参考,请观看普林斯顿大学教授Rob关于快速排序的讲座ert Sedgewick。请提醒编辑的快速排序算法是否存在缺陷

public static void quicksort(int low,int high)
 {

   if(low<high) 
   {
    int temp=0;
    int pivot=a[low];                     
    int large_index=low+1;
    int small_index=high;

    while(large_index<small_index)
  {

     while(a[large_index]<pivot) {
     if(large_index==high) 
     break;
     large_index++; 
   } 

    while(a[small_index]>pivot) 
   {
     if(small_index==low) 
     break;
     small_index--; 
   }

   if(large_index<small_index) {
     temp = a[large_index];
     a[large_index]= a[small_index];
     a[small_index]= temp;
     large_index++;
     small_index--; 
    }
  }   

    if(a[small_index]<pivot) {                               
       temp = a[small_index];
       a[small_index]= a[low];
       a[low]= temp;
  }

     if(low<small_index)                                    //Recursively sort the left half.
    {
     quicksort(low,small_index-1);
    }

    if(high>small_index)                                    //Recursively sort the right half.
   {
    quicksort(small_index+1,high);
    }
  }

  }
publicstaticvoidquicksort(int-low,int-high)
{

if(low)可能尝试使用调试器?至少提供stacktrace。缩进代码并删除不必要的代码。我得到了。这就是为什么我会得到运行时错误。但是如果将ex.while(a[large_index]这是行不通的。它仍然会为5 4 3 2 1抛出一个异常。@AnkurChandra至少提供堆栈跟踪的哪一部分你不明白吗?不是每个人都会试图阅读你的未插入代码,或者用敏锐的眼光轻易发现你的问题。对不起,Luiggi,但我是这里的初学者。我的代码使用记事本+。你能告诉我吗生成堆栈跟踪的方法?提前谢谢。@AnkurChandra堆栈跟踪是执行代码时控制台中出现的长消息。对于初学者,我建议使用Eclipse或IntelliJ Idea之类的IDE。当您获得更多经验时,请使用记事本++或其他文本编辑器。非常感谢。我发现了这些缺陷。您能e澄清为什么数组索引不在while中(一个[large_index]public static void quicksort(int low,int high) { if(low<high) { int temp=0; int pivot=a[low]; int large_index=low+1; int small_index=high; while(large_index<small_index) { while(a[large_index]<pivot) { if(large_index==high) break; large_index++; } while(a[small_index]>pivot) { if(small_index==low) break; small_index--; } if(large_index<small_index) { temp = a[large_index]; a[large_index]= a[small_index]; a[small_index]= temp; large_index++; small_index--; } } if(a[small_index]<pivot) { temp = a[small_index]; a[small_index]= a[low]; a[low]= temp; } if(low<small_index) //Recursively sort the left half. { quicksort(low,small_index-1); } if(high>small_index) //Recursively sort the right half. { quicksort(small_index+1,high); } } }