We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 167ec29 + 3023e2e commit 6f129b8Copy full SHA for 6f129b8
algorithms/sorting/quick_sort.m
@@ -0,0 +1,21 @@
1
+function sortedArray = quick_sort(array)
2
+
3
+ if numel(array) <= 1 %If the array has 1 element then it can't be sorted
4
+ sortedArray = array;
5
+ return
6
+ end
7
8
+ pivot = array(end);
9
+ array(end) = [];
10
11
+ %Create two new arrays which contain the elements that are less than or
12
+ %equal to the pivot called "less" and greater than the pivot called
13
+ %"greater"
14
+ less = array( array <= pivot );
15
+ greater = array( array > pivot );
16
17
+ %The sorted array is the concatenation of the sorted "less" array, the
18
+ %pivot and the sorted "greater" array in that order
19
+ sortedArray = [quick_sort(less) pivot quick_sort(greater)];
20
21
+end
0 commit comments