Monday, May 5, 2008

program to sort an array in c++ using selection sort

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

PROGRAM:

#include <iostream.h>

#include <conio.h>

const int N=10;


void main() {

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


clrscr();

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

first=i;

for(j=i+1;j<N;j++){

if(a[first]>a[j]) {

first=j;

}

}

temp=a[i];

a[i]=a[first];

a[first]=temp;

}

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

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

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

}


getch();

}

No comments: