Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Selection sort in C++

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();
}

No comments:

Post a Comment

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)