avatar
Untitled

Oaspete 347 18th Apr, 2019

                                           
                         #include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

template<class T>int c(T&a, T&b)
{
	return a > b;
}

template <class T> void swap(T* a, T* b)
{
	T t = *a;
	*a = *b;
	*b = t;
}

template<class T> int partition(vector<T>&arr, int low, int high, int (*Compare)(T& e1, T& e2))
{
	T pivot = arr[high]; 
	int i = (low - 1);

	for (int j = low; j <= high - 1; j++)
	{
		if (!Compare(arr[j],pivot))
		{
			i++;  
			swap(&arr[i], &arr[j]);
		}
	}
	swap(&arr[i + 1], &arr[high]);
	return (i + 1);
}

template <class T>void quickSort(vector<T>&arr, int low, int high, int (*Compare)(T& e1, T& e2))
{
	if (!Compare(low,high))
	{
		int pi = partition(arr, low, high,Compare);
		quickSort(arr, low, pi - 1,Compare);
		quickSort(arr, pi + 1, high , Compare);
	}
}


template<class T> int partitiont(vector<T>& arr, int low, int high, function<int(T& e1, T& e2)>Compare)
{
	T pivot = arr[high];
	int i = (low - 1);

	for (int j = low; j <= high - 1; j++)
	{
		if (!Compare(arr[j], pivot))
		{
			i++;
			swap(&arr[i], &arr[j]);
		}
	}
	swap(&arr[i + 1], &arr[high]);
	return (i + 1);
}

template <class T>void quickSortt(vector<T> & arr, int low, int high, function<int(T& e1, T& e2)>Compare)
{
	if (!Compare(low, high))
	{
		int pi = partitiont(arr, low, high, Compare);
		quickSortt(arr, low, pi - 1, Compare);
		quickSortt(arr, pi + 1, high, Compare);
	}
}

template <class T> void Sort(vector<T>& lista, int (*Compare)(T& e1, T& e2))
{
	quickSort(lista, 0, lista.size() - 1, Compare);
}

template <class T>void Sortt(vector<T>& lista, function<int(T& e1, T& e2)>Compare)
{
	quickSortt(lista, 0, lista.size() - 1, Compare);
}


int main()
{
	vector<int>S;
	vector<int>T;

	T.push_back(7);
	T.push_back(10101010);
	T.push_back(25);
	T.push_back(100);

	S.push_back(5);
	S.push_back(4);
	S.push_back(3);
	S.push_back(2);

	auto f = [](int& e1, int& e2) -> int {return e1 > e2;};
	function<int(int&, int&)>FF = [](int&e1, int&e2) -> int { return e1 > e2; };

	Sort<int>(S, f);
	Sortt<int>(T, FF);

	for (auto s : S)cout << s << " ";
	cout << endl;
	for (auto c : T) cout << c << " ";
}
                      
                                       
Pentru a distribui această postare, copiați această adresă URL și trimiteți-o prietenilor
RAW Paste Data