# Thread: Qsort to BubbleSort using C-programming

1. Registered User
Join Date
Feb 2006
Posts
1

## Unanswered: Qsort to BubbleSort using C-programming

How would I convert this C-program which alphbetiizes a string using qsort into a C-program which does the same thing using BubbleSort instead?:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 7

int comp(const void *s1, const void *s2);

int main( void )
{
char *data[MAX], buf[80], *ptr, *key, **key1;
int count;

printf("Enter %d words, pressing Enter after each.\n",MAX);

for (count = 0; count < MAX; count++)
{
printf("Word %d: ", count+1);
gets(buf);
data[count] = malloc(strlen(buf)+1);
strcpy(data[count], buf);
}

qsort(data, MAX, sizeof(data[0]), comp);

for (count = 0; count < MAX; count++)
printf("\n%d: %s", count+1, data[count]);

return 0;
}

int comp(const void *s1, const void *s2)
{
return (strcmp(*(char **)s1, *(char **)s2));

}

2. Super Moderator
Join Date
Jun 2004
Location
Arizona, USA
Posts
1,848
There's no way that I would venture C code, but, the basic bubble sort algorithm is about the simplest sort imaginable. And, it is quite effecient when N (the number of elements to sort) is small - less than 50 or so.

Essentially, you have two loops, nested. Assume the loop counters are I and J. The outer loop (I) iterates from 1 to N-1. The inner loop (J) iterates from the I to N. You simply compare the elements I and J. If Element(I) < Element(J), then swap them. Then, continue through the rest of the iterations.

That's it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•