非递归快排
我们知道递归是非常占内存的,上面的递归每次调用都是在内存中栈上创建的,栈并没有那么大,所以很容易栈溢出。
我们要借助数据结构的栈来实现非递归快排(数据结构的栈实在内存中的堆上创建的),因为递归的二叉树中,是区间控制了整个数组的排序,所以想实现非递归二叉树就要在栈里面存放区间。(要注意栈的特新:先进后出)
这是在栈内出入顺序。(只针对上面的例图)
//stack.h #include <stdio.h> #include <assert.h> #include <stdlib.h> #include <stdbool.h> typedef int SD;//随时更改数据类型 typedef struct stack { SD* a;//数组 int top;//栈顶 int capacity;//容量 }ST; void StackInit(ST* ps);//初始化 void StackPush(ST* ps, SD x);//入栈 void StackPop(ST* ps);//出栈 SD StackTop(ST* ps);//获取栈顶元素 int StackSize(ST* ps);//获取栈中有效的元素 bool StackEmpty(ST* ps);// 检测栈是否为空 void StackDestroy(ST* ps);//销毁栈 void Quicksort(int* a, int start, int end); //stack.c #include "stack.h" void StackInit(ST* ps)//初始化 { assert(ps); ps->a = NULL; ps->capacity = 0; ps->top = 0; } bool StackEmpty(ST* ps)//判断 { assert(ps); return ps->top == 0; } void StackPush(ST* ps, SD x)//入栈 { assert(ps); //扩容 if (ps->capacity == ps->top) { int count = (ps->capacity == 0 ? ps->capacity = 4 : ps->capacity * 2); SD* y = (SD*)realloc(ps->a, count * sizeof(SD)); if (y == NULL) { perror("realloc fail"); exit(-1); } ps->a = y; ps->capacity = count; } ps->a[ps->top] = x;//赋值 ps->top++;//让栈顶往后走一步 } void StackPop(ST* ps)//出栈 { assert(ps); assert(!StackEmpty(ps)); ps->top--; } void StackDestroy(ST* ps)//销毁栈 { assert(ps); free(ps->a); ps->a = NULL; ps->capacity = 0; ps->top = 0; } SD StackTop(ST* ps)//获取栈顶元素 { assert(ps); return ps->a[ps->top - 1]; } int StackSize(ST* ps)//获取栈中有效的元素 { assert(ps); return ps->top; } //源.c #include "stack.h" void sort(int* arr, int n) { int i = 0;//控制end for (i = 0; i < n - 1; i++) { int end = i;//这个是两个数比较中的第一个数 int s = arr[end + 1];//保留的是两个数的后面一个数 while (end >= 0) { if (arr[end] > s)//比较两个数的大小 { arr[end + 1] = arr[end];//如果不是从大到小就重新排序 end--; } else { break; } } arr[end + 1] = s;//如果顺序没错就将储存起来的数据放到指定位置,这么写的好处还有一个是end最坏的情况会到-1 } } void Swap(int* a, int* b) { int c = *a; *a = *b; *b = c; } int middle(int* a, int start, int end)//取中间数 { int mid = (start + end) / 2; if (a[start] < a[mid]) { if (a[start] > a[end])//mid最大,end最小 { return start; } else if (a[end] > a[mid])//end最大,start最小 { return mid; } else//mid最大,start最小 { return end; } } else//a[start] >= a[mid] { if (a[end] > a[start])//end最大,mid最小 { return start; } else if (a[mid] > a[end])//start最大,end最小 { return mid; } else//start最大,mid最小 { return end; } } } int PartSort2(int* a, int start, int end) { int mid = middle(a, start, end); Swap(&a[start], &a[mid]); int key = start; int cur = start + 1;//前指针 int prve = start;//后指针 while (cur <= end) { if (a[cur] < a[key] && ++prve != cur)//如果cur小就停下与prve交换,前提是++prve不能与cur相等,防止自己与自己交换 Swap(&a[cur], &a[prve]); cur++;//如果cur大就继续向前走 } Swap(&a[prve], &a[key]);//cur走出范围key与prve交换 return prve; } void Quicksort(int* a, int start, int end) { ST s; StackInit(&s);//初始化栈 StackPush(&s, start); StackPush(&s, end);//入栈第一个区间 while (!StackEmpty(&s))//如果栈空就停止 { int right = StackTop(&s);//获取存入元素 StackPop(&s);//出栈,end int left = StackTop(&s); StackPop(&s);//strat int key = PartSort2(a, left, right);//单排 if (key + 1 < right)//判断元素是否大于一个 { StackPush(&s, key + 1);//右子树的右区间 StackPush(&s, right);//入栈右子树的左区间 } if (left < key - 1) { StackPush(&s, left);//左子树右区间 StackPush(&s, key - 1);//左子树左区间 } } } int main() { int arr[] = { 6,2,7,1,3,4,10,8 }; int n = sizeof(arr) / sizeof(arr[0]); Quicksort(arr, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
时间复杂度为O(N*logN).
归并排序
递归
基本思想:
先拆分剩一个元素,然后将他们两个一组的进行排序,两个元素分别为不同组,进行排序合并变成了一组,如上图。
这也是二叉树的后序遍历,那么在比较两组数的时候要怎么进行重新排序呢?
这里要创建一个数组,因为如果在原数组进行排序可能会产生原本的值被覆盖,处理起来很麻烦。
我们创建一个和原数组一样大的空间,哪里排序就放在哪个位置,并且,从小到大进行尾插:
这里还要注意,每两组排序完毕记得覆盖掉原来数组中的值。
代码实现:
#include <stdio.h> #include <stdlib.h> #include <string.h> void mergesort(int* a, int start, int end, int* tmp) { if (start >= end) { return;//遇到一个元素就返回 } int mid = (start + end) / 2; mergesort(a, start, mid, tmp);//左子树 mergesort(a, mid + 1, end, tmp);//右子树 int i = start;//新数组的下标 int left1 = start;//第一组开始 int right1 = mid;//第一组结束 int left2 = mid + 1;//第二组开始 int right2 = end;//第二组结束 while (left1 <= right1 && left2 <= right2)//尾插进新的数组 { if (a[left1] <= a[left2]) { tmp[i++] = a[left1++]; } else { tmp[i++] = a[left2++]; } } while (left1 <= right1)//哪组没走完继续走 { tmp[i++] = a[left1++]; } while (left2 <= right2) { tmp[i++] = a[left2++]; } memcpy(a + start, tmp + start, sizeof(int) * (end - start + 1));//将新数组的内容拷贝到原数组 } void _mergesort(int* a, int start, int end)//空数组 { int* tmp = (int*)malloc((end + 1) * sizeof(int)); if (tmp == NULL) { perror("malloc error"); exit(-1); } mergesort(a, start, end, tmp); free(tmp); tmp = NULL; } int main() { int arr[] = { 6,2,7,1,3,4,10,8 }; int n = sizeof(arr) / sizeof(arr[0]); _mergesort(arr, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
时间复杂度O(N*logN).





