Divide and Conquer

/*
	Alrogithms: Divide and Conquer
	1) Binary Search
	2) QuickSort
	3) MergeSort
	4) Maximum and minimum of an array using minimum number of comparisons
*/
#include <iostream>
using namespace std;

struct Pairs
{
	int min;
	int max;
};

int BinarySearch(int arr[], int l, int r, int val);
void QuickSort(int arr[], int l, int r);
int partition(int arr[], int l, int r);
void swap(int &a, int &b);
void MergeSort(int arr[], int l, int r);
void Merge(int arr[], int l, int m, int r); 
struct Pairs getMinMax(int arr[], int l, int r);

int main()
{
	// Binary Search test
	cout<<"BinarySearch test"<<endl;
	int arr1[10] = {1, 3, 5, 7, 9, 10, 22, 33, 55, 100};
	int index = BinarySearch(arr1, 0, 9, 55);
	cout<<arr1[index]<<endl;

	// Quick Sort test
	cout<<"QuickSort test"<<endl;
	int arr2[10] = {1, 3, 5, 7, 9, 10, 8, 6, 4, 2};
	QuickSort(arr2, 0, 9);
	for (int i=0; i<10; i++)
	{
		cout<<arr2[i]<<"  ";
	}
	cout<<endl;

	// MergeSort test
	cout<<"MergeSort test"<<endl;
	int arr3[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
	MergeSort(arr3, 0, 9);
	for (int i=0; i<10; i++)
	{
		cout<<arr3[i]<<"  ";
	}
	cout<<endl;

	// Maximum and minimum of an array using minimum number of comparisons
	cout<<"Maximum and minimum of an array using minimum number of comparisons"<<endl;
	int arr4[10] = {1, 3, 5, 7, 9, 10, 8, 6, 4, 2};
	struct Pairs minmax = getMinMax(arr4, 0, 9);
	cout<<"max:"<<minmax.max<<" min:"<<minmax.min<<endl;

	system("pause");
	return 0;
}

int BinarySearch(int arr[], int l, int r, int val)
{
	if (l > r)
	{
		return -1;
	}

	int m = (l + r) / 2;

	if (arr[m] == val)
	{
		return m;
	}

	if (l == r)
	{
		return -1;
	}

	if (arr[m] > val)
	{
		return BinarySearch(arr, l, m-1, val);
	}
	else
	{
		return BinarySearch(arr, m+1, r, val);
	}
}

void QuickSort(int arr[], int l, int r)
{
	if (l >= r)
	{
		return ;
	}

	int i = partition(arr, l, r);
	QuickSort(arr, l, i-1);
	QuickSort(arr, i+1, r);
}

int partition(int arr[], int l, int r)
{
	int t = arr[r];
	int i = l- 1;
	int j = r;

	for ( ; ; )
	{
		while (arr[++i] < t);

		while (arr[--j] > t)
		{
			if (j == l)
			{
				break;
			}
		}

		if (i >= j)
		{
			break;
		}

		swap(arr[i], arr[j]);
	}

	swap(arr[i], arr[r]);

	return i;	
}

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

struct Pairs getMinMax(int arr[], int l, int r)
{
	struct Pairs minmax;

	if (l == r)
	{
		minmax.max = arr[l];
		minmax.min = arr[l];

		return minmax;
	}

	if (r == l+1)
	{
		if (arr[l] > arr[r])
		{
			minmax.max = arr[l];
			minmax.min = arr[r];
		}
		else
		{
			minmax.max = arr[r];
			minmax.min = arr[l];
		}

		return minmax;
	}

	struct Pairs mml, mmr;
	int mid = (l + r) / 2;

	mml = getMinMax(arr, l, mid);
	mmr = getMinMax(arr, mid+1, r);

	if (mml.max > mmr.max)
	{
		minmax.max = mml.max;
	}
	else
	{
		minmax.max = mmr.max;
	}

	if (mml.min < mmr.min)
	{
		minmax.min = mml.min;
	}
	else
	{
		minmax.min = mmr.min;
	}

	return minmax;
}

// Top-down mergesort
void MergeSort(int arr[], int l, int r)
{
	if (l >= r)
	{
		return ;
	}

	int m = (l + r) / 2;

	MergeSort(arr, l, m);
	MergeSort(arr, m+1, r);
	Merge(arr, l, m, r);
}

void Merge(int arr[], int l, int m, int r)
{
	int i, j, k;
	int len = r - l + 1;
	int *aux = new int[len];

	for (k=0, i=l; i<=m; i++, k++)
	{
		aux[k] = arr[i];
	}

	for (k=len-1, j=m+1; j<=r; j++, k--)
	{
		aux[k] = arr[j];
	}

	i = 0;
	j = len-1;

	for (k=l; k<=r; k++)
	{
		if (aux[j] < aux[i])
		{
			arr[k] = aux[j--];
		}
		else
		{
			arr[k] = aux[i++];
		}
	}
}

你可能感兴趣的:(Divide and Conquer)