快速排序非递归

栈溢出:递归多少层,建立多少个栈

栈不够,会溢出(深度太深)

非递归:循环

有些地方可以用队列,有些不能(二叉树)

所以要用栈,数组在堆上,数组指针不变,递归变的是区间,栈也存于区间。

快速排序非递归:每次单趟排完把子区间入栈。

所以我们要借助栈来实现非递归。

思路:

1.栈里面取一段区间,单趟排序;

2.单趟分割子区间入栈;

3.子区间只有一个值或不存在就不入栈。

下面我们来看一下代码:

先看栈的部分:

头文件:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

//#define N 10
//struct Stack
//{
//	int a[N];
//	int top;
//};

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

void STInit(ST* ps);
void STDestroy(ST* ps);
void STPush(ST* ps, STDataType x);
void STPop(ST* ps);
int STSize(ST* ps);
bool STEmpty(ST* ps);
STDataType STTop(ST* ps);//访问栈顶元素

中间部分:用Stack.c来写

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void STInit(ST* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = 4;
	ps->top = 0;//top栈顶元素的下一个位置
	//ps->top = -1;//top是栈顶元素位置
}

void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		//扩容
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity*2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	ps->a[ps->top] = x;
	ps->top++;
}



void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top - 1];
}

快排部分:用Sort来写:

//非递归
void QuickSortNonR(int* a, int left, int right)
{
	ST st;
	STInit(&st);
	STPush(&st,right);
	STPush(&st, left);

	while (!STEmpty(&st))
	{
		int begin = STTop(&st);
		STPop(&st);
		int end= STTop(&st);
		STPop(&st);
		int keyi = PartSort2(a, begin, end);
		//[begin,keyi-1],keyi,[keyi+1,end]
		if (keyi + 1 < end)
		{
			STPush(&st, end);
			STPush(&st, keyi+1);
		}
		if (begin < keyi)
		{
			STPush(&st, keyi-1);
			STPush(&st, begin);
		}
	}
	STDestroy(&st);
}

 主函数main部分:

void TestQuickSort()
{
	int a[] = { 6,1,2,7,9,3,4,5,10,8 };
	PrintArray(a, sizeof(a) / sizeof(int));
	QuickSortNonR(a, 0, sizeof(a) / sizeof(int) - 1);
	PrintArray(a, sizeof(a) / sizeof(int));
}
int main()
{
	TestQuickSort();

	return 0;
}

 以上就是快速排序非递归的全部代码啦!

你可以使用迭代实现快速排序非递归版本。下面是一个示例代码: ```cpp #include <iostream> #include <stack> using namespace std; // 交换两个元素的值 void swap(int& a, int& b) { int temp = a; a = b; b = temp; } // 快速排序非递归实现 void quickSort(int arr[], int low, int high) { stack<int> stk; stk.push(low); stk.push(high); while (!stk.empty()) { high = stk.top(); stk.pop(); low = stk.top(); stk.pop(); int pivot = arr[high]; int i = low - 1; for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); if (i + 1 < high) { stk.push(i + 2); stk.push(high); } if (i + 1 > low) { stk.push(low); stk.push(i); } } } // 打印数组 void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int arr[] = {5, 2, 8, 12, 3, 7}; int size = sizeof(arr) / sizeof(arr[0]); cout << "原始数组: "; printArray(arr, size); quickSort(arr, 0, size - 1); cout << "排序后数组: "; printArray(arr, size); return 0; } ``` 这段代码使用了一个栈来模拟递归调用的过程。在每一次循环中,取出栈中保存的low和high值,然后进行快速排序的划分操作。如果划分后的左半部分和右半部分的长度大于1,就将新的low和high值压入栈中,以便后续继续排序。通过循环不断处理栈中的任务,直到栈为空,即完成了整个排序过程。 注意,这段代码只是一个示例,实际使用时可能需要根据具体情况进行适当的修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值