Quick Sort algorithm using recursive MATLAB function

The YouTube video for the quick sort algorithm can be found below:

MATLAB function for the recurssive Quick Sort algorithm:

function y = Quick_Sort(x)


if n<2
y = x;

x1 = [];
x2 = [];

for i = 1:n-1

if x(i)<x(n)
x1 = [x1 x(i)];
x2 = [x2 x(i)];

y = [Quick_Sort(x1) x(n) Quick_Sort(x2)];


The algorithm for the Quick Sort is:

Source: Wikipedia

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. The steps are:

  1. Pick an element, called a pivot, from the array.
  2. Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
  3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

The base case of the recursion is arrays of size zero or one, which are in order by definition, so they never need to be sorted.

The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm’s performance.

Leave a Reply