I keep getting a stackOverflow error in my quickSort and I can not figure out why. Any help would be greatly appreciated!!

private static void quickSortRecPriv(int[] arr, int first, int last, int ps) {
		int pivot = arr[first];
		int left = first;
		int right = last;
		
		if (arr == null || first < 0 || last >= arr.length - 1) {
			throw new IllegalArgumentException();
		}
		if (arr.length < 1)
		{
			return;
		}
		if(arr.length < ps)
		{
			InsertionSorter.insertionSort(arr);
			return;
		}
		while ( true )
		  {
		    while ( arr[left] < pivot )
		       left++;
		    while ( arr[right] > pivot )
		       right--;
		    if ( left < right )
		    {  int t = arr[left];
		       arr[left++] = arr[right];
		       arr[right--] = t;
		    } 
		    else
		       break;
		  }
		pivot = partition(arr, first, last, ps);
	      quickSortRecPriv(arr, first, right, ps);
	      quickSortRecPriv(arr, right + 1, last, ps);
	}

	private static int partition(int[] arr, int first, int last, int ps) {

		if (first == last) return first;
        int i = first - 1;
        int j = last;
        while (true) {
            while (arr[++i] < arr[last])       
                ;                           
            while (arr[last] < arr[--j])
                if (j == first) break;
            if (i >= j) break;
            swap(arr, i, j);
        }
        swap(arr, i, last);
        return i;
    }

Your recursion must be looping indefinitely, so add some print statements to show the values of the key variables on each iteration.

This article has been dead for over six months. Start a new discussion instead.