wat is the concept beyond COLUMN SORT??.. can any body guide me...Y column sort is introduced??

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by Ancient Dragon

Do you mean how to sort a 2d array based on just one of its columns? For example if you have an array of ints that has 5 columns and 1,000 rows, how to sort it by the contents of column 2 (or some other number) ?

Do you mean how to sort a 2d array based on just one of its columns? For example if you have an array of ints that has 5 columns and 1,000 rows, how to sort it by the contents of column 2 (or some other number) ?

the name exactly is MULTIPLE COLUMN SORTING.. if u know the concept please tell me.. i ll try the implementation part...

Multiple column sorting sounds like multi-key sorting, where the column totals, are the keys. This is something YOU must verify. Only you know the instructor who gave you the assignment, and have the class notes, contacts, etc.

Sounds like each key is ranked by it's priority, same as always.

Sometimes the words are slightly unclear, so an example is always best to show a clear meaning. That is something YOU have to give US, and show us what you've done to complete this assignment.

The algorithm is almost identical to single column sorting. The only difference is in the swapping of values -- when a swap is needed then swap all columns in the rows.

An easier, and quicker, way to do it is to do indirect swapping, where you have a second single column int array that contains index numbers (0, 1, 2, 3, ...). When a swap is needed just swap the index array

Example

``````#include<stdio.h>

void show(int ay[5][5], int index[5])
{
// display the array
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%-4d",ay[index[i]][j]);
}
printf("\n");
}
printf("\n");

}

int main()
{
#define sortcol  3

int ay[5][5] = {{ 4,3,1,5,2 },{10,4,9,15,3},{8,4,7,2,1},{15,42,35,21,4},{9,20,14,17,2}};
int index[5] = {0,1,2,3,4};
int i,j;
show(ay,index);
for(i = 0; i < 4; i++)
{
for(j = i+1; j < 5; j++)
{
int a = ay[index[i]][sortcol];
int b = ay[index[j]][sortcol];
if( a > b )
{
int temp = index[i];
index[i] = index[j];
index[j] = temp;
}
}
}
show(ay,index);

}``````

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.