Thread: sorting a string with an integer

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    sorting a string with an integer

    im trying to store an integer with a string. The string has an associated integer used to reference the string. the integers and strings are read in from a file and are in random order. then they have to be printed to an output file in sorted order. this is easy to do with the array of integers but the strings stay in the order they came in. i am using an array of structs but i cannot get the strings to stay with their corresponding integer.

    first i tried storing the string in the actual number location of its integer but the integers go up to several thousands and i dont really want to malloc to 50,000 to try and cover all of the possibilities.

    im trying to avoid linked lists but if you think that is a better solution let me know. thanks in advance for the help.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by ominub View Post
    im trying to store an integer with a string. The string has an associated integer used to reference the string. the integers and strings are read in from a file and are in random order. then they have to be printed to an output file in sorted order. this is easy to do with the array of integers but the strings stay in the order they came in. i am using an array of structs but i cannot get the strings to stay with their corresponding integer.
    The above paragraph seems to contradict itself
    You want the output sorted yet the strings have to be in the same order they came in?
    Quote Originally Posted by ominub View Post
    first i tried storing the string in the actual number location of its integer but the integers go up to several thousands and i dont really want to malloc to 50,000 to try and cover all of the possibilities.
    Not sure why you want to malloc() to 50,000? A single malloc for storing an unsigned int will hold a number as large as 4294967295.
    Quote Originally Posted by ominub View Post
    im trying to avoid linked lists but if you think that is a better solution let me know. thanks in advance for the help.
    An array of structs will work fine as long as you know what to sort on.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a perfect job for an index sort.

    In an index sort, the original data remains exactly in the same order as you received it. No data is moved.


    This program keeps the integer in a parallel int array, with it's string in a char array with two dimensions.

    Quite simple and perfect for when you don't want data, to be moved, but need to have it displayed or output
    to a file, in sorted order.

    Code:
    /* IndxSort.c by Adak. A small example of sorting through an index 
    status: ok
    */
    
    #include <stdio.h>
    
    #define NumItems 4
    #define StringLen 30
    
    int main() {
    
       int i, j, temp; 
    
       int Index[NumItems];
       //make 2 arrays, (which work in parallel). One for strings, and one for the int
       int Int[NumItems] = { 7, 2, 11, 6 };
    
       //and a few lines from The Rhyme of the Ancient Mariner:
       char Strings[NumItems][StringLen] = { 
       {  "Water, water, everywhere" },
       {  "And all the boards did shrink" },
       {  "Water, water, everywhere" },
       {  "Nor any drop to drink" }
       }; 
    
       //initialize the index array
       for(i = 0; i < NumItems; i++)
          Index[i] = i;
    
       //now do a simple sort, using the initialized index array and the Int array
    
       for(i = 0; i < NumItems - 1; i++)  {
          for(j = i + 1; j < NumItems; j++)  {
             if(Int[Index[i]] > Int[Index[j]] ) {
                temp = Index[i];
                Index[i] = Index[j];
                Index[j] = temp;
             }
          }
       }
    
       //Now to see the sorted data, we do it by using the Index array 
       printf("\n\n\n Int and String data, sorted by Int[]:\n\n");
       for(i = 0; i < NumItems; i++)  
          printf("Int: %d  String: %s \n", Int[Index[i]], Strings[Index[i]]);
    
       printf("\n\n\n Original Int and String data:\n\n");
       //to see the unsorted data
       for(i = 0; i < NumItems; i++)
          printf("Int: %d  String: %s \n", Int[i], Strings[i]);
    
    
       printf("\n\n\t\t\t     press enter when ready");
       i = getchar();
       return 0;
    }
    BTW: you can do what you need, using your structs, as well. This is just another option.

    I used a simple sorter, so every line of code could be shown easily. There's no reason the sorter couldn't be a Quicksort, mergesort, or comb-11 sort, however.
    Last edited by Adak; 12-04-2009 at 03:01 PM.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    1
    thank you for this sample program...i got an idea on how to make my second project..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM