C language quick sort of

Specific code

#include<stdio.h>

//定位
int Patrition(int* R, int start, int end)
{
	int standard = R[start];
	
	int i = start;
	int j = end;
	
	//寻找恰当位置(下文会细讲这里)
	while(i!=j)
	{
		while(i<j&&R[j]>=standard)	j--;
		
		if(i<j&&R[j]<standard)
		{
			R[i] = R[j];
			i++;
		}
		
		while(i<j&&R[i]<=standard)	i++;
		
		if(i<j&&R[i]>standard)
		{
			R[j] = R[i];
			j--;
		}	
	}
	R[i] = standard;
	return i;	
}

//这里统一用物理位置来表示 
void QuickSort(int* R, int start, int end)
{	
	if(start<end)
	{
		int index = Patrition(R,start,end);
		QuickSort(R,start,index-1);	 
		QuickSort(R,index+1,end);
	}	
}

int main()
{
	int M[6] = {9,5,3,7,8,4};	
	QuickSort(M,0,5);
	for(int i=0;i<6;i++)
	{
		printf("%d  ", M[i]);	
	}
}

Process Analysis

Quick sort of essence, it means, in an array, the number one in the right place in order of size, the array is divided into two smaller arrays. Doing so until each number are in their correct position.

Quicksort is mainly composed of two parts:
1. the sorting operation (the QuickSort), by a recursive manner to obtain an ordered arrangement. Its inner workings is a first positioning element in the array, divided into two smaller, then two small array is sorted, and finally the entire array is ordered. (Further, if the array is divided into a case where only one element, directly back)

2. positioning operation (Patrition), one by one by way of comparison, this number will be placed in the correct position (for example, the fourth element A is smaller in the array, the element A will be placed in a position to No. 4). After such an operation, the left side of this number is less than its full right is greater than its whole. (But not necessarily the order)

For example, to sort the following array:
Here Insert Picture Description
When we call QuickSort function will turn Patrition (positioning) and two QuickSort operation on the divided small array occurs.

Patrition operation

And in conjunction with the code, I step by step analysis:

The first step, select the target (that is, elements of which you find the corresponding position to give, here I chose the first element)

int standard = R[start];

Here Insert Picture Description
The second step in preparation for successively traverse the array from both sides, so the pointer ready:

int i = start;
int j = end;

Here Insert Picture Description
The third step, step core, find the correct positions:
before i and j are met, individual selection element, if greater than the reference, then go into the right side, if less than the reference, then go into the left.

while(i!=j)
{…}

But note that the position of the element can not put the data of the original array is lost.
At first, a position of the first element 5 has been used as a reference, so the first position is now empty, you can store elements.
As required, need to start from the right, to less than the reference elements put over, and now j 3 pointers referring to qualify, so mobile.
Here Insert Picture Description
Now, j content is written, so now the position corresponding to the pointer j is writable, then from the right to find there is no larger than the reference?
Obviously, that is, 7, 7 move past it
Here Insert Picture Description
now should be looking for from the right, and thus, also in line with the conditions 0, i write the position of the pointer 7 to go.
Here Insert Picture Description
Now writable position to the right to go, so keep looking to the right is greater than the benchmark:
Here Insert Picture Description
now what should move the pointer j, 6 is larger than 5, no matter what. 4 to 5 hours, written in the past!
Here Insert Picture Description
Now move the pointer i, 2 can, too! 1 can also be had!

Refers to the time when i 4, i and j pointers pointer met, indicating that this is in fact a reference element 5 should be in the position, so the 5 write over.
Here Insert Picture Description
Positioning complete!

I would add code ah role of each step:

	while(i<j&&R[j]>=standard)	j--;	//右指针,如果大于基准条件,就一直向左走
		
	if(i<j&&R[j]<standard)			//如果小了,那么就把元素写过去
	{								//Ps.另外一边的指针的位置
		R[i] = R[j];				//永远是指的可写的
		i++;						//所以直接R[i] = R[j]
	}
		
	while(i<j&&R[i]<=standard)	i++;//左指针,如果大于基准条件,就一直向左走
		
	if(i<j&&R[i]>standard)			//如果大了,写过去
	{
		R[j] = R[i];
		j--;
	}	
	//继续循环直到i==j  (这一堆代码都在while里)	

The fourth step, into a position corresponding to the value (corresponding to a final FIG above)

R[i] = standard;

The fifth step, return this benchmark positions, representatives of both sides have been differentiated Well, then, respectively, on both sides of the array can be QuickSort

return i;

QuickSort

FIG undertake above:
Here Insert Picture Description
each of the longer sides of the array to the same operation.

Here Insert Picture Description
Sort success!

Performance Analysis

Here Insert Picture Description

Published 33 original articles · won praise 40 · views 3548

Guess you like

Origin blog.csdn.net/qq_43948583/article/details/104399073