Lesson Overview

Welcome to the lesson dedicated to Quick Sort in Go. Sorting is a fundamental class of algorithms in computer science. Understanding different methods of sorting becomes more crucial as data sizes increase.

Quick Look at QuickSort

The QuickSort algorithm is designed to sort an unsorted slice by employing the Divide and Conquer Technique. The algorithm efficiently achieves this with an average time complexity of O(n log n). The idea behind it is to pick a pivot element from the slice and partition the other elements into two slices according to whether they are less than or greater than the pivot.

Here's an overview of how QuickSort works in Go:

  1. Pivot Selection: Choose a pivot element from the slice.

    • The pivot can be any element, but commonly used strategies include picking the last element or selecting a random element.
  2. Partitioning the Slice: Rearrange the slice such that:

    • All elements less than the pivot come before the pivot.
    • All elements greater than the pivot come after the pivot.
    • The pivot element is now in its correct sorted position.
  3. Recursive Sorting:

    • Recursively apply the quickSort function to the sub-slice of elements with values less than the pivot.
    • Recursively apply the quickSort function to the sub-slice of elements with values greater than the pivot.
Example:

Given the slice [10, 7, 8, 9, 1, 5], let's sort it using QuickSort:

  1. Choose Pivot: Let's pick the last element, 5, as the pivot.

  2. Partition:

    • Elements less than 5: [1]
    • Elements greater than 5: [10, 7, 8, 9]
    • Slice after partitioning: [1, **5**, 10, 7, 8, 9]
  3. Recursive QuickSort:

    • Apply QuickSort to [1] (Already sorted)
    • Apply QuickSort to [10, 7, 8, 9]
  4. Repeat the process for the sub-slice [10, 7, 8, 9], choosing pivots, partitioning, and recursing until the entire slice is sorted.

This approach ensures that the sorting process is performed efficiently by continually breaking down the problem into smaller subproblems, which are easier to solve.

We will use a helper function: partition.

Implementation of QuickSort in Go
Partition Breakdown

Let's break down the partition function step by step:

Function Definition

This line defines the partition function, which returns an integer. The function takes a slice of integers arr and two integers low and high as its parameters. These specify the sub-slice to be partitioned.


Initialize the Pivot

Select the last element in the sub-slice (indexed by high) as the pivot. The pivot is the reference value around which the slice will be partitioned.


Initialize the Partition Index

Initialize i to low - 1. This index keeps track of the boundary between elements smaller than the pivot and not yet positioned elements.


Iterate Over the Sub-slice

Use a for loop to iterate over the sub-slice from the low index to high - 1. This loop will compare each element with the pivot.


Quick Sort Breakdown

Let's break down the quickSort function step by step:

Function Definition

This line defines the quickSort function that returns nothing. The function takes a slice of integers arr and two integers low and high as its parameters. These specify the sub-slice to be sorted. The function does not return anything, but the input arr becomes sorted.


Base Case: Check if Sub-slice has More than One Element

The if condition ensures that the sub-slice has more than one element to sort. If low is not less than high, the function returns without making any changes. This is the base case for the recursion.


Partition the Sub-slice

Call the partition function to rearrange the elements so that all elements less than the pivot are on the left, and all elements greater are on the right. The function returns the index of the pivot element in its correct sorted position.


Recursively Sort Elements Before the Pivot

Recursively call the quickSort function on the sub-slice of elements before the pivot index. This ensures that this left sub-slice gets sorted.

Practice Time

Great job learning about quick sort! These foundational algorithms will not only reinforce your understanding of the sorting principle but are often used as a stepping stone to understand more complex algorithms. Happy learning, and let's sort it out!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal