Hi dears!
I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping this array. please help me.
my scenario:
assume that we have a big 2d char array for example students[20][30] for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name.
no i want to sort this array according to last name.
my Idea:
i defined char mapped[number][16]={""} and mapped 2nd 15 chars of student array. so mapped[i]=student[i] but the result is NULL. I don't now why!!!
when showing student there is nothing to display on screen.
someone help!
here is new bubble sort function:
-
void BubbleSort (void)
-
{
-
char mapped[number][21]={""};
-
for(i=0;i<number;i++)
-
for(j=0;j<15;j++)
-
mapped[i][j]=student[i][j+15];
-
-
cout<<endl<<"mapped List:"<<endl;
-
for(i=0;i<number;i++)
-
cout<<mapped[i]<<endl;
-
//now bubble sorting
-
bool done = false;
-
while (!done)
-
{
-
done = true;
-
for (int n=0; n<number-1; n++)
-
if (strcmp(mapped[n], mapped[n+1]) > 0)
-
{
-
char temp[length+1];
-
strcpy(temp,student[n]);
-
strcpy(student[n], student[n+1]);
-
strcpy(student[n+1], temp);
-
done = false;
-
}
-
}
-
cout<<endl<<"Sorted Student List:"<<endl;
-
for(i=0;i<number;i++)
-
cout<<student[i]<<endl; //nothing displayed!!!
-
}
-
and definiton of student array, if u wanna know:
-
#define number 20
-
#define length 31
-
-
char student[number][length];
-