all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,21 @@
|
|||
function sortedArray = quickSort(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 = [quickSort(less) pivot quickSort(greater)];
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function sortedArray = quickSort(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) = [];
|
||||
|
||||
sortedArray = [quickSort( array(array <= pivot) ) pivot quickSort( array(array > pivot) )];
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
quickSort([4,3,7,-2,9,1])
|
||||
|
||||
ans =
|
||||
|
||||
-2 1 3 4 7 9
|
||||
Loading…
Add table
Add a link
Reference in a new issue