Bubble sort with random array

Hey so I am writing a program that sorts a randomly generated list five different ways (bubbleArray, selectionArray, insertionArray, quickArray, and mergeArray). Right now I believe that I have it sorting by bubbleSort.

Can someone verify?

All of the examples of bubbleSort that I've seen differ.

My code seems to output the largest number three times. How do I correct this?
Also, how do I get it to output to a separate text file?

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
#include <iostream>
#include <math.h> 
#include <stdlib.h>
#include <time.h>   
using namespace std;

int main()
{
	int i, j, n=10, temp;
	int bubbleArray [n];
	
	srand (time(NULL));
	
	cout << "This is the randomly generated list: ";
	for (i = 0; i < 10; i++)
	{
		bubbleArray[i] = rand() % 100 + 1;
		cout << bubbleArray[i] << " ";
	}
	cout << "\n";
	cout << "\nThis is the list after being sorted by the bubble sort method: ";
	i = 0;
	j = 0;
	do	{
		j= n-1;
		do {
			if (bubbleArray[j-1] > bubbleArray[j])
				{
					temp = bubbleArray[j-1];
   					bubbleArray[j-1] = bubbleArray[j];
    				bubbleArray[j] = temp;
				}
			else
			j = j-1;
		} while (j > i);
		i = i+1;
		cout << bubbleArray[j] << " ";
	} while (i <= n);
	
	
	return 0;
}
Last edited on
This is a very obscure version of bubble sort! I think you are actually outputting the second largest three times.

Your i loop runs too many times and you write out at the end of each loop irrespective of whether that loop was necessary. With your current logic you have no conceivable way of outputting bubbleArray[n-1], the last entry, since you started at n-1 and subtracted 1 at least once.

The outer loop in bubblesort should run (a maximum of) n-1 times, but you need to output n numbers. So don't do both together: sort first and then output the array. (In fact I would probably sort in a separate function).

Change your logic to do all the loop tests at the start of the loop or (better) change to entirely for-loops: you know exactly how many times each has to run.

Line 10 is illegal in standard c++ (at the moment).

Line 33 is redundant and makes you do the check twice if you swap anything.



Like many sorting algorithms, it's quite nice by recursion. Actually, in this instance I think it would make the limits in the non-recursive version more obvious.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;

void bubbleSort( int A[], int n )
{
   if ( n > 1 )
   {
      for ( int j = 0; j < n - 1; j++ ) if ( A[j] > A[j+1] ) swap( A[j], A[j+1] );
      bubbleSort( A, n - 1 );
   }
}

int main()
{
   const int N = 10;
   int A[N];
   srand( time( 0 ) );
   for ( int &e : A ) e = 1 + rand() % 100;

   cout << "Original: ";
   for ( int e : A ) cout << e << ' ';

   bubbleSort( A, N );
   cout << "\nSorted:   ";
   for ( int e : A ) cout << e << ' ';
}

Original: 26 26 83 22 47 98 69 15 68 90 
Sorted:   15 22 26 26 47 68 69 83 90 98 

Last edited on
So I am trying to take it piece by piece and here is how I am trying to start it to get all of the same randomly generated lists for each of the five arrays. My output is jumbled as well. Any ideas?

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
#include <iostream>
#include <math.h> 
#include <stdlib.h>
#include <ctime>   

using namespace std;

int main()
{
	int i, j, n=10, temp;
	int bubbleArray [n];
	int selectionArray [n];
	int insertionArray [n];
	int quickArray [n];
	int mergeArray [n];
	srand (time(NULL));
	
	
	for (i = 0; i < 10; i++)
	{
		int value = rand() % 100 + 1; // Random from 1 through 100
		bubbleArray [i] = value;
		selectionArray [i] = value;
		insertionArray [i] = value;
		quickArray [i] = value;
		mergeArray [i] = value;
		
		
		cout << "This is the randomly generated list for bubbleArray: " << bubbleArray[i] << " ";
				
	}
		
	
	
	system("pause");
	return 0;
}



This is the randomly generated list for bubbleArray: 73 
This is the randomly generated list for bubbleArray: 59 T
his is the randomly generated list for bubbleArray: 77 
This is the randomly generated list for bubbleArray: 81 
This is the randomly generated list for bubbleArray: 62 
This is the randomly generated list for bubbleArray: 31 
This is the randomly generated list for bubbleArray: 86 
This is the randomly generated list for bubbleArray: 38 
This is the randomly generated list for bubbleArray: 31 
This is the randomly generated list for bubbleArray: 35 
Press any key to continue . . .
Last edited on
Topic archived. No new replies allowed.