心胸决定格局,眼界决定境界...

快排的非递归

#include<stdio.h>
int partition(int a[], int l, int r){
	int key = a[l], i = l, j = r;
	while (i<j){
		while (i<j && a[j] >= key)
			j--;
		if (i<j)
			a[i++] = a[j];
		while (i<j && a[i] <= key)
			i++;
		if (i<j)
			a[j--] = a[i];
	}
	a[i] = key;
	return i;
}
void qsort(int a[], int l, int r){
	//boundary case
	if (l >= r)
		return;
	//state 0
	int mid = partition(a, l, r);
	qsort(a, l, mid - 1);
	//state 1
	qsort(a, mid + 1, r);
	//state 2
}

struct recorc{
	int l, r, mid;   //local virables. a[] never changed, no need to save.
	int state;
}stack_list[100000];


void nun_recursive_qsort(int a[], int l, int r){
	int state = 0, top = 0;
	int mid;
	while (1){
		if (l >= r){ //boundary case, return previous level
			if (top == 0)
				break;   //end of recursion
			top--;
			l = stack_list[top].l;        //end of function, update to previous state
			r = stack_list[top].r;
			mid = stack_list[top].mid;
			state = stack_list[top].state;
		}
		else if (state == 0){//当前状态为0
			mid = partition(a, l, r);
			stack_list[top].l = l;			//recursive call, push current state into stack
			stack_list[top].r = r;
			stack_list[top].mid = mid;
			stack_list[top].state = 1;//保存state=1时,所需要的状态
			top++;
			r = mid - 1;
			state = 0;            //don't forget to update state value   继续state=0的状态
			//(先入的总是state=0的状态,直到所有的state=0都处理完后,才进入state=1的状态)
		}
		else if (state == 1){
			stack_list[top].l = l;
			stack_list[top].r = r;			//recursive call, push current state into stack
			stack_list[top].mid = mid;
			stack_list[top].state = 2;//记录下一个状态
			top++;
			l = mid + 1;
			state = 0;
		}
		else if (state == 2){
			if (top == 0)
				break;   //end of recursion
			top--;
			l = stack_list[top].l;
			r = stack_list[top].r;
			mid = stack_list[top].mid;
			state = stack_list[top].state;
		}
	}
}


void my_nonrecusive_sort(int data[], int l, int r)
{
	int top = 0;
	int state = 0;
	int mid = 0;
	while (1)
	{

		if (l >= r)//注意等号
		{
			if (top == 0)
				break;
			top--;
			l = stack_list[top].l;
			r = stack_list[top].r;
			mid = stack_list[top].mid;
			state = stack_list[top].state;
			
		}//注意这几种状态必须在一个if里面
		else if (state == 0)
		{
			mid = partition(data , l, r);
			stack_list[top].l = l;
			stack_list[top].r = r;
			stack_list[top].mid = mid;
			stack_list[top].state = 1;
			r = mid - 1;
			top++;
			state = 0;
		}
		else if (state == 1)
		{
			stack_list[top].l = l;
			stack_list[top].r = r;
			stack_list[top].mid = mid;
			stack_list[top].state = 2;
			l = mid + 1;
			top++;
			state = 0;
		}
		else if (state == 2)
		{
			if (top == 0)
				break;
			top--;//注意top 不是放在最后,top标记了当前栈的元素个数,但是访问时,需要-1,出现后也要-1,所以自减
			l = stack_list[top].l;
			r = stack_list[top].r;
			mid = stack_list[top].mid;
			state = stack_list[top].state;
			
		}
	}

}


	int   data1[] = { 2, 31, 14, 6, 76, 88, 66, 12, 1 };
	//qsort(data1, 0, sizeof(data1) / sizeof(int)-1);
	//nun_recursive_qsort(data1, 0, sizeof(data1) / sizeof(int)-1);
	my_nonrecusive_sort(data1, 0, sizeof(data1) / sizeof(int)-1);
	for (int i = 0; i < sizeof(data1) / sizeof(int); i++)
	{
		cout<<data1[i]<<" ";
	}
	cout << endl;

  

posted @ 2019-02-04 15:44  WELEN  阅读(122)  评论(0)    收藏  举报