Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
965 B
0
Indexable
Never
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;

template <class T>
void XuatMang(T a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
}

template <class T>
void sort(T a[], int n, bool (*Compare)(T a, T b))
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (Compare(a[i], a[j]))
			{
				T temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
}

template <class T>
bool increase(T a, T b)
{
	return a > b;
}

template <class T>
bool decrease(T a, T b)
{
	return a < b;
}

int main()
{
	int a[] = { 13,44,12,87,23 };
	//float a[] = { 1.6F,7.9F,8.6F,2.3F };
	//string a[] = { "hello","chao","bonjour" };

	int n = sizeof(a) / sizeof(a[0]);

	cout << "\nSap xep tang: " << endl;
	sort<int>(a, n, increase);
	XuatMang(a, n);

	cout << "\nSap xep giam: " << endl;
	sort<int>(a, n, decrease);
	XuatMang(a, n);

	return 0;
}