hi..stuck in sorting

This is a discussion on hi..stuck in sorting within the C Programming forums, part of the General Programming Boards category; hi..i try to implement the bubble sorting where it read from file..i mean line by line then break into token..it ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    hi..stuck in sorting

    hi..i try to implement the bubble sorting where it read from file..i mean line by line then break into token..it can break into token but it can't sort the words..
    Eg. input
    Code:
    "this is a string"
    output from the program.
    Code:
    "this
    is 
    a 
    string"

    output that i want..
    Code:
    "a 
    is 
    string 
    this"
    it seems like it din't pass any value into the bubbleSort..my bubble sort function can work with character by character but i jus want it works by words by words..can give some tips where i did wrong in my program??..thanks..

    here is the program:
    Code:
    /* header file */
    #include <stdio.h>
    #include <stdlib.h>
    typedef char* Item;
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "b.h"
    #define M 50
    
    
    int main()
    {
        FILE *infile, *outfile;
        char line[1000], words[1000];
        char *token;
        char temp[1000];
        char *filename = "test.txt";
        char *delimeter = " ";
        
        if((infile = fopen(filename, "r")) == NULL)
        {
           printf("Error");
        }
        
        if (fgets(line, sizeof(line), infile) != NULL)
        {
    
        printf("%s\n", strtok(line, " "));
    
       while ( (token=strtok(NULL, " ")) != NULL)
       {
        bubble(&token, M);
        printf("%s\n", token);
        
       }
        
    }
        return 0;
    }
    
    void bubble(Item a[], int N)
    {
         int pass, pos;
         Item temp;
         for (pass = 1; pass < N; pass ++)
         
             for (pass = N - 1; pos >= pass; pos --)
             {
                 if (a[pos] < a[pos-1])
                 {
                     *temp = *a[pos];
                     *a[pos] = *a[pos-1];
                     *a[pos-1] = *temp;
                 }
             }
    }
    Last edited by cBegginer; 09-19-2005 at 11:12 AM. Reason: enter wrong output

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Is it just me or your output from your program is the same as the output you want (at least in your post) ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    hey man..sorry..i had type wrong the original output..n the code had been edited..sorry..

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    for (pass = 1; pass < N; pass ++)
         
             for (pass = N - 1; pos >= pass; pos --)
             {
    Maybe that should be pos?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
     Item temp;
                 if (a[pos] < a[pos-1])
                 {
                     *temp = *a[pos];
                     *a[pos] = *a[pos-1];
                     *a[pos-1] = *temp;
                 }
    Get rid of the asterisks. temp is not a pointer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Your passing the string one by one.

    Code:
       while ( (token=strtok(NULL, " ")) != NULL)
       {
        bubble(&token, M);
        printf("%s\n", token);
        
       }
    The first loop will pass the word "this" and sort its character. The printf will print "ihts" which is the sorted "this" string. and itll do that with the other tokenized words.

    My suggestion is use a 2d array to pass the all strings. and use strcmp to compare strings.

    Code:
             for (pass = N - 1; pos >= pass; pos --)
             {
                 if (a[pos] < a[pos-1]) // replace this with strcmp
                 {
                     *temp = *a[pos];
                     *a[pos] = *a[pos-1];
                     *a[pos-1] = *temp;
                 }
             }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You didn't notice . . .
    Code:
    for (pass = N - 1; pos >= pass; pos --)
    etc.

    A 2D aray is a good idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    char *token;
    bubble(&token, M);
    typedef char* Item;
    void bubble(Item a[], int N)
    Array of pointers is not pointer to a pointer.although it is logically correct in C.but it is confusing so better go for array

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    thanks

    hey..guys thanks for helping me out..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  3. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21