Sort - Quick Sort

First, the algorithm thought

Take any one element (e.g., first take a) from the column as a pivot to be sorted, all smaller than all of its elements prior to the discharge, is greater than all of its elements placed after all, are formed around the two sub-tables; then for each reselect the central element child table and adjust the rule so until each element of only a sub-table. At this time it is the ordered sequence.


Second, the algorithm process

Here Insert Picture Description

The first trip to the quick sort, for example, explain the process:

(1) First, select 256 as a pivot, i = 0, j = length -1, respectively, point to the head, the tail key
Here Insert Picture Description
(2) start scanning from right to left of j, to find the number 076 is less than 256, so that R & lt [ i] = R [j]
Here Insert Picture Description

(3) A i start scanning from left to right, to find the number is greater than 301,256, so that R [j] = R [i]

Here Insert Picture Description

(4) i is from right to left to begin scanning, found less than the number 129256, so that R [i] = R [j]

Here Insert Picture Description

(5) start scanning from left to right of i, to find the number is greater than 751,256, so that R [j] = R [i]

Here Insert Picture Description

(6) i will start scanning from right to left, when i = j, it will fill vacancies at 256, to give a final answer

Here Insert Picture Description


Third, to achieve

int Partition(int R[],int low,int high)	
{
	int temp=R[0];
	while(low<high)
	{
		while(low<high && temp<=R[high])
		{
			high--;	//	直到找到比temp小的元素
		}
		R[low]=R[high];
		while(low<high && temp>=R[low])
		{
			low--;	//	直到找到比temp大的元素
		}
		R[high]=R[low];
	}
	R[low]=temp;
	return low;	//返回枢轴所在位置,以便进一步划分

}
void QSort(int R[],int low,int high)	//	若要对数组R进行快排,low可以设为0,high可以设为length-1
{
	int mid;
	if(low<high)
	{
		mid=Partition(R,low,high);
		QSort(R,mid+1,high);	//	在右子区间进行递归快排,直到长度为1
		QSort(R,low,mid0-1);	//	在左子区间进行递归快排,直到长度为1
	}
}

Fourth, the algorithm performance analysis

(1) Time Complexity Analysis

  • Consider the worst situation: that is, when the object to be sorted according to their key sequence has been sorted from small to large, the recursive tree branch into a single tree, each division only once to obtain at least one object on a sequence specific. In this way, it must be n 1 n-1 trip to locate all the objects, and the first trip i need to go through n i n-i times compare to find the key i i placement of objects, the total number of comparisons will reach key n 2 / 2 n^2/2 , the time complexity is O ( n 2 ) O (n ^ 2)

  • Consider the best case: If each division is positioned on an object, the object's left child the same sub-sequence length sequence right side, the next step will be to halve the length of the two sequences to sort, which It is the ideal situation. At this point, the least number of laps quick sort. Its time complexity is O ( n l o g n ) O (nlog n)

  • For the average case, its complexity of the algorithm is O ( n l o g n ) O (nlog n)

Complexity (2) Spatial Analysis

Quick sort is recursive, the need for pointers and parameters (new low and high) when a stack for each recursive call. Consistent depth, ideally with the maximum number of recursive call hierarchy is recursive tree l o g 2 n + 1 log_2(n+1) . Therefore, the requirements for the storage overhead O l o g 2 n O(log_2n)

(3) Stability

Since any of the optional elements as a fulcrum, so that the algorithm is unstable.

Guess you like

Origin blog.csdn.net/starter_____/article/details/93930702