We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6cfc103 commit 3023e2eCopy full SHA for 3023e2e
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