### I have been trying to bubble sort of a set of numbers into ascending order but not able to, please check it out

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

#define SIZE 10

int main()
{
void SetRandomSeed();
int RandomInteger(int LB, int UB);
void OutputXs(int xs[SIZE]);
int BubbleSort(int xs[SIZE]);

printf("The Random Numbers between 1 and 30 are: \n");
int i,j,xs[SIZE];
SetRandomSeed();

for (i = 0; i <= SIZE-1; i++)
{
xs[i] = RandomInteger(1,3*SIZE);
}

OutputXs(xs);

for(i=0; i<SIZE; i++)
{
printf("%d ",BubbleSort(xs));
}

system("PAUSE");
return( 0 );

}

void SetRandomSeed()
{
srand(time(NULL));
}

int RandomInteger(int LB, int UB)
{
return( rand() % (UB-LB+1)+LB);
}

void OutputXs(int xs[SIZE])
{
int i;

for(i = 0; i<=SIZE-1; i++)
{
printf("%2d ",xs[i]);
}
printf("\n");
}

int BubbleSort(int xs[])
{
int i,j,temp;
for(i = 0; i < SIZE-1; i++)
{

for (j = 0; j < SIZE-1; j++)
{
if (xs[j] > xs[j+1])
{
temp = xs[j];
xs[j] = xs[j+1];
xs[j+1] = temp;
}
}

}
}
Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include using namespace std; void bubbleSort(int array[], int size) { int i, j, temp; for(i = 0; i < size; i++) { for(j = 0; j < size - 1; j++) { if(array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } int main() { int i; int array[30]; cout << "The Random Numbers between 1 and 30 are : "; for(i = 0; i < 30; i++) { cout << (array[i] = 1 + rand() % 30); if(i < (30 - 1)) cout << ", "; } cout << endl << endl; bubbleSort(array, 30); cout << "The Sorted Array is : "; for(i = 0; i < 30; i++) { cout << (array[i]); if(i < (30 - 1)) cout << ", "; } cout << endl; cin.get(); return 0; }``````

 ```The Random Numbers between 1 and 30 are : 12, 18, 5, 11, 30, 5, 19, 19, 23, 15, 6, 6, 2, 28, 2, 12, 26, 3, 28, 7, 22, 25, 3, 4, 23, 23, 22, 27, 9, 6 The Sorted Array is : 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 9, 11, 12, 12, 15, 18, 19 , 19, 22, 22, 23, 23, 23, 25, 26, 27, 28, 28, 30```

The program is different, be sure to adapt it properly to your needs.
Topic archived. No new replies allowed.