A recursion for sorting 2D Array by index

User/Community Contributed Code Snippets using the Framework

A recursion for sorting 2D Array by index

Postby prasad » Sun Mar 28, 2010 10:55 pm

This code is written for the following problem.
I got the output from CyberT R script which has around 30 odd columns and around 55000 probes.
Now I have a p-raw column. I have to sort all the rows and put them in ascending / descending order w.r.t to the p-raw value column.
This recurssion logic is what I implemented to handle this problem.


Code: Select all
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

void index(vector<int>vn, vector<int> &srt)
{

        srt.push_back(vn[max_element(vn.begin(), vn.end())-vn.begin()]);
        //cout<<max_element(vn.begin(), vn.end())-vn.begin()<<"\t";
        //cout<<*max_element(vn.begin(), vn.end())<<endl ;
        if (vn.size() > 1 ) {
        vn.erase(max_element(vn.begin(), vn.end()) );
        vector<int> vn2 = vn;
        index(vn2 , srt);
        }
}

int main()
{

vector<int> vals;
vector<int> valssorted;

vals.push_back(15);
vals.push_back(4);
vals.push_back(5);
vals.push_back(5);
vals.push_back(2);
vals.push_back(2);
vals.push_back(3);
vals.push_back(6);
vals.push_back(9);
vals.push_back(10);

cout<<vals.size()<<endl;


index(vals, valssorted);

for ( int i = 0; i < valssorted.size(); i++)
cout<<i<<'\t'<<valssorted[i]<<endl;

return 0;
}
prasad
 
Posts: 8
Joined: Sat Mar 20, 2010 4:51 am

Return to BioBhasha Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest

cron