Saturday, September 3, 2016

Selection Sort in C++

A program that I wrote a long time ago in my C++ programming class in college to perform selection sort. The code is very simple and easy to use.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
 
My mobile number here in the Philippines is 09173084360
.



Program Listing

#include <iostream>

using namespace std;

#define MAX 5

void SelSort(int X[],int start,int stop)
{
   int begin=start;
   int smallsofar=begin;
   int tmp;

   if(start<stop)
   {
        for(int i=begin+1;i<=stop;i++)
      {
           if(X[i] < X[smallsofar])
              smallsofar=i;
      }
      tmp=X[begin];
      X[begin]=X[smallsofar];
      X[smallsofar]=tmp;

      SelSort(X,start+1,stop);
   }
}

int main()
{
   int abc[MAX];
    cout << "\t\t\t      SELECTION SORT IN C++";
    cout << "\n\t\t Created By: Mr. Jake R. Pomperada, MAED-IT";
    cout << "\n\n";   
   for(int i=0;i<5;i++) {

    cout << "Enter Item No.:" << i+1 << ":";
    cin >> abc[i];
   }
   cout << "\n\t Original Arrangement of Numbers";
   cout << "\n\n";
   for(int i=0;i<5;i++)
        {

        cout << "\t " << abc[i] << " ";
           }

   SelSort(abc,0,MAX-1);
   cout << "\n";
   cout << "\n\t Sorted Arragmenent  of Number";
   cout << "\n\n";
   for(int i=0;i<5;i++)
        {
        cout << "\t " << abc[i] << " ";
           }
cout << "\n\n";
system("PAUSE");
}

No comments:

Post a Comment