c++ Bubble Sort vs Radix Sort

741 views Asked by At

I have been working on this program for a project. Everything runs fine but there is something wrong with my bubble sort. I ran and displayed the result of the function but every so often it shows negative numbers, which it should NOT. And also every so often it does not sort properly, meaning it the my bubble sort function is not sorting them in order some what.

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int const temp = 10000;

void bubbleSort ( int array [ temp ] );  
void radixSort ( int * array, int arraySize );
void display ( int btime);

int main ( )
{
    int A1;
    int array [ temp ];
    char a = '\0';

    cout << "\nWould You Like To Perform Bubble and Radix Test? (y/n): ";
    cin >> a;

    while ( a == 'y' || a == 'Y' )
    {
        srand ( ( unsigned ) time ( 0 ) );

        for ( size_t i = 0; i < temp; i++ )
        {
            A1 = ( rand ( ) % temp ) + 1 ;           
            array [ i ] = A1;            
            bubbleSort ( array );
        }
    }

    while ( a == 'n' || a == 'N' )
    {
        break;
    }

    return 0;
}

void bubbleSort ( int array [ temp ] )
{
   for ( int i = 0; i < temp; i++ )
    {
        for ( int j = 0; j < temp - 1; j++ )
        {
            if ( array [ j ] > array [ j + 1 ] )
            {
                int temp = array [ j ];
                array [ j ] = array [ j + 1 ];
                array [ j + 1 ] = temp;

                //Test: to see if its working properly.
                cout << array [ j + 1 ] << endl; //idk if this should be placed here
           }
        }
    }  
}
1

There are 1 answers

0
mazhar islam On

Problems in your code:

  1. Your while loop. You don't make any breaking condition in the loop.

Solution: Use you own preferred breaking condition. Something like:

while ( true )
    {
        cin >> a;
        if(a == 'y' || a == 'Y') {
            //your array init and bubblesrt method calling here
        } else {
           break;
        }
    }
  1. Calling bubbleSort with an uninitialized array in a for loop (!).

Solution:

Put the bubbleSort method calling outside of for loop,

for ( size_t i = 0; i < temp; i++ )
   {
      A1 = ( rand ( ) % temp ) + 1 ;           
      array [ i ] = A1;            
   }
bubbleSort ( array );

Following is the 'proof' of bubbleSort() method implementation which actually works perfectly:

#include <iostream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int const TEMP = 4;

void bubbleSort ( int array [ TEMP ] );


int main ( )
{
    int array [ TEMP ] = {3,2,-1,0}; // a simple demonstration
    bubbleSort(array);

    for (int i = 0; i < TEMP; i++) {
        cout<< array[i];
    }
    return 0;
}

void bubbleSort(int array[TEMP]) {
int temp;
for (int i = 0; i < TEMP -1; i++) {
    for (int j = 0; j < TEMP -i - 1; j++) {
        if (array[j] > array[j + 1]) {
            temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }
}

And the output is:

-1023

Cheers!