Hi Dears!
I Wrote A Bubble Sort for sorting 2D array of characters. and logically it seems correct, but unfortunately not works. please help me.
this is my output:
unsorted:
mah
ali
sal
Sorted:
ali
ali
mah
Here is my Code so far:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define number 3
#define length 3
char student[number][length];
int i,j,k;
bool done;
void BubbleSort (void);
void main(){
for(i=0;i<number;i++)
for(j=0;j<length;j++)
cin>>student[i][j];
BubbleSort();
cout<<endl;
for(i=0;i<number;i++){
for(j=0;j<length;j++){
cout<<student[i][j];}
cout<<endl;}
getchar();
}
void BubbleSort (void)
{
bool done = false;
int limit = 0,size=number;
while (!done)
{
done = true;
for (int n=0; n<size-limit-1 ; n++)
if (strcmp(student[n], student[n+1]) > 0)
{
char temp[10];
strcpy(temp,student[n]);
strcpy(student[n], student[n+1]);
strcpy(student[n+1], temp);
done = false;
}
limit++;
}
}
what is my mistake? I can't Understand.
please help me!