Monday, September 24, 2018

Program of Bubble Sort in C++

//Bubble Sort Program
#include<iostream>
using namespace std;
#include<conio.h>

//Function of Bubble Sort
void BubbleSort(int arr[], int size)
{
int temp;

for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter the elements to be sorted : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

BubbleSort(arr, size);

cout<<"\n Sorted Array : ";
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}

getch();
return 0;
}


No comments:

Post a Comment