avatarKaruna Sehgal

Summary

This blog post provides an overview of Quick Sort, explaining its association with algorithms, breaking down its steps, and offering an example, while also discussing its characteristics and importance in the context of sorting algorithms.

Abstract

The article is part of a series aimed at demystifying complex algorithmic concepts for programmers. It delves into Quick Sort, a popular divide-and-conquer sorting algorithm, by detailing its process of selecting a pivot element to partition an array into two sub-arrays, which are then recursively sorted. The author emphasizes Quick Sort's efficiency, with an average time complexity of O(nLogn), and its in-place nature, requiring no additional storage. Despite its occasional worst-case time complexity of O(n^2), Quick Sort is praised for its practical utility in sorting arrays. The post also references the author's previous work on related topics and teases future content on other sorting algorithms, such as bubble sort.

Opinions

  • The author acknowledges the difficulty of grasping algorithmic concepts, suggesting a personal struggle and the need for simplified explanations.
  • Quick Sort is presented as a fundamental algorithm in computer science, implying its significance in the field.
  • The author seems to advocate for the practicality of Quick Sort due to its average-case efficiency and in-place sorting capability.
  • There is an implicit endorsement of Quick Sort as a preferred method for array sorting, given its common usage.
  • The mention of upcoming blog posts on other sorting algorithms hints at the author's dedication to providing comprehensive coverage of the subject.

A Quick Explanation of Quick Sort

This blog post is a continuation of a series of blog posts about Algorithms, as it has been a hard concept for me to grasp as a programmer. Feel to check out the first blogpost about Algorithms, where I provide an introduction of what Algorithms are and an example of an algorithm and the second blog post about Data Structures, where I explained what are Data Structures and what are some types of Data Structures. Also check out the third blog post about Time Complexity and Space Complexity, which I provide an explanation of Time and Space Complexity. I have also written a blog post about Big O Notation. Recently I have written blog posts about Binary Search, Linear Search, Interpolation Search, Sorting Algorithms, Selection Sort, Insertion Sort and Merge Sort.

This blog post I will focus on Quick Sort. I will explain what Quick Sort is, how Quick Sort is associated with Algorithms, try to break down Quick Sort step by step and provide an example.

What is Quick Sort and how is it associated with Algorithms?

Quick Sort is a sorting algorithm, which is commonly used in computer science. Quick Sort is a divide and conquer algorithm. It creates two empty arrays to hold elements less than the pivot value and elements greater than the pivot value, and then recursively sort the sub arrays. There are two basic operations in the algorithm, swapping items in place and partitioning a section of the array.

Quick Sort Algorithm: Steps on how it works:

  1. Find a “pivot” item in the array. This item is the basis for comparison for a single round.
  2. Start a pointer (the left pointer) at the first item in the array.
  3. Start a pointer (the right pointer) at the last item in the array.
  4. While the value at the left pointer in the array is less than the pivot value, move the left pointer to the right (add 1). Continue until the value at the left pointer is greater than or equal to the pivot value.
  5. While the value at the right pointer in the array is greater than the pivot value, move the right pointer to the left (subtract 1). Continue until the value at the right pointer is less than or equal to the pivot value.
  6. If the left pointer is less than or equal to the right pointer, then swap the values at these locations in the array.
  7. Move the left pointer to the right by one and the right pointer to the left by one.
  8. If the left pointer and right pointer don’t meet, go to step 1.

Below is an image of an array, which needs to be sorted. We will use the Quick Sort Algorithm, to sort this array:

And here is a YouTube video which explains Quick Sort:

Quick Sort: An example

Here is an example of writing the Quick Sort Algorithm based on the steps I provided earlier. Below I have written a function, which accept the following parameter: an array. The function returns the sorted array.

Important Characteristics of Quick Sort:

  • Quick Sort is useful for sorting arrays.
  • In efficient implementations Quick Sort is not a stable sort, meaning that the relative order of equal sort items is not preserved.
  • Overall time complexity of Quick Sort is O(nLogn). In the worst case, it makes O(n2) comparisons, though this behavior is rare.
  • The space complexity of Quick Sort is O(nLogn). It is an in-place sort (i.e. it doesn’t require any extra storage)

Overall Quick Sort is an important concept to understand when it comes to algorithms. Thank you for reading this blog post. In upcoming blog posts of this series, I will go over other sorting algorithms like bubble sort.

Algorithms
Coding
Web Development
Programming
Coding Interviews
Recommended from ReadMedium