preface

Since many algorithm problems in LeetCode involve some basic data structures, in order to better understand the animation of some complex problems updated later, a new series —– “Illustrated Data Structures” is launched, mainly using animation to describe common data structures and algorithms. This series includes ten kinds, heap, queue, tree, and search set, graph and so on about dozens of articles.

Quick sort

Quicksort is a sorting algorithm developed by Tony Hall. On average, order n two items and compare them (nlogn) several times. In the worst case, a comparison of TWO (N2) steps is required, but this is not common. In fact, quicksort is generally significantly faster than any other two (NLOGN) algorithms because its inner loop can be implemented efficiently on most architectures.

Quicksort uses a Divide and conquer strategy to Divide one serial (list) into two sub-lists.

Quicksort is a typical application of divide-and-conquer in sorting algorithm. Quicksort is essentially a recursive divide-and-conquer method based on bubble sort.

Algorithm steps

  1. Pick an element from the sequence, called pivot;

  2. Reorder the array so that all elements smaller than the base value are placed in front of the base value and all elements larger than the base value are placed behind the base value (the same number can go to either side). After the partition exits, the benchmark is in the middle of the array. This is called a partition operation;

  3. Recursively sorts subsequences of elements less than the base value and subsequences of elements greater than the base value;

The bottom case of recursion is if the sequence is of size zero or one, so it’s always sorted. The algorithm always exits, though recursively, because in each iteration it puts at least one element in its last position.

Source: github.com/hustcc/JS-S…

Algorithm demo

Sort animation process explanation

  1. First, you manipulate all the numbers in the sequence

  2. Choose a number out of all the numbers as the base for sorting (Pivot). Pivots are usually chosen at random, and here for demonstration purposes, we choose the rightmost number as pivot

  3. After pivot is selected, the leftmost number in the operation sequence is marked as the left mark and the rightmost number as the right mark

  4. Move the left marker to the right

  5. When the left mark reaches a number beyond pivot, the movement stops

  6. In this case, 8 is greater than 6, so stop moving

  7. Then move the marker on the right to the left

  8. When the right mark reaches a number less than pivot, the movement stops

  9. In this case, 4 is greater than 6, so stop moving

  10. When the left and right tags stop, change the number of tags

  11. Thus, the left marker is used to find a number greater than Pivot, and the right marker is used to find a number less than Pivot

  12. By swapping numbers, a collection of numbers smaller than Pivot can be collected on the left side of the array and larger than Pivot on the right

  13. After the exchange, continue to move the left tag

  14. In this case, 9 is greater than 6, so stop moving

  15. Then move the marker on the right to the left

  16. The right tag also stops moving when it hits the left tag

  17. If the left and right marks stop, and both are in the same position, swap this number with the pivot number

  18. This completes the first operation

  19. Anything less than 6 is to the left of 6, anything greater than 6 is to the right of 6

  20. And then recursively do the same thing to both parts of the partition

  21. Complete quicksort

Code implementation

In order to better let readers use their familiar programming language to understand animation, the author will post a variety of programming language reference code, code all from the Internet.

C++ code implementation

Java code implementation

Python code implementation

JavaScript code implementation

If you’re an iOS developer, you can go to GitHub at github.com/MisterBooo/… Get more intuitive debug run source code.

Welcome to: