[C ++] - quick sort Detailed

This blog describes the process quick sort, because the Internet read a lot, I feel some bloggers are really misleading scholars, painting is a bubble diagram or other sort of map, seriousness and talking fast row, really self-deception. This is not to say, when I was happy to read other people's text description, find the code written is a mess, a waste of my review time, well, clothed yourself.

- Quick basic idea:
by the primary sorting the entire unordered list into two portions independent of each other, a small portion of the data wherein the data than the other part contains, and then continue with this method were two parts of the same operation until each part can not be divided, the entire sequence of the ordered sequence obtained becomes.

For example the sequence: 4,938,659,776,132,749

  • Providing two pointers low and high, ratio of points to point unordered list table headers and footers
    Here Insert Picture Description

  • We first reference number 49, first high pointer traversing right to left, the first to find a smaller number than 49, i.e., is 27, and a digital low pointed exchange
    Here Insert Picture Description

  • The second step from low beginning, from left to right to start looking for larger figure than the 49, that is 65, and then the high point of the digital exchange
    Here Insert Picture Description

  • The third step is continued from the high start, looking smaller than 49 numbers, i.e. 13 is then pointed and low digital exchange
    Here Insert Picture Description

  • The fourth step from the low to the right traversal to find more than 49 large numbers, i.e. 97 is then pointed and digital exchange high
    Here Insert Picture Description
    when low and high point to the same position, the sort of the trip has been completed, 49 as the center, the unordered list divided into two parts, and then the same method, the two parts of the left and right sort.

#include <iostream>
#include <stdlib.h>
#include <stack>
#include <assert.h>
using namespace std;

void QuickSort1(int *a,int left, int right)
{
	if (left < right)
	{
		int low = left;
		int high = right;
		int temp = a[low];
		while (low != high)
		{
			while (low < high&&a[high] >= temp)
				high--;
			if (a[high] < temp)
			{
				int ret = a[high];
				a[high] = a[low];
				a[low] = ret;
			}
			while (low < high&&a[low] <= temp)
				low++;
			if (a[low] > temp)
			{
				int ret = a[high];
				a[high] = a[low];
				a[low] = ret;
			}
		}
		a[low] = temp;
		QuickSort1(a, left, low - 1);
		QuickSort1(a, low + 1, right);
	}
}
void prit(int *a,int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	int a[9] = { 49, 38, 65, 97, 76, 13, 27, 49 };
	cout << "原数组是:" << endl;
	prit(a, 8);
	cout << "递归快速排序后:" << endl;
	QuickSort1(a, 0, 7);
	prit(a, 8);
	system("pause");
	return 0;
}
  • Necessary to use a non-recursive stack, similar ideas with recursion, direct attach codes
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <assert.h>
using namespace std;
int partition(int* a, int left, int right)
{
	double x = a[right];
	int i = left - 1, j = right;
	for (;;)
	{
		while (a[++i] < x) {}
		while (a[--j] > x) { if (j == left) break; }
		if (i < j)
			swap(a[i], a[j]);
		else break;
	}
	swap(a[i], a[right]);
	return i;
}
void Quick_Sort2(int  *arr, int len)
{
	//1.申请一块内存当栈
	//2.进行一次快速排序,找到基准
	//3.把左边 右边的数对进行入栈
	//4.取出数据进行一趟快速排序
	//5.top>0
	int *stack = (int *)malloc(sizeof(int) * len);//定义栈的大小
	assert(stack != NULL);
	int top = 0;
	int low = 0;
	int high = len - 1;
	int par = partition(arr, low, high);
	if (par > low + 1)//左边有两个数据以上数时  入栈
	{
		stack[top++] = low;
		stack[top++] = par - 1;
	}
	if (par < high - 1)//右边左边有两个数据以上数时  入栈
	{
		stack[top++] = par + 1;
		stack[top++] = high;
	}
	while (top > 0)//出栈  栈不空,需要取两个数据出来排序
	{
		high = stack[--top];
		low = stack[--top];
		par = partition(arr, low, high);
		if (par > low + 1)
		{
			stack[top++] = low;
			stack[top++] = par - 1;
		}
		if (par < high - 1)
		{
			stack[top++] = par + 1;
			stack[top++] = high;
		}
	}
	free(stack);
	stack = NULL;
}

void prit(int *a,int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	int a[9] = { 49, 38, 65, 97, 76, 13, 27, 49 };
	cout << "原数组是:" << endl;
	prit(a, 8);
	cout << "非递归快速排序后:" << endl;
	Quick_Sort2(a, 8);
	prit(a, 8);
	system("pause");
	return 0;
}
Published 33 original articles · won praise 13 · views 1059

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/104357358