C++ program for bubble sort.

#include<iostream.h>

#include<conio.h>

#define N 5

//bubble sort function

void bsort(){

int list[N],i,j,temp;

clrscr();

cout<<"Bubble Sort"<<endl<<"Enter elements of List"<<endl;

for(i=0;i<N;i++){

cout<<"Enter "<<endl<<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(){

bsort();

getch();

}


No comments:

Post a Comment