From 3023e2e9ff6558d0a3719fec7eaaa1be758f2dec Mon Sep 17 00:00:00 2001 From: puneet-pr-arya Date: Tue, 6 Oct 2020 00:25:54 +0530 Subject: [PATCH] Create quick_sort.m --- algorithms/sorting/quick_sort.m | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 algorithms/sorting/quick_sort.m diff --git a/algorithms/sorting/quick_sort.m b/algorithms/sorting/quick_sort.m new file mode 100644 index 0000000..010a383 --- /dev/null +++ b/algorithms/sorting/quick_sort.m @@ -0,0 +1,21 @@ +function sortedArray = quick_sort(array) + + if numel(array) <= 1 %If the array has 1 element then it can't be sorted + sortedArray = array; + return + end + + pivot = array(end); + array(end) = []; + + %Create two new arrays which contain the elements that are less than or + %equal to the pivot called "less" and greater than the pivot called + %"greater" + less = array( array <= pivot ); + greater = array( array > pivot ); + + %The sorted array is the concatenation of the sorted "less" array, the + %pivot and the sorted "greater" array in that order + sortedArray = [quick_sort(less) pivot quick_sort(greater)]; + +end \ No newline at end of file