class2-- example basis Fortune law left classic 2 fast row, row the Dutch flag improve fast, fast random row in C ++

1. The classic fast row

analysis

Dutch flag ideas and issues in the preliminary title the same.
1. First, the last element in the array as a num [l, r] in Comparative give two regions, each region and the region or less larger than the last number of the last number.
2. In the region above the first paragraph, the last value of a number num left intact, the number of the penultimate num renamed, then the comparison continues, the iteration continues.
3. In the region of said second section 1, a number of the last named num, then the comparison continues, the iteration continues.
4. Continue iteration of conditionsif(l<r)

Core code

1. sorting portion
returns a value x-1: less than or equal to return the penultimate position of the range positions, the last position is the original num, directly next penultimate position as the next num.

int partition(int arr[],int l,int r)
{
	int num = arr[r];	//把最后一个数设为num进行比较
	int x = l-1;		    //x为小于等于的区域,设为区域之前一个位置
	int cur = l;		    //cur是当前遍历的指针
	while(cur < r+1)	//遍历所有位置
	{
		if(arr[cur]<=num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else
			cur++;
	}

	return x-1;			}```
2.迭代部分
迭代的条件是在[l,r]范围内,如果越界表示不可继续分
```cpp
void quicksort(int arr[],int l,int r)
{
	if(l<r)
	{
		int a = partition(arr,l,r);
		quicksort(arr,l,a);
		quicksort(arr,a+1,r);
	}
}

The complete code

#include<iostream>
#include<time.h>
#define length 10
using namespace std;

//交换函数
void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

int partition(int arr[],int l,int r)
{
	int num = arr[r];	//把最后一个数设为num进行比较
	int x = l-1;		//x为小于等于的区域,设为区域之前一个位置
	int cur = l;		//cur是当前遍历的指针
	while(cur < r+1)	//遍历所有位置
	{
		if(arr[cur]<=num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else
			cur++;
	}

	return x-1;			
}



void quicksort(int arr[],int l,int r)
{
	if(l<r)
	{
		int a = partition(arr,l,r);
		quicksort(arr,l,a);
		quicksort(arr,a+1,r);
	}
}


int main()
{
	
	//准备随机数组
	int arr[length] ;
	srand((unsigned)time(NULL));
	cout<<"arr = ";
	for(int i = 0;i < length;i++)
	{
		arr[i] = rand()%10;
		cout<<arr[i]<<" ";
	}
	cout<<endl;

	//意外情况直接返回(本题用不到)
	if (arr == NULL || length < 2) 
	{
		return 0 ;
	}

	//核心
	quicksort(arr,0,length - 1);


	//结果输出
	cout<<"arr = ";
	for(int i = 0;i<length;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;

	system("pause");
	return 0 ;
}

2. Improved the Dutch flag classic fast row

analysis

Classic fast row disadvantage: Only to find a sort num, num If there are multiple identical values ​​also need to continue to divide and do more useful work. If you consider ordering method using the flag of the Netherlands, the same num time to find out, then the time constant time complexity can be reduced.

Core code

Dutch flag problem requires two hands and a recording range of less than num num range of greater than, consider defining an array of length 2 of the record, as the next step returns an array of pointers.

void quicksort(int arr[],int l,int r)
{
	int a[2] = {0};
	if(l<r)
	{
		int *p = paitition(arr,l,r,a);
		quicksort(arr,l,*p);
		quicksort(arr,*(p+1),r);
	}
}
int* paitition(int arr[],int l,int r,int a[])
{
	int x = l-1;
	int y = r+1;
	int cur = l;
	int num = arr[r];
	while(cur<y)
	{
		if(arr[cur]<num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else if(arr[cur]>num)
		{
			swap(arr[cur],arr[--y]);
		}
		else
		{
			cur++;
		}
		
	}
	a[0] = x;
	a[1] = y;
	return a;
}

The complete code

#include<iostream>
#include<time.h>
#define length 20
using namespace std;

void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

int* paitition(int arr[],int l,int r,int a[])
{
	int x = l-1;
	int y = r+1;
	int cur = l;
	int num = arr[r];
	while(cur<y)
	{
		if(arr[cur]<num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else if(arr[cur]>num)
		{
			swap(arr[cur],arr[--y]);
		}
		else
		{
			cur++;
		}
		
	}
	a[0] = x;
	a[1] = y;
	return a;
}

void quicksort(int arr[],int l,int r)
{
	int a[2] = {0};
	if(l<r)
	{
		int *p = paitition(arr,l,r,a);
		quicksort(arr,l,*p);
		quicksort(arr,*(p+1),r);
	}
}

int main()
{
	//生成随机数组
	int arr[length];
	srand((unsigned)time(NULL));
	cout<<"arr = ";
	for(int i = 0;i<length;i++)
	{
		arr[i] = rand()%10;
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	
	quicksort(arr,0,length - 1);
	
	//打印结果
	cout<<"arr1 = ";
	for(int i = 0;i<length;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;

	system("pause");
	return 0;
}

3. fast random row

analysis

Improved classic Dutch flag quick drain a disadvantage in that the number used as the last num, e.g. [1,2,3,4,5,6] is high in complexity, because the complexity of the conditions associated to the data.
Consider: random number selected in the array as a num, so you can bypass the status of the raw data, the complexity becomes a long-term expectations.

Core code

Num value on a random initialization

int num = arr[l+rand()%(r-l+1)];

The complete code

#include<iostream>
#include<time.h>
#define length 20

using namespace std;
void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

int* paitition(int arr[],int l,int r,int a[])
{
	int x = l-1;
	int y = r+1;
	int cur = l;
	int num = arr[l+rand()%(r-l+1)];
	while(cur<y)
	{
		if(arr[cur]<num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else if(arr[cur]>num)
		{
			swap(arr[cur],arr[--y]);
		}
		else
		{
			cur++;
		}
		
	}
	a[0] = x;
	a[1] = y;
	return a;
}

void quicksort(int arr[],int l,int r)
{
	int a[2] = {0};
	if(l<r)
	{
		int *p = paitition(arr,l,r,a);
		quicksort(arr,l,*p);
		quicksort(arr,*(p+1),r);
	}
}


int main()
{
	//生成随机数组
	int arr[length];
	srand((unsigned)time(NULL));
	cout<<"arr = ";
	for(int i = 0;i<length;i++)
	{
		arr[i] = rand()%100;
		cout<<arr[i]<<" ";
	}
	cout<<endl;


	quicksort(arr,0,length - 1);


	//打印结果
	cout<<"arr1 = ";
	for(int i = 0;i<length;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;

	system("pause");
	return 0;
}

result

Complexity Analysis

Time complexity of O (N logN), additional space complexity of O (logN)
1. For long-term option, the possibility of random a value intermediate the maximum, partition thought T (n) = 2T (N / 2) + O (n), the time complexity of O (N
logN).
2. Find a random, half of the number of times the number of breakpoints, the best-case O (logN), the worst is O (N), the probability of long-term O (logN).

Published 51 original articles · won praise 1 · views 1395

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103496136