feat(guide): added java implementation to quicksort (#36005)
* feat: added java implementation to quicksort * fix: edited heading in quicksort
This commit is contained in:
committed by
Quincy Larson
parent
07d9862c0e
commit
830def0604
@ -185,6 +185,46 @@ function [pInd, unsorted] = partition(unsorted, low, high)
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
```
|
||||||
|
### Implementation in Java
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Quicksort {
|
||||||
|
|
||||||
|
static void quickSort(int[] array, int p, int r) {
|
||||||
|
if (p < r) {
|
||||||
|
int q = partition(array, p, r);
|
||||||
|
quickSort(array, p, q - 1);
|
||||||
|
quickSort(array, q + 1, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int partition(int[] array, int p, int r) {
|
||||||
|
// using the last element as the pivot
|
||||||
|
int pivot = array[r];
|
||||||
|
int i = p - 1;
|
||||||
|
for (int j = p; j <= r - 1; j++) {
|
||||||
|
if (array[j] <= pivot) {
|
||||||
|
i++;
|
||||||
|
int temp = array[i];
|
||||||
|
array[i] = array[j];
|
||||||
|
array[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int temp = array[i + 1];
|
||||||
|
array[i + 1] = array[r];
|
||||||
|
array[r] = temp;
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int [] array = {2,8,7,1,3,5,6,4};
|
||||||
|
quickSort(array, 0, 7);
|
||||||
|
for (int i : array)
|
||||||
|
System.out.print(i + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The space complexity of quick sort is `O(n)`. This is an improvement over other divide and conquer sorting algorithms, which take `O(nlong(n))` space. Quick sort achieves this by changing the order of elements within the given array. Compare this with the <a href='https://guide.freecodecamp.org/algorithms/sorting-algorithms/merge-sort' target='_blank' rel='nofollow'>merge sort</a> algorithm which creates 2 arrays, each length `n/2`, in each function call.
|
The space complexity of quick sort is `O(n)`. This is an improvement over other divide and conquer sorting algorithms, which take `O(nlong(n))` space. Quick sort achieves this by changing the order of elements within the given array. Compare this with the <a href='https://guide.freecodecamp.org/algorithms/sorting-algorithms/merge-sort' target='_blank' rel='nofollow'>merge sort</a> algorithm which creates 2 arrays, each length `n/2`, in each function call.
|
||||||
@ -209,4 +249,3 @@ The space complexity of quick sort is O(n). This is an improvement over other di
|
|||||||
- <a href='https://www.youtube.com/watch?v=SLauY6PpjW4' target='_blank' rel='nofollow'>Youtube: Gayle Laakmann McDowell (author of Cracking The Coding Interview) explains the basics of quicksort and show some implementations</a>
|
- <a href='https://www.youtube.com/watch?v=SLauY6PpjW4' target='_blank' rel='nofollow'>Youtube: Gayle Laakmann McDowell (author of Cracking The Coding Interview) explains the basics of quicksort and show some implementations</a>
|
||||||
|
|
||||||
- <a href='https://www.youtube.com/watch?v=COk73cpQbFQ' target='_blank' rel='nofollow'>Quick Sort - MyCodeSchool</a>
|
- <a href='https://www.youtube.com/watch?v=COk73cpQbFQ' target='_blank' rel='nofollow'>Quick Sort - MyCodeSchool</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user