-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: improve space complexity of QuickSort algorithm #1704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DevAnuragT
wants to merge
1
commit into
TheAlgorithms:master
from
DevAnuragT:improve-quicksort-space-complexity
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,61 @@ | ||
/** | ||
* @function QuickSort | ||
* @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy. | ||
* It sorts the array in place by using a partitioning technique instead of creating separate arrays. | ||
* @param {Integer[]} items - Array of integers | ||
* @param {Integer} left - Starting index | ||
* @param {Integer} right - Ending index | ||
* @return {Integer[]} - Sorted array. | ||
* @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | ||
*/ | ||
function quickSort(items) { | ||
const length = items.length | ||
|
||
if (length <= 1) { | ||
function quickSort(items, left = 0, right = items.length - 1) { | ||
if (left >= right) { | ||
return items | ||
} | ||
const PIVOT = items[0] | ||
const GREATER = [] | ||
const LESSER = [] | ||
|
||
for (let i = 1; i < length; i++) { | ||
if (items[i] > PIVOT) { | ||
GREATER.push(items[i]) | ||
} else { | ||
LESSER.push(items[i]) | ||
|
||
const pivotIndex = partition(items, left, right) | ||
quickSort(items, left, pivotIndex - 1) | ||
quickSort(items, pivotIndex + 1, right) | ||
|
||
return items | ||
} | ||
|
||
/** | ||
* @function partition | ||
* @description Partitions the array in place and returns the index of the pivot. | ||
* @param {Integer[]} items - Array of integers | ||
* @param {Integer} left - Starting index | ||
* @param {Integer} right - Ending index | ||
* @return {Integer} - Pivot index. | ||
*/ | ||
function partition(items, left, right) { | ||
const PIVOT = items[right] // Choose the rightmost element as pivot | ||
let partitionIndex = left | ||
|
||
for (let i = left; i < right; i++) { | ||
if (items[i] < PIVOT) { | ||
swap(items, i, partitionIndex) | ||
partitionIndex++ | ||
} | ||
} | ||
|
||
const sorted = [...quickSort(LESSER), PIVOT, ...quickSort(GREATER)] | ||
return sorted | ||
// Move the pivot to its correct position | ||
swap(items, partitionIndex, right) | ||
|
||
return partitionIndex | ||
} | ||
|
||
/** | ||
* @function swap | ||
* @description Helper function to swap two elements in the array. | ||
* @param {Integer[]} items - Array of integers | ||
* @param {Integer} i - Index of the first element | ||
* @param {Integer} j - Index of the second element | ||
*/ | ||
function swap(items, i, j) { | ||
const temp = items[i] | ||
items[i] = items[j] | ||
items[j] = temp | ||
} | ||
|
||
export { quickSort } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.