If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Qsort to BubbleSort using C-programming

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-06, 19:04
Mike Kurtz Mike Kurtz is offline
Registered User
 
Join Date: Feb 2006
Posts: 1
Question 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));

}
Reply With Quote
  #2 (permalink)  
Old 02-06-06, 15:17
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
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.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On