C++ program for selection sort to sort numbers. This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
Source Code:-
#include <iostream.h>
#include <conio.h>
void main()
{ clrscr();
int array[100], n, c, d, position, swap;
cout<<"Program for Selection Sort by-Tarun Rawat\n";
cout<<"\nEnter number of elements : ";
cin>>n;
cout<<"Enter "<<n<<" integers\n";
for ( c = 0 ; c < n ; c++ )
cin>>array[c];
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{ position = c;
for ( d = c + 1 ; d < n ; d++ )
{ if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{ swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
cout<<"\nSorted list in Ascending order:\n";
for ( c = 0 ; c < n ; c++ )
cout<<array[c]<<"\t";
getch();
}
Welcome to " way2cplusplus.blogspot.in " Objective of this blog is to implement various Computer Science Engineering Lab problems into C++ programming language. These are basically most common Lab Exercise problems based on the curriculum of engineering colleges throughout the Nation. These lab exercises are also relevant to Data structure. Simply C++ programming zone...
No comments:
Post a Comment