Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Having trouble with comparison of MergeSort and BubbleSort

$
0
0
Hey everyone! First time here!

As the title suggests, I'm having trouble making a comparison between a slow sort, specifically MergeSort, and a faster sort, specifically BubbleSort. According to the professor, the output is supposed to be:

The seed to use is: 39

Bubble sort had 25349145 swaps

The first 10 values: 0 0 0 3 3 6 6 7 7 9

The last 10 values: 9991 9991 9991 9992 9992 9994 9997 9997 9998 9998



Merge sort had 133616 moves

The first 10 values: 0 0 0 3 3 6 6 7 7 9

The last 10 values: 9991 9991 9991 9992 9992 9994 9997 9997 9998 9998

But that's not exactly what I'm getting. Here's my code:

#include <stdlib.h>
#include <random>
#include <iostream>
using namespace std;
typedef int DataType;

void bubbleSort(DataType[], int);
void swap(DataType&, DataType&);
void mergeSort(DataType[], int, int);
void merge(DataType[], int, int, int);
void radix(DataType[], int, int, int);

const int SIZE = 10000;
int count = 0;

int main()
{
int seed = 39;
int theArray[SIZE];
srand(seed);
    for(int i = 0; i < SIZE; i++)
    {
    theArray[i] = rand() % SIZE;
    }

bubbleSort(theArray,SIZE);

cout << "The seed value is: " << seed << endl;
cout << "Bubble Sort had " << count << " swaps." << endl;
cout << "The first 10 values are: ";
for (int i = 0; i < 10; i++)
{

cout << theArray[i] << " ";
}
cout << "\nThe last 10 values are: ";
for (int i = SIZE-10; i < (SIZE); i++)
{
    cout << theArray[i] << " ";
}
cout << "\n" << endl;

mergeSort(theArray,0,SIZE-1);

cout << "Merge Sort had " << count << " swaps." << endl;
cout << "The first 10 values are: ";
for (int i = 0; i < 10; i++)
{
    cout << theArray[i] << " ";
}

cout << "\nThe last 10 values are: ";
for (int i = SIZE-10; i < (SIZE); i++)
{
    cout << theArray[i] << " ";
}

cout << "\n" << endl;

system("PAUSE");
return 0;
}

void bubbleSort(DataType theArray[], int n)
{
bool sorted = false;
for(int pass = 1; (pass < n) && !sorted; ++pass)
{
    sorted = true;
    for (int index = 0; index < n-pass; ++index)
    {
        int nextIndex = index+1;
        if(theArray[index] > theArray[nextIndex])
        {
            swap(theArray[index],theArray[nextIndex]);
            count++;
            sorted = false;
        }
    }
}
}

void swap(DataType& x, DataType& y){
DataType temp = x;
x = y;
y = temp;
}

void mergeSort(DataType theArray[], int first, int last)
{
    if(first < last)
    {
        int mid=((first+last)/2);
        mergeSort(theArray, first, mid);
        mergeSort(theArray, mid+1, last);
        merge(theArray, first, mid, last);
    }
}

void merge(DataType theArray[], int first, int mid, int last){
DataType tempArray[SIZE];
int first1 = first;
int last1 = mid;
int first2 = mid+1;
int last2 = last;
int index = first1;

for(; (first1<=last1) && (first2<=last2); ++index){
if(theArray[first1] < theArray[first2]){
tempArray[index] = theArray[first1];
++first1;
}
else{
tempArray[index] = theArray[first2];
++first2;
}
}
for(; first1 <= last1; ++first1, ++index){
tempArray[index] = theArray[first];
}
for(; first2 <= last2; ++first2, ++index){
tempArray[index] = theArray[first2];
}
for(index = first; index <= last; ++index){
theArray[index] = tempArray[index];
}
}



Any input would be greatly appreciated! :)/>

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>