---
title: "Quick Sort"
description: "Learn data structure by example of quick sort algorithm"
url: "https://www.geekboots.com/datastructure/quick-sort"
markdown_url: "https://www.geekboots.com/datastructure/quick-sort/md"
category: "Data Structure"
author: "Geekboots"
published: "2016-06-06"
updated: "2026-11-30T10:15:42.342Z"
tags: ["quick sort algorithm", "data structure", "algorithm", "quick sort"]
---

# Quick Sort (Data Structure)

> Learn data structure by example of quick sort algorithm

## Explanation

Quick Sort or partition-exchange sort is aptly named because, when properly implemented, it is the fastest known general-purpose in-memory sorting algorithm in the average case. Quick sort is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items.

In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quick Sort is often faster in practice than other O(n log n) algorithms. It does not require the extra array. [Quick Sort](https://www.geekboots.com/c/quick-sort) is widely used, and is typically the algorithm implemented in a library sort routine such as the UNIX qsort function.

Quick Sort is a divide and conquer algorithm. [Quick Sort](https://www.geekboots.com/python/quick-sort) first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quick Sort can then recursively sort the sub-arrays.

## Source Code

### quick-sort-algorithm.c

```c
function quicksort(array)
    if length(array) > 1
        pivot = select any element of array
        left = first index of array
        right = last index of array
        while left <= right
            while array[left] < pivot
                left = left + 1
            while array[right] > pivot
                right = right - 1
            if left <= right
                swap array[left] with array[right]
                left = left + 1
                right = right - 1
        quicksort(array from first index to right)
        quicksort(array from left to last index)
```

## Related Code Examples

- [Binary Tree](https://www.geekboots.com/datastructure/binary-tree) ([Markdown](https://www.geekboots.com/datastructure/binary-tree/md))
- [Queue](https://www.geekboots.com/datastructure/queue) ([Markdown](https://www.geekboots.com/datastructure/queue/md))
- [Stack](https://www.geekboots.com/datastructure/stack) ([Markdown](https://www.geekboots.com/datastructure/stack/md))
- [Linked list](https://www.geekboots.com/datastructure/linkedlist) ([Markdown](https://www.geekboots.com/datastructure/linkedlist/md))
- [Heap Sort](https://www.geekboots.com/datastructure/heap-sort) ([Markdown](https://www.geekboots.com/datastructure/heap-sort/md))
- [Merge Sort](https://www.geekboots.com/datastructure/merge-sort) ([Markdown](https://www.geekboots.com/datastructure/merge-sort/md))
- [Selection Sort](https://www.geekboots.com/datastructure/selection-sort) ([Markdown](https://www.geekboots.com/datastructure/selection-sort/md))
- [Insertion Sort](https://www.geekboots.com/datastructure/insertion-sort) ([Markdown](https://www.geekboots.com/datastructure/insertion-sort/md))

---
Source: [Geekboots](https://www.geekboots.com/datastructure/quick-sort)
