isn't the point of quicksort that you don't need extra memory? left and rightArray aren't necessary imo.
can you show this correction in code as i am not getting the pointπ
Sure,
class QuickSort { public sort(array: number[], left: number, right: number): number[] { if (array.length <= 1) { return array; } let index = this.partition(array, left, right); if (left < index - 1) { this.sort(array, left, index - 1); } if (index < right) { this.sort(array, index, right); } return array; } private partition(array: number[], left: number, right: number): number { const pivot = array[Math.floor(right + left / 2)]; let i = left; let j = right; while (i <= j) { while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { this.swap(array, i, j); console.log(`swapping ${i} ${j}`, array); i++; j--; } } return i; } private swap(array: number[], i: number, j: number) { const aux = array[i]; array[i] = array[j]; array[j] = aux; } } const q = new QuickSort(); const array = [5, 6, 3, 7, 1, 8, 9]; console.log(q.sort(array, 0, array.length - 1));
So this one is memory efficient
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
isn't the point of quicksort that you don't need extra memory? left and rightArray aren't necessary imo.
can you show this correction in code as i am not getting the pointπ
Sure,
So this one is memory efficient