Bubble Sort

Okay, so I just created a bubble sort to sort 50 random numbers in an array. I've been able to get it down to 1134 comparisons so far. I'm pretty sure that's the least number possible, but does anyone know if I can get it reduced even lower? I want it to organize these numbers with the least amount of comparisons possible. Here's my code:

Loops:

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
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
using namespace std;

const int SIZE = 50;
int globalCount = 0;

bool compare(int, int);
void swap(int&, int&);

int main( )
{
	// declare and initialize a large array
	int data[SIZE];
	srand(0); // seed the random number generator
	for (int i = 0; i < SIZE; i++)
	{
		data[ i ] = rand( );
	}
	cout << "\nPrint out the unsorted array:";
	for (int i = 0; i < SIZE; i++)
	{
		cout << "\ndata[ " << i << " ] = " << data[ i ];
	}
	cout << endl;
	
	//Bubble Sort Starts!
	
	int j = 0;
	
	bool again = true;
	
	while ((j<SIZE-1 && again == true))
	{
		int count = 0;
		again = false;
		for (int i = 0; i < SIZE-1-j; i++)  // index for inner loop is i
		{
			if (compare(data[i],data[i+1]) == true)
			{
				swap( data[ i ], data[ i + 1] );
				count++;
			}
		}
		if (count > 0)
			again = true;
		j++;
	}
	
	
	
	// print out the sorted array and the value of globalCount
	for (int i = 0; i < SIZE; i++)
	{
		cout << "\ndata[ " << i << " ] = " << data[ i ];
	}
	cout << "\n\n";
	cout << globalCount << " Comparison's were done to sort the array.\n";
	
	system("PAUSE");
	return 0;
}


Compare() function - Used to compare and keep track of the number of compares.

1
2
3
4
5
6
7
8
9

bool compare(int n1, int n2)
{
	globalCount++;
	if ( n1 > n2)
		return true;
	else
		return false;
}


Swap() function

1
2
3
4
5
6
7
void swap (int& n1, int& n2)
{
	int temp;
	temp = n1;
	n1 = n2;
	n2 = temp;
}


Edited to add missing code***
Last edited on
I think you're accessing past the end of the array:
When j==0, i==[0..n-1-j]==[0..n-1].
Later on, you're comparing data[i] and data[i+1]; OR, data[n-1] and data[n-1+1]

I doubt you can get many less comparisons than (n^2-n)/2 without the array being partially sorted at the start. If you need more speed, change algorithms. Insertion sort has good times.
Last edited on
I've been doing my bubble sorts in one easy function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void sortArray(int array[], int size)
{
	int  temp;
	bool swap;

	do
	{	
               swap = false;
		for (int count = 0; count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}//if
		}//for
   } while (swap);
}//sortArray 
bubble sort runs in O(n^2) comparisons on average. You can't get any faster than that on average.

I'm unable to get this program, can you help me
is it using template class
Topic archived. No new replies allowed.