Monday, May 5, 2008

sorting an array in c++ using bubble sort

Write a program that will sort the given ten numbers in descending order using: Bubble sort



PROGRAM:

#include <iostream.h>

#include <conio.h>

const int N=10;

void main() {

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


clrscr();

cout<<"Enter ten values to be sorted:\n";

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

cout<<"Enter Value "<<i+1<<" : ";

cin>>a[i];


}


for(i=0;i<N-1;i++)

for(j=0;j<N-1;j++)

if(a[j]>a[j+1]) {

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;               

}


cout<<"\nThe sorted array is: \n";

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

cout<<"\t"<<a[i];

}


getch();

}

No comments: