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
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
50
51
#include <iostream>
#include <ctime>
#include <cstdlib>

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.