#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] << " ";
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter