Working on an assignment for class. I was able to get the sorts created buy for some reason im having a problem with reducing redundant code within my int main() function
/*
1.) create 10, 100, 1000, and 10000 randomly generated elements
2.) Perform The Following Sorts on the same randomly generated data and compare the efficiency by comparing their number of comparisons made.
"Bubble Sort",
"Merge Sort",
"Insertion Sort",
"Combination Sort"
3.) Output must look like this:
SORTING ALGORITHM: Bubble Sort
Number of values in array: 10
Number of comparisons required: xx
Number of values in array: 100
Number of comparisons required: xxxx
Number of values in array: 1000
Number of comparisons required: xxxxxx
Number of values in array: 10000
Number of comparisons required: xxxxxxx
SORTING ALGORITHM: Merge Sort
......
.....
*/
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>
#include "SortFunctions.h"
unsigned int uiRandomBuffer1[10] = { 0 };
unsigned int uiRandomBuffer2[100] = { 0 };
unsigned int uiRandomBuffer3[1000] = { 0 };
unsigned int uiRandomBuffer4[10000] = { 0 };
char *NameOfSort[4] = { "Bubble Sort","Insertion Sort","Combination Sort" };
void GenerateRandomNumbers( size_t uiNumElements, unsigned int *puiAddressOfStorage )
{
unsigned int uiBuffer;
srand( time( NULL ) );
for( unsigned int i = 0; i < uiNumElements; i++)
{
*(puiAddressOfStorage+i) = (rand() % uiNumElements);// +1?
Sleep(1);
}
}
void DisplaySortingInfo( unsigned int uiNumOfElements, unsigned int uiNumOfComparisons )
{
printf( "\n\nNumber of values in array: %u", uiNumOfElements );
printf( "\nNumber of comparisons required: %u", uiNumOfComparisons );
}
void FillArrayWithRand( unsigned int uiNumElementsRand, unsigned int *uiRandomBuffer, unsigned int uiNumElementsTemp, unsigned int *uiTempBuffer )
{
for ( unsigned int i = 0; i < uiNumElementsRand;i++ )
{
uiTempBuffer[i] = uiRandomBuffer[i];
}
}
int main()
{
// Temp variable to hold comparison return value of each sorting function
unsigned int uiTempComparisons;
// Generate random numbers for arrays of size 10, 100, 1000, and 10000
GenerateRandomNumbers( 10, uiRandomBuffer1 );
GenerateRandomNumbers( 100, uiRandomBuffer2 );
GenerateRandomNumbers( 1000, uiRandomBuffer3 );
GenerateRandomNumbers( 10000, uiRandomBuffer4 );
// TempBuffers for sorting the array. These buffers will be reused by each sorting function
unsigned int uiTempBuffer1[10];
unsigned int uiTempBuffer2[100];
unsigned int uiTempBuffer3[1000];
unsigned int uiTempBuffer4[10000];
// Bubble Sort
printf( "\nSORTING ALGORITHM: %s", NameOfSort[0] );
// Fill TempBuffer with values of random array
FillArrayWithRand( 10, uiRandomBuffer1, 10, uiTempBuffer1 );
FillArrayWithRand( 100, uiRandomBuffer2, 100, uiTempBuffer2 );
FillArrayWithRand( 1000, uiRandomBuffer3, 1000, uiTempBuffer3 );
FillArrayWithRand( 10000, uiRandomBuffer4, 10000, uiTempBuffer4 );
// Perform Sort of 10,100,1000,and 10000 elements
uiTempComparisons = BubbleSort( 10, uiTempBuffer1);
DisplaySortingInfo( 10, uiTempComparisons );
uiTempComparisons = BubbleSort( 100, uiTempBuffer2);
DisplaySortingInfo( 100, uiTempComparisons );
uiTempComparisons = BubbleSort( 1000, uiTempBuffer3);
DisplaySortingInfo( 1000, uiTempComparisons );
uiTempComparisons = BubbleSort( 10000, uiTempBuffer4);
DisplaySortingInfo( 10000, uiTempComparisons );
// Insertion Sort
printf( "\nSORTING ALGORITHM: %s", NameOfSort[1] );
// Fill TempBuffer with values of random array
FillArrayWithRand( 10, uiRandomBuffer1, 10, uiTempBuffer1 );
FillArrayWithRand( 100, uiRandomBuffer2, 100, uiTempBuffer2 );
FillArrayWithRand( 1000, uiRandomBuffer3, 1000, uiTempBuffer3 );
FillArrayWithRand( 10000, uiRandomBuffer4, 10000, uiTempBuffer4 );
uiTempComparisons = InsertionSort( 10, uiTempBuffer1);
DisplaySortingInfo( 10, uiTempComparisons );
uiTempComparisons = InsertionSort( 100, uiTempBuffer2);
DisplaySortingInfo( 100, uiTempComparisons );
uiTempComparisons = InsertionSort( 1000, uiTempBuffer3);
DisplaySortingInfo( 1000, uiTempComparisons );
uiTempComparisons = InsertionSort( 10000, uiTempBuffer4);
DisplaySortingInfo( 10000, uiTempComparisons );
getchar();
// Combination Sort
printf( "\nSORTING ALGORITHM: %s", NameOfSort[2] );
// Fill TempBuffer with values of random array
FillArrayWithRand( 10, uiRandomBuffer1, 10, uiTempBuffer1 );
FillArrayWithRand( 100, uiRandomBuffer2, 100, uiTempBuffer2 );
FillArrayWithRand( 1000, uiRandomBuffer3, 1000, uiTempBuffer3 );
FillArrayWithRand( 10000, uiRandomBuffer4, 10000, uiTempBuffer4 );
// Perform Sort of 10,100,1000,and 10000 elements
uiTempComparisons = CombinationSort( 10, uiTempBuffer1);
DisplaySortingInfo( 10, uiTempComparisons );
uiTempComparisons = CombinationSort( 100, uiTempBuffer2);
DisplaySortingInfo( 100, uiTempComparisons );
uiTempComparisons = CombinationSort( 1000, uiTempBuffer3);
DisplaySortingInfo( 1000, uiTempComparisons );
uiTempComparisons = CombinationSort( 10000, uiTempBuffer4);
DisplaySortingInfo( 10000, uiTempComparisons );
return 0;
}