Skip to content

fix(QuickSort): use low instead of hardcoded 0 in left recursive call#2

Merged
omprashantjain merged 1 commit into
mainfrom
prashant/fix-quicksort-low
Jun 21, 2026
Merged

fix(QuickSort): use low instead of hardcoded 0 in left recursive call#2
omprashantjain merged 1 commit into
mainfrom
prashant/fix-quicksort-low

Conversation

@omprashantjain

Copy link
Copy Markdown
Collaborator

Fixes #1

The left recursive call in quickSort used a hardcoded 0 as the lower bound instead of low:

quickSort(nums, 0, partitionIndex - 1);   // before
quickSort(nums, low, partitionIndex - 1); // after

Every left-partition call re-sorted the array from index 0 rather than from the start of the current sub-problem. The output stayed correct (the [0, low-1] prefix is already in place), but recursion work blew up to O(n²). On a descending array of 2000 elements the buggy version makes 1,001,001 quickSort invocations vs 3,999 after the fix — which is what can drive the reported StackOverflowError on large inputs.

Validated by compiling and running the sort on several arrays (including descending and a 2000-element descending case); all sort correctly, and recursion depth is back to expected.

Thanks to @UsaaryanByte07 for the clear report and root-cause trace.

The left recursive call passed a hardcoded 0 as the lower bound instead
of low, so every left-partition call re-sorted the array from index 0
rather than from the start of the current sub-problem. The result stayed
correct (the [0, low-1] prefix is already in place) but recursion work
blew up to O(n^2): on a descending array of 2000 elements the buggy
version makes 1,001,001 quickSort invocations vs 3,999 after the fix,
which is what can drive the reported StackOverflowError on large inputs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@omprashantjain omprashantjain merged commit 7a0fbe7 into main Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential bug in left recursive call of QuickSort

1 participant