Stackoverflow with Quicksort Java implementation

前端 未结 9 1510
一生所求
一生所求 2020-12-06 19:25

Having some problems implementing quicksort in java. I get a stackoverflow error when I run this program and I\'m not exactly sure why. If anyone can point out the error, it

9条回答
  •  隐瞒了意图╮
    2020-12-06 19:38

    int partition(int array[], int too_big_index, int too_small_index)
    {
         int x = array[too_big_index];
         int i = too_big_index;
         int j = too_small_index;
         int temp;
         do
         {                 
             while (x array[i])
             {
                  i++;
             } 
              if (i < j)
             { 
                     temp = array[i];    
                     array[i] = array[j];
                     array[j] = temp;
             }
         }while (i < j);     
         return j;           // middle  
    }
    
    void QuickSort(int num[], int too_big_index, int too_small_index)
    {
          // too_big_index =  beginning of array
          // too_small_index = end of array
    
         int middle;
         if (too_big_index < too_small_index)
        {
              middle = partition(num, too_big_index, too_small_index);
              QuickSort(num, too_big_index, middle);   // sort first section
              QuickSort(num, middle+1, too_small_index);    // sort second section
         }
         return;
    }
    
    
    
    void main()
    {
        int arr[]={8,7,13,2,5,19,1,40,12,34};
    
        QuickSort(arr,0,9);
        for(int i=0;i<10;i++)
             System.out.println(arr[i]);
    }
    

提交回复
热议问题