快速排序非递归的思想是:
利用栈来完成,如果要排升序,可以先把右区间入栈,再把左区间入栈,当栈里面不为空时,取出当前栈顶的左右区间,并根据此时的左右区间,选出一个基准值key,再把key的左右两边分为两个区间,分别入栈,这样依次循环,直到左右区间只有一个数或没有数时,排序完毕。
具体代码如下:
- <Stack.h>
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top;//栈顶
int _capacity;
}Stack;
void StackInit(Stack* ps, int n);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDataType x);//在栈顶入数据
void StackPop(Stack* ps);//在栈顶出数据
STDataType StackTop(Stack* ps);//取出栈顶的数据
int StackSize(Stack* ps);//返回数据个数
int StackEmpty(Stack* ps);//如果是空 返回0;非空返回1
void StackTest();
- <Stack.c>
#include "Stack.h"
void StackInit(Stack* ps, int n)
{
assert(ps);
ps->_a = (STDataType*)malloc(sizeof(STDataType)*n);
ps->_top = 0;
ps->_capacity = n;
}
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}
void StackPush(Stack* ps, STDataType x)//在栈顶入数据
{
assert(ps);
if (ps->_top == ps->_capacity)//容量检测
{
ps->_a = (STDataType*)realloc(ps->_a, ps->_capacity * 2 * sizeof(STDataType));
ps->_capacity *= 2;
}
ps->_a[ps->_top] = x;
ps->_top++;
}
void StackPop(Stack* ps)//在栈顶出数据
{
assert(ps);
if (ps->_top > 0)
{
ps->_top--;
}
}
STDataType StackTop(Stack* ps)//取出栈顶的数据
{
assert(ps);
return ps->_a[ps->_top - 1];
}
int StackSize(Stack* ps)//返回数据个数
{
assert(ps);
return ps->_top;//top其实就是链表中的size
}
int StackEmpty(Stack* ps)
{
assert(ps);
if (ps->_top == 0)
{
return 0;
}
else
{
return 1;
}
//return ps->_top == 0 ? 0 : 1;
}
- <QuickSortR.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "Stack.h"
void QuickSortR(int* a, int left, int right);
void PrintArray(int* a, int n);
- <QuickSortR.c>
-
#include "QuickSortR.h" #include "Stack.h" void Swap(int* x, int* y) { int tmp = *x; *x = *y; *y = tmp; } int PartSort(int* a, int begin, int end) { int key = a[begin]; int start = begin; while (begin < end) { while (begin < end && a[end] >= key) { end--; } while (begin < end && a[begin] <= key) { begin++; } Swap(&a[begin], &a[end]); } //begin和end相遇时 Swap(&a[begin], &a[start]); return begin; } void QuickSortR(int* a, int left,int right) { Stack st; StackInit(&st,10); //先把区间入栈,保证栈内不为空 StackPush(&st, right); StackPush(&st, left); while (StackEmpty(&st) != 0) { int begin = StackTop(&st);//取出左边区间 StackPop(&st); int end = StackTop(&st); // 取出右边区间 StackPop(&st); int div = PartSort(a, begin, end);//根据此时的左右区间,选出中间的基准值key //[div+1,end] if (div + 1 < right)//如果只有一个元素或没有元素时,就没有左右区间,就不用入栈了 { StackPush(&st,right);//因为是升序,所以先入右边 StackPush(&st, div + 1); } //[begin,div-1] if (begin < div - 1) { StackPush(&st, div - 1); StackPush(&st, begin); } } } void PrintArray(int* a, int n) { for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); }
- <test.c>
#include "QuickSortR.h"
void SortTest()
{
int a[10] = { 5, 2, 4, 6, 1, 3, 8, 7, 9, 0 };
int sz = sizeof(a) / sizeof(a[0]);
printf("快速排序:>");
QuickSortR(a,0,sz-1);
PrintArray(a, sz);
}
int main()
{
SortTest();
system("pause");
return 0;
}
- 总结:
1.快速排序整体的综合性能和使用场景都是比较好的,所以才敢叫快速排序
2.时间复杂度: O(N*logN)
3.空间复杂度: O(logN)
4.稳定性:不稳定