1 of 7
user avatar
user avatar

What's wrong with this sorting algorithm? (Stack overflow error)

It won't compile properly. I'm testing this for fun. I know using this kind of pivot probably doesn't do anything. I'm trying to to find the largest value in the array in the interval from this -> original length / sqrt of the original length * log 2 (original length);

import java.lang.*;

public class QuickSort{
   int[] ary;

   public QuickSort(int[] num){
      ary = num;  
   }  
  
   public int[] sort(){ 
      int low = 0;
      int high = ary.length-1;
         quickSort(ary, low, high);
      return ary;
   }
     
   private void quickSort(int[] orig, int low, int high){
      
      int save = partition(orig, low, high);
   
      if (low < (save - 1)){
         quickSort(orig, low, (save-1)); 
      }
      if (save < high){
         quickSort(orig, save, high);
      }  
   }
   
   private int partition(int[] orig, int low, int high){  
      int i = low;
      int j = high;
      int temp;
      int large = 0;
      int swapspot = 0;
      
       int need = orig.length/((int)(Math.sqrt(ary.length))*((int)(Math.log(ary.length)/(Math.log(2)))));
       for(int q = need; q > need/2 - 1; q--){
          if(orig[q]>=orig[q-1]){
             orig[q] = large;
          }  else  {
             orig[q-1] = large;
          }
          if(q == need/2){
             int saver = (int)(large*.33);
             for(int h = 0; h < orig.length; h++){
                if(orig[h]>=saver){
                   swapspot = orig[h]; 
                }
             } 
          }
       }
       
         while (i <= j){
            while(orig[i] < swapspot){
               i++;
            }
            while(orig[j] > swapspot){
               j--;
            }
               if(i <= j){
                  temp = orig[i];
                  orig[i] = orig[j];
                  orig[j] = temp;
                  i++;
                  j--;
               }
            }  
         return i;  
      }
   }
user5900690