Bubbling and quick sort
1. Bubble sort
1. Principle
Before quick sorting, we have to understand a simple version of sorting with a similar idea, bubble sort (or bubble sort).
In bubble sorting, our idea is similar to blowing bubbles or throwing stones: from back to front, move the smallest (largest) value to the front (or back) until all elements are sorted, This sequence becomes orderly.
The time complexity of bubble sort is: O (n 2) O(n^2)O ( n2)
2. Sorting process
3. Code
Here is an example of a code that "sinks" the largest element to the end.
void BubbleSort(int arry[], int len)
{
int i, j, temp;
for (i = 0; i < len; ++i) // 一共要排的个数
{
for (j = 0; j < len - i - 1; ++j) // 从0开始到最后-1-i个元素
{
if (arry[j] > arry[j+1]) // 如果当前元素比后面元素大,则交换位置
{
temp = arry[j];
arry[j] = arry[j+1];
arry[j+1] = temp;
}
}
}
}
2. Quick sort
1. Principle
Quick sort uses the idea of recursion + bubble sort. The process is as follows:
- First, mark the start and end of the sequence table to be sorted as low, high pointers, and select [low, high] to take a position value as the pivot (or pivot) (pivotkey).
- Start from high (including high) and look forward, look for a value less than (or greater than) the pivot, and swap this value with the pivot.
- Start from low (including low) and search backwards, look for a value less than (or greater than) the pivot, and swap this value with the pivot.
- Repeat 2 and 3 until
high==low
orlow==high
when the pivot position is the final position, and the left side of the pivot is smaller than the pivot, and the right is larger than the pivot. - The two pieces on the left and right sides of the pivot are recursively
1234
operated. Finally, the array can be sorted
2. Sorting process
Then we perform recursive fast sorting on the new sequence 1:
At this time, the original array has become:
Since it is a recursive expression, our recursive stack should start sorting the new sequence 3:
At this time, the array status is:
and then the new sequence 5. :
Array state:
Then the recursive stack comes to a new sequence 4:
Then new sequence 6:
Finally, return to the new sequence 2 that was initially stacked:
Therefore, the overall flow chart is:
3. Performance analysis
I won’t look at the derivation for now, just remember the conclusion: T avg (n) <(b 2 + 2 c) (n + 1) ln (n + 1) n ≥ 2 T_{avg}(n) <(\frac{b } {2}+2c)(n+1)ln(n+1)\ \ \ n≥ 2Tavg(n)<(2b+2 c ) ( n+1 ) l n ( n+1) n≥2 , whichb c
is a constant, then our time complexity is:O(nlogn)
orders of magnitude. The average and best time complexity is nlog2n, and the worst case time complexity is n2. The space complexity can be optimized to: log2n.
If other methods are used for the selection of pivots, the performance of sorting in the worst case can be greatly improved.
Or一次划分
optimizeat thetime, you can also improve the worst-case performance.
But it will not be discussed here.
4. Code
I'm too lazy to write the code myself, copy one:
#include <stdio.h>
void quickSort(int arr[], int low, int high)
{
int first = low;
int last = high;
int key = arr[first];
if(low >= high)
return;
while(first < last)
{
while(first < last && arr[last] > key)
{
last--;
}
arr[first] = arr[last];
while(first < last && arr[first] < key)
{
first++;
}
arr[last] = arr[first];
}
arr[first] = key;
quickSort(arr, low, first-1);
quickSort(arr, first+1, high);
}
int main()
{
int i;
int a[10] = {
3, 1, 11, 5, 8, 2, 0, 9, 13, 81};
for(i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
quickSort(a, 0, 9);
for(i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}