Amazon Affiliate

Thursday 31 May 2012

PROGRAM FOR SELECTION SORT....

// SELECTION SORT


# include<iostream.h>
#include <stdio.h>
#include <stdlib.h>

   class selection
      {
       private:    int temp, current, j;

       public:
        void selection_sort(int *, int);
        void display(int *, int );

    };


void selection :: selection_sort(int array[], int size)
   {
   for (current = 0; current < size; current++)
    for (j = current + 1; j < size; j++)
      if (array[current] > array[j])
        {
          temp = array[current];
          array[current] = array[j];
          array[j] = temp;
        }
  }

   void selection :: display( int list[], int n)
       {
      cout<<"\n Sorted list is as follows :\n";
      for(int i = 0 ; i < n ; i++)
         {
           cout<<"  "<<list[i];
         }
    }

void main(void)
 {
   selection sort;

   int list[30];
   int number;

   cout<<"\n Input the number of the elements:";
   cin>>number;
   for (int i = 0; i < number; i++)
      {
    cout<<"\n Input the values for | "<<i+1<<" | ";
    cin>>list[i];
      }
       sort.selection_sort(list, number);
       sort.display(list, number);
    }

No comments:

Post a Comment