Thread: C+ Sorting

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    7

    Angry C+ Sorting

    I am working on a sorting program that generates a list of random numbers and does a sort. I have run across some programs in an old C book. In the bubble sort it doesn't seem to work. Since I am not a programmer just a college student I am having trouble finding the problem with this program. I know the program needs modified to generate random numbers but haven't gotten that far yet. Anybody see the problem with this program?

    Bubble Sort

    #include <stdio.h>
    void sort (int [],int);
    void print_it (int [], int);

    int main(void)
    {
    int doit[] = {4, 6, 8, 2, 4, 1, 9, 23, 99, 77, 55, 33, 11}; /* Can replace this with the random generated numbers*/
    int size;

    size = sizeof(doit) / sizeof(int);
    sort (doit, size – 1);
    print_it(doit, size);
    }
    void sort (int array[], int limit)
    {
    int row, column, temp, sorted;

    for (row = 0, row <= limit; row++)
    {
    sorted = 0;
    for (column = 0, column <= limit – 1 ; column++)
    {
    if (array[column] > array[column + 1])
    {
    sorted = 1;
    temp = array[column];
    array[column] = array[column + 1];
    array[column + 1] = temp;
    }
    }
    if (sorted = = 0)
    break;
    }
    }
    void print_it (int doit[], int limit)
    {
    int I;
    (for (i = 0; i < limit; i++)
    {
    printf(“%4i”, doit[i]);
    if (((i + 1) % 14) = = 0) putchar (‘\n’);
    }
    putchar(‘\n’)
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I just posted something else with sorting and strings using the STL set container. You can easily modify that to store ints instead of strings. When you insert the numbers into the container, just call whatever function you have that generates random numbers. The set will automatically be sorted lowest to highest.

    Code:
    #include <iostream>
    #include <set>
    using namespace std;
    ...
    ...
    ...
    set<int> IntSet;
    set<int>::iterator it;
    
    IntSet.insert( RandomNumberGenerator() );  // Insert 1st random num
    IntSet.insert( RandomNumberGenerator() );  // Insert 2nd random num
    IntSet.insert( RandomNumberGenerator() );  // Insert 3rd random num
    
    for( it = IntSet.begin(); it != IntSet.end(); ++it )
        cout << *it << endl;  // Write out sorted list of random integers
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Do you mean it won't compile or it produces the wrong output? You have a few syntax errors that will cause the compiler to bark(assuming the errors werent introduced when you typed it here). The bubble sort looks okay, although I'm no expert on bubble sorts.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    7
    Yes, there are several syntax errors. What I'm trying to do is replace the fixed set of numbers with a way for the program to randomly generate a given set of numbers and then do the bubble sort. I've tried adding the code provided by "hk_mp5kpdw" and I just get more errors. I have no trouble finding individual codes but when I try putting them together the program doesn't like me. Once I get the random generated numbers to work and do a bubble sort, I will then try to include a code to tell me how long it took to sort the list of random numbers.

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    There are errors in for statements (for(a;b;c), not for(a,b;c)) and two == operators have been written as = = 's. Also, one extra bracket seems to be there. All the errors can be easily seen with a good editor.

    Btw, why using a container set to sort the items? Why not just insert them straightly in O(1) time and sort them afterwards with sort? This should be faster and some vector could be used.

    However, probably the bubble sort "was supposed" to be used.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Hrm, I can only think of one way to sort C+... and that is via expiry date.

  7. #7
    Unregistered
    Guest
    I'm sure there is a better faster way to have the program select random numbers and put into an array and do the sort all the while telling me how long it took to do the sort. My assignment was to have the program randomly select a set of numbers and then I will have to do 4 sorts (bubble, insertion, quick, can't think of the fourth) for differing set sizes (i.e. 1000, 5000, 10000, etc numbers). I will then need to prepare an insert to the program that will tell me how long it took to do the sort from my computer clock. The output will develop a 4 column table with headers telling the total set size and rows for each sort. The time taken to perform the task will be input into the table (ie for bubble sorting 1000 numbers might take 50 secs, therefore the table will have 50 secs).

    If anyone has a shorter program to do all this I'm all ears. Right now with just the bubble sort and insertion sort I'll have about 4 pages of code. talk about getting lost. Yikes!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM