0

Hi Dears!
I am a Beginner, Please Help Me.
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[35];
   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;
}
4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by cikara21
0

Hi Dears!
I am a Beginner, Please Help Me.
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[35];
   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.

0

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...

Edited by mike_2000_17: Fixed formatting

0

what is this?..

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

let me introduce "strcpy"

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

are u sure that is bubble sort?..

This question has already been answered. 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.