Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

Rank Sort

Posted by Vinod on September 22, 2006


/*********************************************************

-> This C++ program is to perform rank sort.

-> This program works in microsoft vc++ 6.0 environment.

-> The numbers are sorted in increasing order.

**********************************************************/

#include<iostream.h>

class sorting
{
private:
 int n,rank[50];
 double *array;
public:
 void input();
 void ranksort();
 void output();
};

void sorting::input()
{
 cout<<”****************************************************\n”
  <<”This program sorts numbers in increasing order”
  <<”\n\t\tusing rank sort technique\n”
  <<”****************************************************\n”;

 cout<<”Enter how many numbers you are going to enter for sorting ::”;
 cin>>n;
 array=new double[n+1];
 cout<<”Now enter your elements ::\n”;
 for(int i=1;i<=n;i++)
  cin>>array[i];
}

void sorting::ranksort()
{
  int i,j;
  double b[50];
        for(i=1;i<=n;i++)
                rank[i]=0;
        for(i=1;i<=n;i++)
                for(j=1;j<=i;j++)
                        if(array[j]>array[i])rank[j]++;
                        else rank[i]++;
        for(i=1;i<=n;i++)
                b[rank[i]]=array[i];
        for(i=1;i<=n;i++)
                array[i]=b[i];
}

void sorting::output()
{
 cout<<”Now the sorted numbers are ::\n”;
 for(int i=1;i<=n;i++)
  cout<<array[i]<<’\t’;
 cout<<endl;
}

int main()
{
 sorting obj;
 obj.input();
 obj.ranksort();
 obj.output();
 return 0;
}
/********************************************************************

SAMPLE OUTPUT ::

****************************************************
This program sorts numbers in increasing order
                using rank sort technique
****************************************************
Enter how many numbers you are going to enter for sorting ::7
Now enter your elements ::
1.7
1.6
1.5
1.4
1.3
1.2
1.1
Now the sorted numbers are ::
1.1     1.2     1.3     1.4     1.5     1.6     1.7
Press any key to continue
*********************************************************************/

One Response to “Rank Sort”

  1. [...] 10) Rank Sort [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 76 other followers