Hi Dears!
If you Can Help me to find out how to sort a 2d arrays of characters with recursive bubble sort algorithm.
I have got something but it doesn't work.
what is my mistake?

I Did codes below So far, but it isn't completed and has some errors. it is my primary Idea. check it and learn me:

``````#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define number 3
#define length 3

static char student[number][length];
int i,j,k,NF_Length=15,n;
bool done;
void BubbleSort(void);
bool BubbleUp(int);

void main(){
for(i=0;i<number;i++)
for(j=0;j<length;j++)
cin>>student[i][j];

BubbleSort();

for(i=0;i<number;i++)
for(j=0;j<length;j++)
cout<<student[i][j];
getchar();
}
void BubbleSort(void){

if (number < 2)   // array must be sorted
return;

done = BubbleUp(number);
if (!done){                         // then bubblesort the rest
--number;
BubbleSort();
}
}
bool BubbleUp(int n){
char Temp;
done = true;   // until we know better
for (k = 0; k < NF_Length - 1; k++){
if (strcmp(student[n][k],student[n][k+1])>0)
{
done = false;   // because we are making a swap
//Now Swapping
strcpy(Temp,student[n]);
strcpy(student[n],student[n+1]);
strcpy(student[n+1],Temp);
}
}
return done;
}``````

## All 3 Replies

Hi Dears!
If you Can Help me to find out how to sort a 2d arrays of characters with recursive bubble sort algorithm.
I have got something but it doesn't work.
what is my mistake?

I Did codes below So far, but it isn't completed and has some errors. it is my primary Idea. check it and learn me:

``````#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define number 3
#define length 3

static char student[number][length];
int i,j,k,NF_Length=15,n;
bool done;
void BubbleSort(void);
bool BubbleUp(int);

void main(){
for(i=0;i<number;i++)
for(j=0;j<length;j++)
cin>>student[i][j];

BubbleSort();

for(i=0;i<number;i++)
for(j=0;j<length;j++)
cout<<student[i][j];
getchar();
}
void BubbleSort(void){

if (number < 2)   // array must be sorted
return;

done = BubbleUp(number);
if (!done){                         // then bubblesort the rest
--number;
BubbleSort();
}
}
bool BubbleUp(int n){
char Temp;
done = true;   // until we know better
for (k = 0; k < NF_Length - 1; k++){
if (strcmp(student[n][k],student[n][k+1])>0)
{
done = false;   // because we are making a swap
//Now Swapping
strcpy(Temp,student[n]);
strcpy(student[n],student[n+1]);
strcpy(student[n+1],Temp);
}
}
return done;
}``````

The formatting is giving me a headache. Can you repost with formatted code? Note that if you mix tabs with spaces, it often looks awful when posting here. Tabs are 8 spaces wide here.

Why are you recursively looping a function like that? It's hard to follow.
Also not sure why you're not using `int main()`, globals in that way, string.h, or idk this is way more complex than it needs to be...

what is this?..

``````#define number 3
//...
--number;``````

let me introduce "strcpy"

``strcpy(char *_destination, const char *_source);``

are u sure that is bubble sort?..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.