Saturday, March 29, 2014

C++ program to sort the numbers with selection sort method

This is done by code::blocks Ide, You can run it in Turbo C++ also, but with changing header into iostream.h and removing using namespace std;

#include<iostream>
#include<conio.h>
using namespace std;
class bubble
{
    int num[50],n,i,j,temp;
public:
    void input()
    {
        cout<<"How many values you want to enter\n";
cin>>n;
cout<<"Enter values"<<endl;
for (i=0;i<n;i++)
  {
cin>>num[i];
  }
    }
    void display()
    {
for(i=0;i<n;i++)
cout<<num[i]<<"\n";
    }
    void calculation()
    {
        for (i=0;i<n;i++)
{
for(j=i;j<n;j++)
  {
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
     }

    }
    }
};
int main()
{
    bubble b;
    b.input();
    cout<<"Unlisted values are as follows:\n";
    b.display();
    b.calculation();
    cout<<"Listed values are as follows:\n";
    b.display();
getch();
return 0;
    }

No comments:

Post a Comment