My Blog List

Sunday, November 10, 2019

Bubble Sort using CPP.

In this tutorial we learn how to sort the data using Bubble Sort.

A Sorting technique is use to rearrange the data in the ordered manner.The data arrange by Ascending and Descending order.Bubble Sort is he simple Sorting Algorithm Technique it works by repeatedly swapping data.When the data are giving input in wrong order then the Sorting technique is used.

Following code shows the Bubble Sorting technique:-
In this example we take a dynamic array the size will dynamic as given by user.


#include<iostream.h>
#include<conio.h>
void bubblesort(int numbers[],int n)
{
int i,j,temp;
for(i=(n-1);i>=0;i--)
{
for(j=1;j<=i;j++)
{
if(numbers[j-1]>numbers[j])
{
temp=numbers[j-1];
numbers[j-1]=numbers[j];
numbers[j]=temp;
}
}
}
}
void main()
{
int *numbers,n;
clrscr();
cout<<"\nEnter the size of array:";
cin>>n;
numbers=new int[n];
cout<<"\nEnter the array element:";
for(int i=0;i<n;i++)
{
cin>>numbers[i];
}
bubblesort(numbers,n);
cout<<"\nSorted element:";
for(i=0;i<n;i++)
{
cout<<" "<<numbers[i];
}
getch();
}


The above code shows the following output:-




No comments:

Post a Comment

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...