Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2006

    Question 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);
    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. #2
    Join Date
    Jun 2004
    Arizona, USA
    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.
    "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

Posting Permissions

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