Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort Array using BubbleSort #99

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Sorting/Bubble_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include<iostream>
using namespace std;
template <class X> //defining template X for data types in Swapping function
void swapping(X &number1,X &number2)
{
X temp;
temp=number1;
number1=number2;
number2=temp;
}

template<class T> ////defining template Y for data types in BubbleSort function
void BubbleSort(T numbers[],int elements) //defining BubbleSort function
{
for(int count1=0; count1<=elements-1; count1++) //running loop1
{
for(int count2=elements-1; count1<count2; count2--) //running loop2
{
if(numbers[count2]<numbers[count2-1])
{
swapping(numbers[count2],numbers[count2-1]); //calling "swapping" function to swap two consecutive values according to the condition
}
}
}
}




int main()
{
int array1[5]= {10,40,50,20,30}; //creating first array with data type as int i.e X=int
float array2[5]= {5.5,2.2,1.1,4.4,3.3}; //creating second array with data type as float i.e Y=float
int counter; //display input array
cout<<"Arrays before Soring"<<endl;
cout<<"Array 1:"<<endl;
for(counter=0; counter<5; counter++)
{
cout<<array1[counter]<<" "<<endl;
}
cout<<"Array 2:"<<endl;
for(counter=0; counter<5; counter++)
{
cout<<array2[counter]<<" "<<endl;
}

BubbleSort(array1,5);
BubbleSort(array2,5);

cout<<"Sorted arrays are"<<endl; //displaying Arrays after Bubble sorting
cout<<"Array 1:"<<endl;
for(counter=0; counter<5; counter++)
{
cout<<array1[counter]<<" "<<endl;
}
cout<<"Array 2:"<<endl;
for(counter=0; counter<5; counter++)
{
cout<<array2[counter]<<" "<<endl;
}
return 0;
}