Top 10 Programming Algorithms (1)

Algorithm 1: Quick Sort Algorithm

Quicksort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. O(n2) comparisons are required in the worst case, but this is not common. In fact, quicksort is usually significantly faster than other O(n log n) algorithms because its inner loop can be implemented efficiently on most architectures.

Quicksort uses a divide and conquer strategy to divide a list into two sub-lists.

Algorithm steps:

1 Pick an element from the sequence, called a "pivot",

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

3 Recursive (recursive) sort the subarrays of elements smaller than the reference value and the subarrays of elements larger than the reference value.

The bottom case of recursion is that the size of the sequence is zero or one, which is always sorted. Although it keeps recursing, the algorithm will always exit, because in each iteration, it will put at least one element to its last position.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652312&siteId=291194637