Examples of divide and conquer method

Find the largest and the second largest Yuan Yuan

Algorithm Description:
splitting the problem into sub-problems, a comparison or two elements, when one element, the second largest maximum value per se, when two elements, a larger maximum value, the second largest value is small. If the minimum is not the case, then the problem split into small problems. Finally, the front pass back the maximum value and the later period of time the second largest maximum value, comparing the number of four, if the latter is larger than the maximum value of the maximum value and the maximum value MAX1, then compare the value of the second largest, the second largest value to a large . This key question is how to compare the two and the second largest maximum value.
Source:

#include <iostream>
using namespace std;
int a[100];

/*求最大值和次大值*/
void Max12(int i,int j,int &max,int &mimax){
	int max1,mimax1;
	if(i==j){//一个元素时,最大值次大值是本身
		max=mimax=a[i];
	}
	else if(i==j-1){//两个元素时,最大值是较大的一个,次大值是小的
		if(a[i]<a[j]){
			max=a[j];mimax=a[i];
		}
		else{
			max=a[i];mimax=a[j];
		}
	}

	else{//其他情况时调用Max12,将问题分解
		int m=(i+j)/2;
		Max12(i,m,max,mimax);
		Max12(m+1,j,max1,mimax1);

		if(max1>max){//如果后面最大值比最大值大,改变最大值次大值
			mimax=max;
			max=max1;
			if(mimax1>mimax)
				mimax=mimax1;
		}
		else if(max1>mimax){//后面最大值大于次大值,改变次大值
			mimax=max1;
		}
	}
}

int main(){
	int n,i;
	int max,mimax;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];

	max=a[0];//给最大值次大值初始化
	mimax=a[0];

	Max12(0,n-1,max,mimax);
	cout<<max<<" "<<mimax<<endl;
	return 0;
}

Quick Sort

Algorithm Description:
with the quick sort sequence given in descending order, before writing a quick sort to write the partition function. Functions can be divided into the larger value than the first one on the left, a small place on the right. Here set up a surveillance post, the end of the sequence assignment is infinitesimal, to prevent cross-border i. i go backwards until you find a value smaller than the first stop, j stop moving forward until you find a value larger than the first, if i, j is not staggered, two elements of the exchange until, i, j staggered. Finally, the value of j and a value exchange, the first value to line up the order.

Source:

#include <iostream>
using namespace std;
int a[100];

/*从大到小划分函数*/
int Pa(int left,int right){
	int i=left;
	int j=right+1;
	int temp=a[j];//暂时保留right后面的元素,并把他的值变为无穷大
	a[j]=0x80000000;
	do{
		do
			i++;
		while(a[i]>a[left]);//i找比第一个值小的

		do
			j--;
		while(a[j]<a[left]);//j找比第一个值大的

		if(i<j)
			swap(a[i],a[j]);//把大的和小的交换位置
	}while(i<j);

	swap(a[left],a[j]);
	a[right+1]=temp;
	return j;
}

/*快速排序*/
void QuickSort(int left,int right){
	if(left<right){
		int j=Pa(left,right);
		QuickSort(left,j-1);
		QuickSort(j+1,right);
	}
}

int main(){
	int i,n;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a[i];
	}
	a[n]=0x80000000;//最后为无穷大,当做监视哨
	QuickSort(0,n-1);
	for(i=0;i<n;i++){
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
}

Find a small element of the k

Algorithm Description:
find the k small elements, using the patition partition function, did not find the time to find the first j + 1 small, take advantage of this division has been calling function until the first of the small K, note that there is a comparison and j k calls to reduce the size of the order, similar to binary search, when large j, j find the left of the hour, looking for the right, until you find.

Source:

#include <iostream>
using namespace std;
int a[100];

/*从小到大划分函数*/
int Pa(int left,int right){
	int i=left;
	int j=right+1;
	int temp=a[j];
	a[j]=0x7fffffff;
	do{
		do
			i++;
		while(a[i]<a[left]);

		do
			j--;
		while(a[j]>a[left]);

		if(i<j)
			swap(a[i],a[j]);
	}while(i<j);
	swap(a[left],a[j]);
	a[right+1]=temp;
	return j;
}

/*这是找到第k小的元素位置*/
int find(int left,int right,int k){
	int j;
	j=Pa(left,right);
	do
	{
		if(j>k)//如果j是大的,K小肯定在左边
			j=Pa(left,j-1);
		else if(j<k)//j大,k小在右边
			j=Pa(j+1,right);
	}while(j!=k);//循环直到找到第k小
	return j;
	
}

int main(){
	int n,i,j;
	int x;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a[i];
	}
	cin>>x;
	j=find(0,n-1,x-1);//x-1是为了适应数组下标
	cout<<a[x-1]<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/jihome/article/details/94966165