Sorting Algorithms in Go

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

package main

import (
    "fmt"
)

// Partition function that makes use of pivot
func partition(arr []int, low, high int) int {
    pivot := arr[high] // pivot
    i := low - 1       // Index of smaller element

    for j := low; j < high; j++ {
        // If the current element is smaller than or equal to the pivot
        if arr[j] <= pivot {
            i++
            arr[i], arr[j] = arr[j], arr[i]
        }
    }
    arr[i+1], arr[high] = arr[high], arr[i+1]
    return i + 1
}

// The main function that implements QuickSort
func quickSort(arr []int, low, high int) {
    if low < high {
        // pi is the partitioning index, arr[pi] is now at the right place
        pi := partition(arr, low, high)
        // Separately sort elements before partition and after partition
        quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)
    }
}

func main() {
    arr := []int{10, 7, 8, 9, 1, 5}
    n := len(arr)
    quickSort(arr, 0, n-1)
    fmt.Printf("Sorted array: %v\n", arr)  // Sorted array: [1, 5, 7, 8, 9, 10]
}
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