Need help with my bubble sort!
Nov 16, 2011 at 1:30am UTC
I have a programming project where I must generate 10,000 random numbers and sort them out using bubble sort and shell sort. But before I start on shell sort i need to get this out of the way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
double BubbleSort(double array[]);
int main()
{
double lowest=-1, highest=1, range=(highest-lowest);
double array[10000];
srand((unsigned )time(0));
for (int i=0; i<10000; i++)
{
array[i] = lowest+double (range*rand()/(RAND_MAX ));
cout << BubbleSort(array);
}
}
double BubbleSort(double array[])
{
int f, j, flag =1;
double temp;
for (f=0; f <=10000; f++)
{
for (j=f+1; j<=6; j++)
{
temp = array[f];
array[f] = array[j];
array[j] = temp;
}
}
for (f=0; f<=10000; f++)
{
cout << endl << array[f] << endl;
}
return array [f];
}
for some reason it is not seeing out actual numbers instead just one number repeatedly. Any ideas on the problem???
Nov 16, 2011 at 2:04am UTC
you declaed flag variable but it is not used at all in "BoubleSort" function.
I think you shall use it after line 41 to indicate that change has been made.
also puting flag between 36 and 37 and set it to false.
to make flag variable work you have to use if statement to check if next number is higher that this.
then change the flag if so.
Topic archived. No new replies allowed.