SELECTION SORT



AIM:
            To write a program to perform selection sort
ALGORITHM:
            STEP 1:  Start the program
            STEP 2:  Define a class select and declare data members
            STEP 3:  In member function set elements get the elements and store them in the array
            STEP 4:  In member function sort assume first element as minimum element.Find the  
                            Minimum element in the array and swap it.
            STEP 5:  Repeat the step 4 for remaining element
            STEP 6:  In member function print elements print the select element
            STEP 7 : Stop the program.



#include<iostream.h>
 main()
{
        int a[100],i,n,item;
        int temp=0,j,k,min1,min=0;
        char ch;
        cout<<"\n--------Selection Sort--------\n";
        cout<<"\n Enter the number of elements:";
        cin>>n;
        cout<<"\n*******************************";
        cout<<"\n\n Enter the elements:";
        for(i=0;i<n;i++)
        {
                cin>>a[i];
        }
        for(i=0;i<n;i++)
        {
                min=i;
                for(j=i+1;j<n;j++)
                {
                  if(a[min]>a[j])
                  {
                    min=j;
                  }
                }
                cout<<"\n\nThe sorted array is:";
                for(k=0;k<n;k++)
                cout<<" "<<a[k];
                temp=a[i];
                a[i]=a[min];
          
     a[min]=temp;
        }
        cout<<"\n\n******************************\n";
        cout<<"\n\nThe resulting array is:";
        for(i=0;i<n;i++)
        cout<<" "<<a[i];
      }
Output:
SELETION SORT
 Enter the number of elements:5
 Enter the elements:
1
9
6
89
987
 SORTED  ITERATION is: 1 9 6 89 987
 SORTED  ITERATION is: 1 9 6 89 987
 SORTED  ITERATION is: 1 6 9 89 987
 SORTED  ITERATION is: 1 6 9 89 987
 SORTED  ITERATION is: 1 6 9 89 987

THE RESULTANT SORT IS: 1 6 9 89 987

0 comments: