Skip to content

Commit 3023e2e

Browse files
Create quick_sort.m
1 parent 6cfc103 commit 3023e2e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

algorithms/sorting/quick_sort.m

+21
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)