Fortune left base class2-- method example 1 Dutch flag issue

Preparatory Title: Given an array ARR, and a number num, num less than or equal to the requested number of the array on the left, on the right of the number is greater than num array. It requires additional space complexity O (1), the time complexity of O (N)

analysis

1. The array is divided into num less and a region of greater than num. The variable x indicates a position less than region. Since the beginning of the region less does not exist x = -1, it represents a pointer to the array 1 (does not exist). Then set the variable cur traverse position as the beginning cur = 0point of the first array number.
2. start traversing
(1) if cur <= num, then the current element cur, and the less the number of the next area exchange, i.e. the number of switching positions and cur ++ x position, cur, X backward;
(2 ) If the cur> num, cur shift;
(3) if cur == arr_length traverse ends.

EG [4,6,7,3], NUM = 5
1.cur point 4,4 <5, i.e., the next bit position 0 and 4 x 4 (and their own) exchange. After the point x 4, cur ++, cur point. 6;
2.cur point 6,5 <6, x unchanged, CUR ++;
3.cur point 7,5 <7, x unchanged, CUR ++;
4.cur 3,3 points <5, the exchange, the exchange 3 and 6 i.e. an x. After pointing x 3, cur ++, exit the loop.
In this case, x 3 and the point 4 prior to or less area, and after the x 7,6 is larger than the area.

Figure ????? unable to go. . .

Core code

while(cur < arr_length)
	{
		if(arr[cur] <= num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else
		{
			cur++;
		}
	}

The complete code

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

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

int main()
{
	int arr[arr_length];
	int cur = 0;
	int num = 6;
	int x = -1;

	//生成随机长度为arr_length的数组,并输出
	srand((unsigned)time(NULL));
	cout<<"arr = ";
	for(int i = 0;i < arr_length;i++)
	{
		arr[i] = rand()%10;
		cout<<arr[i]<<" ";
	}
	cout<<endl;

	//核心代码
	while(cur < arr_length)
	{
		if(arr[cur] <= num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else
		{
			cur++;
		}
	}
	
	//输出交换后的arr1,arr2
	cout<<"arr1 = ";
	for(int i = 0;i<=x;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	cout<<"arr2 = ";
	for(int i = x+1;i<arr_length;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}

Dutch flag problem: Given an array ARR, and a number num, num Please count is smaller than on the left of the array, is equal to the number in the middle num array, num greater than the number on the right side of the array. It requires additional space complexity O (1), the time complexity of O (N)

analysis

Similar problems and preliminary
1. num array divided into regions is less than, equal num and a region of greater than num. The variable x indicates a position less than region, and y represents an area larger than the num. Since the beginning of less than and greater than the area does not exist x = -1,y = length, it represents a pointer to the array -1, length (does not exist). Then set the variable cur traverse position as the beginning cur = 0point of the first array number.
2. start traversing
(1) if cur <num, then the current element cur, and the next number is smaller than the area of the exchange, i.e. the number of switching positions and cur ++ x position, cur, X backward;
(2) if cur> num, then the current element cur, and larger than the area of a number of pre -exchange, i.e. the number of switching positions and cur position -y, y forward, cur unchanged (because the number of uncertainty over the exchange)
(3) If cur == num, cur ++.
(4) If the cur == y end.

Core code

while(cur < y)
	{
		if(arr[cur] < num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else if(arr[cur] == num)
		{
			cur++;
		}
		else
		{
			swap(arr[cur],arr[--y]);
		}
	}

The complete code

#include<iostream>
#include<time.h>
#define arr_length 5
using namespace std;

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

int main()
{
	int arr[arr_length];
	int cur = 0;
	int num = 6;
	int x = -1;
	int y = arr_length;

	//生成随机长度为arr_length的数组,并输出
	srand((unsigned)time(NULL));
	cout<<"arr = ";
	for(int i = 0;i < arr_length;i++)
	{
		arr[i] = rand()%10;
		cout<<arr[i]<<" ";
	}
	cout<<endl;


	//核心代码
	while(cur < y)
	{
		if(arr[cur] < num)
		{
			swap(arr[cur++],arr[++x]);
		}
		else if(arr[cur] == num)
		{
			cur++;
		}
		else
		{
			swap(arr[cur],arr[--y]);
		}
	}


	//输出交换后的arr1,arr2,arr3
	cout<<"arr1 = ";
	for(int i = 0;i<=x;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	cout<<"arr2 = ";
	for(int i = x+1;i<y;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	cout<<"arr3 = ";
	for(int i = y;i<arr_length;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;


	system("pause");
	return 0;
}
Published 51 original articles · won praise 1 · views 1396

Guess you like

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