Bubble sort

PHOTO EMBED

Wed Jul 20 2022 09:51:20 GMT+0000 (Coordinated Universal Time)

Saved by @sahmal #c++

#include<iostream>
using namespace std;
template<typename T>
void swap(T* xp, T* yp);
template<typename T>
void sort(T* array, int size);
template<typename T>
void print(T* array, int size);
int main() {
	int size;//for taking size of the array 
	cout << "enter size of the array " << endl;
	cin >> size;
	int* array = new int[size];
	//taking input
	cout << "Enter values of the array " << endl;
	for (int i = 0; i < size; i++)
		cin >> array[i];
	//calling sort function
	sort<int>(array, size);
	//calling print function
	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) {

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


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

content_copyCOPY

Bubble sort