Bubble Sorting in Data Structure

Bubble Sorting in data structure also known as exchange sort in which repeatedly list is sorted in which comparing two items at a time and swapping them if they wrong in order.

Advantage of Bubble Sorting

  • Easiest comparison sort to implement.
  • Simple Algorithm to describe to computer.

Disadvantage of Bubble Sort

  • Insufficient for large data set

Bubble Sort in C++ Using Function

The program specifies how to sort an array using bubble sort in c++ :

#include<iostream>
#include<stdlib.h>

using namespace std;

void bubble(int ar[10]){
int i,j,temp;
for(int i=0;i<9;i++){
for(j=0;j<9-i;j++){
if(ar[j]>ar[j+1]){
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}}}
cout<<"sorted array"<<endl;
for(i=0;i<10;i++){
cout<<ar[i]<<"\t";
}}

int main()
{
   int ar[10],i;
   cout<<"enter 10 elements in a array for sorting\n";
   for(i=0;i<10;i++){
   cin>>ar[i];
   }
   bubble(ar);
   return 0;
}

The output of the above program is as follows:

Bubble Sort

Bubble Sort in c

The program specifies how to sort an array using bubble sort in c:

#include<stdio.h>
#include<stdlib.h>

void bubble(int ar[10]){
int i,j,temp;
for(int i=0;i<9;i++){
for(j=0;j<9-i;j++){
if(ar[j]>ar[j+1]){
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}}}
printf("sorted array");
printf("\n");
for(i=0;i<10;i++){
printf("%d",ar[i]);
printf("\t");
}}

int main()
{
   int ar[10],i;
   printf("enter 10 elements in a array for sorting\n");
   for(i=0;i<10;i++){
   scanf("%d",&ar[i]);
   }
   bubble(ar);
   return 0;
}

The output of the above program is as follows:

Bubble Sort

Discover more from easytechnotes

Subscribe to get the latest posts sent to your email.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top