C++ or C Program for Bubble Sort

File Name:- bsort.cpp

#include<iostream.h>
#include<conio.h>
#define N 5
void bsort(){
int list[N],i,j,temp;
cout<<"Enter elements of List"<<endl;
for(i=0;i<N;i++){
cout<<"Enter "<<i+1<<" element= ";
cin>>list[i];
}
cout<<"List before Sorting:-"<<endl;
for(i=0;i<N;i++){
cout<<list[i]<<" ";
}
for(i=0;i<N;i++){
for(j=0;j<N-1-i;j++){
if(list[j]>list[j+1]){
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}}}
cout<<"List After Sorting:-"<<endl;
for(i=0;i<N;i++){
cout<<list[i]<<" ";
}
}
}
void main(){
clrscr();
cout<<"Bubble Sort"<<endl;
bsort();
getch();
}

File Name:- bsort.c

#include<stdio.h>
#include<conio.h>
#define N 5
void bsort(){
int list[N],i,j,temp;
printf("Enter elements of List\n");
for(i=0;i<N;i++){
printf("Enter %d element= ",i+1);
scanf("%d",&list[i]);
}
printf("List before Sorting-\n");
for(i=0;i<N;i++){
printf(" %d ",list[i]);
}
for(i=0;i<N;i++){
for(j=0;j<N-1-i;j++){
if(list[j]>list[j+1]){
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}}}
printf("\nList after Sorting-\n");
for(i=0;i<N;i++){
printf(" %d ",list[i]);
}
}
void main(){
clrscr();
printf("Bubble Sort\n");
bsort();
getch();
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...