Quick Sort

What is sorting ?

Sorting is the process of arranging numbers in ascending or descending order. Sorting can be done on numbers, names or records, and every sorting is done in some manner.

Quick sort

In this method, there is a pivot element. The position of the pivot element is fixed, and numbers less than it is placed at its left and the greater one placed at its right. They make two arrays in a quick sort array; one holds the smaller elements than the pivot, and another holds a greater one. The average time complexity of this method is o(n2). This method is useful for a large number of datasets.

  1. Choose the highest number as a pivot element.
  2. Take two variables to the left and right side of the array.
  3. Left points to low elements.
  4. Right points to high elements.
  5. When the value is less than pivot then move right.
  6. When the value is higher than pivot then move left.
  7. If both stem 5 and 6 don't match the swap left and right.
  8. If left >= right then meet a new pivot.

Advantages of Quick sort

  1. It requires n(log n) to sort n times.
  2. It has a short inner loop
  3. It uses a small stack for sorting.

Disadvantages of Quick sort

  1. It is a recursive method and takes time.
  2. It requires quadratic time.
  3. Simple mistakes can affect the performance very badly.