Article Directory
The basic idea:
Take any one of the elements to be sorted sequence as a reference value to the reference element, the sequence into two subsequences. Left sequence element are smaller than the reference value, the right sequence elements are greater than the reference value, then repeat the sequence around the above process until all elements arranged up to the appropriate location.
Graphic:
Code:
Theme framework code:
//快排,我们需要依次递归
void QuickSort(int*a, int begin,int end)
{
if (begin > end)
return;
int key = PartSort(a, begin, end);// PartSort函数实现单次排序
QuickSort(a, begin, key - 1);
QuickSort(a, key + 1, end);
}
Single Sort function codes:
1. A method
// Swap function function for the exchange of two numbers
int PartSort(int* a, int begin, int end)
{
int key = begin;
while (begin < end)
{
while (begin<end&&a[end]>=a[key])
{
--end;
}
while (begin < end&&a[begin] <= a[key])
{
++begin;
}
Swap(&a[begin], &a[end]);
}
Swap(&a[begin], &a[key]);
return begin;
}
2. Method Two: pit-bit law
int PartSort(int* a, int begin, int end)
{
int key = a[begin];
while (begin<end)
{
while (begin < end&&a[end] >= key)
{
--end;
}
a[begin] = a[end];
while (begin < end&&a[begin] <= key)
{
++begin;
}
a[end] = a[begin];
}
a[begin] = key;
return begin;
}
3. Method three: before and after the Pointer
int PartSort3(int *a, int begin, int end)
{
int prev = begin - 1;
int cur = begin;
while (cur<end)
{ //以end处元素为key
if (a[cur] < a[end])
{
++prev;
Swap(&a[prev], &a[cur]);
++cur;
}
else
++cur;
}
++prev;
Swap(&a[prev], &a[end]);
return prev;
}
Method three simple codes:
int PartSort3(int *a, int begin, int end)
{
int prev = begin - 1;
int cur = begin;
while (cur<end)
{
if (a[cur] < a[end]&&++prev!=cur)
{
Swap(&a[prev], &a[cur]);
}
++cur;
}
Swap(&a[++prev], &a[end]);
return prev;
}
Through a process illustrated earlier, we have found to be sorted in reverse order as fast when the minimum discharge efficiency, time complexity is O (N ^ 2), however, this is the result of our selection key value caused. To address this situation, we can take the three digital method used.
Fast row optimization
Taking the number of three inter-cell + optimized (when less data, we can direct insertion sort, a higher efficiency)
three number code fetch implemented:
int GetMinIndex(int* a, int begin, int end)
{
int min = begin + ((end - begin) >> 1);
if (a[begin] < a[min])
{
if (a[min] < a[end])
{
return min;
}
else
{
if (a[begin] < a[end])
return end;
else
return begin;
}
}
else//a[begin]>a[min]
{
if (a[min]>a[end])
return min;
else
{
if (a[begin] < a[end])
return end;
else
return begin;
}
}
}
After the code is optimized to achieve faster row
int PartSort(int *a, int begin, int end)
{
int min = GetMinIndex(a, begin, end);
Swap(&a[min], &a[begin]);
int key = a[begin];
while (begin<end)
{
while (begin < end&&a[end] >= key)
{
--end;
}
a[begin] = a[end];
while (begin < end&&a[begin] <= key)
{
++begin;
}
a[end] = a[begin];
}
//begin和end一定相遇于坑的位置
a[begin] = key;
return begin;
}
void QuickSort(int*a, int begin, int end)
{
if (begin > end)
return;
if (end - begin + 1 <3)
{
Insertsort(a, end - begin + 1);
}
int key = PartSort3(a, begin, end);
//[begin,key-1] key [key+1,end]
QuickSort(a, begin, key - 1);
QuickSort(a, key + 1, end);
}
Code is non-recursive implementation of the method:
Achieved by means of a non-recursive stack portion starts to be sorted, the end of the index into the stack, showing some areas.
Stack interface code embodied herein, may refer to https://blog.csdn.net/weixin_43519514/article/details/104783366
void QuickSortNonr(int* a, int left, int right)
{
assert(a);
Stack s;
StackInit(&s);
StackPush(&s, left);
StackPush(&s, right);
while (!StackEmpty(&s))
{
int end = StackTop(&s);
StackPop(&s);
int begin = StackTop(&s);
StackPop(&s);
int key = PartSort(a, begin, end);
if (begin < key - 1)
{
StackPush(&s, begin);
StackPush(&s, key - 1);
}
if (key + 1 < end)
{
StackPush(&s, key+1);
StackPush(&s, end);
}
}
StackDestroy(&s);
}
Function code is used to achieve
void Swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
The insertion sort may be realized reference to the following URL
https://editor.csdn.net/md/?articleId=104770087