Selection sort

PHOTO EMBED

Wed Jul 20 2022 07:24:18 GMT+0000 (Coordinated Universal Time)

Saved by @sahmal #c++

#include<iostream>
using namespace std;
template<typename T>
void sort(T* array, int size);
template<typename T>
void swap(T* xp, T* yp);
template<typename T>
void print(T* array, int size);
int main() {
	int size;
	cout << "enter size of the array " << endl;
	cin >> size;
	int* array = new int[size];
	cout << "enter elements of the array " << endl;
	for (int i = 0; i < size; i++)
		cin >> array[i];
	sort<int>(array, size);
	print<int>(array, size);
	return 0;
}
template<typename T>
void swap(T* xp, T* yp) {
	T temp;
	temp = *xp;
	*xp = *yp;
	*yp = temp;
}
template<typename T>
void sort(T* array, int size) {
	int min_index;
	for (int i = 0; i < size-1; i++)

	{
		min_index = i;
		for (int j = i + 1; j < size; j++)
			if (array[j] < array[min_index])
				min_index = j;
		swap(&array[min_index], &array[i]);
				

	}

}
template<typename T>
void print(T* array, int size) {
	for (int i = 0; i < size; i++)
		cout << array[i] << "  ";

}
content_copyCOPY

Selection sort