Ok... Got a lab to generate 150 random numbers and then sort the 1st 50 by BUBBLE SORT, the 2nd 50 by SHELL SORT, and the 3rd 50 by SELECTION SORT. After I get those three to work correctly then I need to MERGE SORT the three of them together.
I've got the 150 random numbers and the bubble sort working, but the shell & selection are off....
I'm sure I have issues with the merge as well but I guess I'll worry about that after the three lists are correct.
ANy ideas/suggestions?
/////////////////////////////////////////////////////////////////////////////////
//*******************************************************************************
//
// TITLE: Lab6 - Main Program
// FILENAME: Main.cpp
// PREPARED FOR: CS230
// PROGRAMMER(S):
// DEVELOPMENT DATE: 12/08/12
// COMPILER USED: MS Visual C++ 2010 Express - Version 10
// TARGET PLATFORM: x86 Windows Platform
//
//*******************************************************************************
/////////////////////////////////////////////////////////////////////////////////
//
// *** TEST PLAN ***
//
// 1) Generate 150 random numbers from between 0 - 1000 and display them
//
// 2) Take the 1st 50 and display them, then BUBBLE SORT them and display again
//
// 3) Take the 2nd 50 and display them, then SHELL SORT them and display again
//
// 4) Take the 3rd 50 and display them, then SELECTION SORT them and display again
//
// 5) Take the 3 sorted lists and MERGE SORT then together and display final list
//
/////////////////////////////////////////////////////////////////////////////////
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
void BubbleSort(int ArraySort_1[ ]);
void ShellSort(int ArraySort_2[ ]);
void SelectionSort(int ArraySort_3[ ]);
void MergeSort(int ArraySort_1[ ],int ArraySort_2[ ], int ArraySort_3[ ]);
void main( )
{
int InitArray[150], ArraySort_1[50], ArraySort_2[50], ArraySort_3[50];
cout << endl << "\t ---> THIS IS THE RANDOMLY GENERATED ARRAY OF 150 ELEMENTS <---" << endl;
cout << "\t ----------------------------------------------------------------" << endl;
srand ((unsigned) time(0));
for (int i = 0; i < 150; i++)
{
InitArray[i] = rand( ) %1000;
cout << InitArray[i] << " ";
Sleep(1);
}
////////////////////// Bubble Sort //////////////////////
cout << endl << endl << endl << endl << "\t * 1A: THIS IS THE FIRST 50 ELEMENTS OF THE UNSORTED ARRAY *"<< endl;
cout << "\t -------------------------------------------------------------" << endl;
for (int i = 0; i < 50; i++)
{
ArraySort_1[i] = InitArray[i];
cout << ArraySort_1[i] << " ";
}
cout << endl << endl << "\t * 1B: THIS IS THE BUBBLE SORT OF THE FIRST 50 ARRAY *" << endl;
BubbleSort(ArraySort_1);
for (int i = 0; i < 50; i++)
{
cout << ArraySort_1[i] << " ";
}
////////////////////// Shell Sort //////////////////////
cout << endl << endl << endl << endl << "\t ** 2A: THIS IS THE SECOND 50 ELEMENTS OF THE UNSORTED ARRAY **" << endl;
cout << "\t ----------------------------------------------------------------" << endl;
for (int i = 50; i < 99; i++)
{
ArraySort_2[i - 50] = InitArray[i];
cout << ArraySort_2[i] << " ";
}
cout << endl << endl <<"\t ** 2B: THIS IS THE SHELL SORT OF THE SECOND 50 ARRAY **" << endl;
ShellSort(ArraySort_2);
for (int i = 0; i < 49; i++)
{
cout << ArraySort_2[i] << " ";
}
////////////////////// Selection Sort //////////////////////
cout << endl << endl << endl << endl << "\t *** 3A: THIS IS THE THIRD 50 ELEMENTS OF THE UNSORTED ARRAY ***" << endl;
cout << "\t -----------------------------------------------------------------" << endl;
for (int i = 100; i < 149; i++)
{
ArraySort_3[i - 100] = InitArray[i];
cout << ArraySort_3[i] << " ";
}
cout << endl << endl << "\t *** 3B: THIS IS THE SELECTION SORT OF THE THIRD 50 ARRAY ***" << endl;
SelectionSort(ArraySort_3);
for (int i = 0; i < 49; i++)
{
cout << ArraySort_3[i] << " ";
}
////////////////////// Merge Sort //////////////////////
cout << endl << endl << endl << endl << "\t **** 4A: THIS IS THE MERGE SORT OF THE THREE PREVIOUSLY SORTED ARRAYS ****" << endl;
cout << "\t --------------------------------------------------------------------------" << endl;
MergeSort(ArraySort_1, ArraySort_2, ArraySort_3);
for (int i = 0; i < 99; i++)
{
cout << ArraySort_3[i] << " ";
}
cout << endl << endl << endl << endl << "\t";
system ("pause");
return;
}
////////////////////// FREEE FUNCTIONS //////////////////////
void BubbleSort(int ArraySort_1[ ])
{
int tmp;
for (int i = 0; i < 49; i++)
{
for (int x = 0; x < 49; x++)
{
if (ArraySort_1[x] > ArraySort_1[x + 1])
{
tmp = ArraySort_1[x];
ArraySort_1[x] = ArraySort_1[x + 1];
ArraySort_1[x + 1] = tmp;
}
}
}
}
void ShellSort(int ArraySort_2[ ])
{
int h, i, j, k, hCnt;
int increments[20];
for (h = 1, i = 0; h < 49; i++)
{
increments[i] = h;
h = 3 * h + 1;
}
for (i--; i >= 0; i--)
{
h = increments[i];
for (hCnt = h; hCnt < 2 * h; hCnt++)
{
for (j = hCnt; j < 49;)
{
int tmp = ArraySort_2[j];
k = j;
while (k - h >= 0 && tmp < ArraySort_2[k - h])
{
ArraySort_2[k] = ArraySort_2[k - h];
k -= h;
}
ArraySort_2[k] = tmp;
j += h;
}
}
}
}
void SelectionSort(int ArraySort_3[ ])
{
int tmp, min;
for (int i = 0; i < 50; i++)
{
min = i;
for (int x = i; x < 50; x++)
{
if (ArraySort_3[x] < ArraySort_3[min])
{
min = x;
}
}
tmp = ArraySort_3[i];
ArraySort_3[i] = ArraySort_3[min];
ArraySort_3[min] = tmp;
}
}
void MergeSort(int ArraySort_1[ ],int ArraySort_2[ ], int ArraySort_3[ ])
{
int Tmp1[100];
int i = 0;
while ((ArraySort_1[i] < 50) && (ArraySort_2[i] < 50))
{
if (ArraySort_1[i] < ArraySort_2[i])
{
Tmp1[i] = ArraySort_1[i];
ArraySort_1[i]++;
cout << ArraySort_1 << " ";
}
else
{
Tmp1[i] = ArraySort_2[i];
ArraySort_2[i]++;
cout << ArraySort_2 << " ";
}
Tmp1[i]++;
}
while (Tmp1[i] < 100)
{
Tmp1[i] = ArraySort_1[i];
ArraySort_1[i]++;
ArraySort_3[i]++;
}
while (ArraySort_2[i] < 100)
{
Tmp1[i] = ArraySort_2[i];
ArraySort_2[i]++;
ArraySort_3[i]++;
}
}