Quicksort代码不断给我StackOverflowError

时间:2019-11-22 02:57:14

标签: java sorting recursion quicksort

我已经为快速排序编写了此代码,但是我遇到了StackOverflowError,但找不到原因。我试图确保递归具有正确的枢轴,前后值,这是我认为搞砸的地方,但目前我不太确定。

   {
       int intTemp = 0;
       int intPivotP = intPivot;
       int intFrontP = intFront;
       int intBackP = intBack;
       //System.out.println ("front");
       while (intFront != intBack - 1 && intFront < intBack)
       {
           //System.out.println ("while");
           if (aClone [intFront] <= aClone [intPivot] && aClone [intBack] >= aClone [intPivot])
           {
               intFront = intFront + 1;
               intBack = intBack - 1;
           }

           else if (aClone [intFront] > aClone [intPivot] && aClone [intBack] >= aClone [intPivot])
           {
               intBack = intBack - 1;
           }

           else if (aClone [intFront] <= aClone [intPivot] && aClone [intBack] < aClone [intPivot])
           {
               intFront = intFront + 1;
           }

           else if (aClone [intFront] > aClone [intPivot] && aClone [intBack] < aClone [intPivot])
           {
               intTemp = aClone [intBack];
               aClone [intBack] = aClone [intFront];
               aClone [intFront] = intTemp;
           }
       }

       if (intFrontP < intBackP)
       {
           //quick ((intBackP - intFrontP) / 2 + intFrontP, intFrontP, intPivotP - 1);
           quick ((intPivotP - intFrontP) / 2 + intFrontP, intFrontP, intPivotP - 1);
           quick ((intBackP - intPivotP) / 2 + intPivotP, intPivotP, intBackP);
       }
   }

为澄清起见,aClone是一个整数数组。

0 个答案:

没有答案