C++ Program to Implement Bubble Sort using Templates

In this code, we will discuss program to implement bubble sort using templates in C++.

The code is self explanatory. The template function sorts the array in ascending order. The advantage of using template is we don’t have to write different function for different datatypes.

Program

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

// template function to perform bubble sort on array, arr
// n: size of arr
template<typename T>
void BubbleSort(T arr[], int n)
{
	for(int i=0;i<n-1;++i){
		for(int j=0;j<n-i-1;++j){
			if(arr[j]>arr[j+1]){
				T temp = arr[j+1];
				arr[j+1] = arr[j];
				arr[j] = temp;
			}
		}
	}
}

// Template function to print array
// n: size of arr[]
template<typename T>
void PrintArray(T arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << "\n\n";
}

int main()
{

    int arr[] = { 1, 10, 90, 100, -1, 11, 9, 14, 3, 2, 20, 19 };
    int n = sizeof(arr) / sizeof(int);

    cout << "Array Before Sorting: " << endl;
    PrintArray(arr, n);

    BubbleSort(arr, n);

    cout << "Array After Sorting: " << endl;
    PrintArray(arr, n);

}

Output

c++ bubble sort using template

Read
Square Root Using Binary Search
Print Diamond Pattern (recursive implementation)
Reverse Stack using Queue
Program to Reverse Array

Leave Comment if you face any problem

Leave a Comment

Your email address will not be published. Required fields are marked *