
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’)
}

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

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.

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.

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.

Hrm, I can only think of one way to sort C+... and that is via expiry date.

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!!