Exchange sort and its application

Bubble sort
Put the smallest element to the left every time
Time complexity o(n^2), space complexity o(1), the best time complexity o(n)
void bubblesort(int a[], int n)
{
	for (int i = 0; i <= n - 1; i++)
	{
		bool flag = 0;
		for(int j=n-1;j>i;j--)//Move from the back of the array move the smallest element to the front
			if (a[j] < a[j-1])
			{
				int temp = a[j];
				a[j] = a[j-1];
				a[j-1] = temp;
				flag = 1;
			}
		if (flag == 0) return;//Description is in order
	}
}
Two way bubbling sequence
Determine the minimum value from back to left and the maximum value from left to right
Time complexity o(n^2), space complexity o(1), the best time complexity o(n)
void bubblesort1(int a[], int n)
{
	int low = 0, high = n - 1;
	bool flag = 1;
	while (low < high&&flag == 1)
	{
		flag = 0;
		for(int i=low;i!=high;i++)//Determine the maximum value
			if (a[i] > a[i + 1])
			{
				int temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
				flag = 1;
			}
		high--;
		for (int i =high; i != low; i--)//Determine the minimum value
			if (a[i] < a[i - 1])
			{
				int temp = a[i];
				a[i] = a[i - 1];
				a[i - 1] = temp;
				flag = 1;
			}
		low++;
	}

}

The worst spatial complexity o(n), the average spatial complexity o(logn)
Worst time complexity o(n^2), mean time complexity o(nlogn)

Partitioning algorithm
Each time, take the first element of the current table as the pivot value, move the table that is larger than the pivot value backward and the table that is smaller forward.
int part(int a[], int low, int high)
{
	int pivot = a[low];//Pivot value
	while (low < high)//The loop exit condition ensures that the pivot value is found
	{
		while (low<high&&a[high]>=pivot)high--;
		a[low] = a[high];//Small left shift
		while (low < high&&a[low] <= pivot)low++;
		a[high] = a[low];//Great right shift
	}
	a[low] = pivot;
	return low;

}
Recursive call partition algorithm
void quicksort(int a[], int low, int high)
{
	if (low < high)
	{
		int pivot = part1(a, low, high);//Partitioning algorithm
		quicksort(a, low, pivot - 1);
		quicksort(a, pivot + 1, high);
	}
}
Quick sort non recursive
Stack storage
void quicksort1(int a[], int low, int high)
{
	stack<int>temp;
	int i, j;
	temp.push(high);
	temp.push(low);
	while (!temp.empty())
	{
		i = temp.top();
		temp.pop();
		j = temp.top();
		temp.pop();
		if (i < j)
		{
			int k = part(a, i, j);
			if (k < j)
			{
				temp.push(j);
				temp.push(k + 1);
			}
			if (k > i)
			{
				temp.push(k - 1);
				temp.push(i);
			}
		}
	}
}
Take random number as pivot value
int part1(int a[], int low, int high)
{
	int rand_index = low + rand() % (high - low + 1);
	swap(a[rand_index], a[low]);
	int pivot = a[low];
	while (low < high)
	{
		while (low<high&&a[high]>pivot)
		high--;
		a[low]=a[high]
		while (low < high&&a[low] < pivot)low++;
		a[high]=a[low];
	}
	a[low] = pivot;
	return low;
}
Move odd to front, even to back
Using the idea of fast arrangement, find an even number from the front to the back, then an odd number from the back to the front, and then exchange
void move(int a[], int n)
{
	int low = 0, high = n - 1;
	while (low < high)
	{
		while (low < high&&a[low] % 2 == 1)low++;//Low < high is necessary,
		//To prevent the following operations from crossing the boundary once they meet the requirements
		while (low < high&&a[high] % 2 == 0)high--;
		if (low < high)
		{
			int temp = a[low];
			a[low] = a[high];
			a[high] = temp;
		}
		low++;
		high--;
	}
}
Find the smallest number in the array
int kth(int a[], int low, int high, int k)
{
	int pivot = a[low];//Pivot value
	int low_temp = low;
	int high_temp = high;//Intermediate variable
	while (low< high)
	{
		while (low<high&&a[high]>=pivot)high--;
		while (low< high&&a[low] <=pivot)low++;
		if (low < high)
		{
			int t = a[low];
			a[low] = a[high];
			a[high] = t;
		}
	}//Find the location of change value
	a[low] = pivot;
	k--;
	if (low==k)return a[low];
	else if (low > k )
		return kth(a, low_temp, low - 1, k);
	else
		return kth(a, low+ 1, high_temp, k - low);
}
Divide the array into two groups to maximize n1-n2 and the difference between the sum of the arrays

i

nt setparttion(int a[], int n)
{
	int pivot1, low = 0, high = n - 1, low0 = 0, high0 = n - 1, k = n / 2;
	int s1=0, s2=0;
	bool flag = 1;//Judge whether the boundary point is found
	while (flag)
	{
		pivot1 = a[low];
		while (low < high)
		{
			while (low<high&&a[high]>pivot1)--high;
			while (low < high&&a[low] < pivot1)++low;
			if(low<high)swap(a[low], a[high]);
		}//Pivot
		a[low] = pivot1;
		if (low = k - 1)
			flag = 0;//Exit when found
		else
		{
			if (low < k - 1)
			{
				low++;//At this time, low is the subscript of pivot value, which is the lowest value
				low0 = low;
				high = high0;//Upper limit
			}
			else
			{
				low = low0;//low recount
				high--;
				high0 = high;//New upper limit
			}
		}
	}
	cout << a[k] << endl;
	for (int i = 0; i < k; i++)
		s1 += a[i];
	for (int i = k; i < n; i++)
		s2 += a[i];
	return s2 - s1;
}
Dutch flag problem, 1,2,3 are red, white and blue respectively
With three pointers, the elements before i are all red, j is the scanning pointer, and the elements after K are all blue
void flag(int a[], int n)
{
	int i = 0, j = 0, k = n - 1;
	while (j <= k)
	{
		switch (a[j])
		{
		case 1://When the scan encounters red, it moves to the front
			swap(a[i], a[j]);
			i++;
			j++;
			break;
		case 2://White in the middle
			j++;
			break;
		case 3://Blue back
			swap(a[j], a[k]);
			k--;//j does not decrease, to prevent the blue color after exchange
		}
	}
}

Posted by luke101 on Fri, 15 Nov 2019 13:14:01 -0800