Bubble Sort Random Array
Mar 31, 2013 at 11:08am UTC
Hi, I am new here and im looking for some help. I am trying to create an array with 800 element that are randomly arranged within the array. Once this is complete i want to use the bubble sort algorithm to organize the number in ascending order. I have the following code but it doesn't seem to work, Any help will be great help, Thanks
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUBBLE 800
int main()
{
int array[BUBBLE];
int i, j;
int temp = 0;
srand(time(NULL));
for (i = 0; i < BUBBLE; i ++)
{
printf("%d \n" , rand() % BUBBLE + 1);
for (j = 0; j < BUBBLE; j++);
{
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
system("PAUSE" );
return 0;
}
Mar 31, 2013 at 2:24pm UTC
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUBBLE 800
int main()
{
int myArray[BUBBLE];
int i, j;
int temp = 0;
int num;
srand(time(NULL));
//fill array
for (i = 0; i < BUBBLE; i ++)
{
num = rand() % BUBBLE + 1;
myArray[i] = num;
}
//sort array
for (i = 0; i < BUBBLE; i++)
{
for (j = 0; j < BUBBLE-1; j++)
{
if (myArray[j] > myArray[j+1])
{
temp = myArray[j];
myArray[j] = myArray[j+1];
myArray[j+1] = temp;
}
}/*End inner for loop*/
}/*End outer for loop*/
//print array
for (i = 0; i < BUBBLE; i++)
{
printf("%d\n" ,myArray[i]);
}
system("PAUSE" );
return 0;
}/*End of main*/
Last edited on Mar 31, 2013 at 2:25pm UTC
Mar 31, 2013 at 2:59pm UTC
thanks a million Yanson!
Topic archived. No new replies allowed.