function sum(array) {
if(!array.length) { // base case
return 0;
}
return array[0] + sum(array.slice(1)); // reduce problem size
}
function count(array){
if(!array.length) { // base case
return 0;
}
return 1 + count(array.slice(1)); // reduce problem size
}
function findMax(array){
if(array.length === 1) { // base case
return array[0];
}
const subMax = findMax(array.slice(1)); // reduce problem size
return array[0] > subMax ? array[0] : subMax;
}
[33, 15, 10]
pivot
.pivot
33
.partitioning
, now we have:[15,10]
[]
sorted sub array
+ pivot
+ sorted sub array
quicksort([15,10]) + [33] + quicksort([])
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
mergesort(array, left, right)
mergesort(array, left, right)
if left > right
return
mid = (left + right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
step 4: Stop
print_items
: 2 4 6 8 10print_items2
: If there’s an array already sorted, quicksort will re-sort it again.
If you choose pivot from the first item:
If you choose the pivot from middle of the array: