Write your question here.
Can someone explain to me what is wrong with this program. It is supposed to sort a random array of 5000 elements by three different sorting methods. Any help would be appreciated.
Thanks in advance
[code]
Put the code you need help with here.
// This program creates three identical arrays, list1, list2, list3
// of 5000 elements each. Then uses bubble-sort, selection-sort, & insertion sort
// and outputs the number of comparisons and assignments.
using namespace std;
// declaring function prototypes:bubble-sort, selection-sort, & insertion-sort
void bubbleSort(int[], int &, int &);
void selectionSort(int[], int &, int &);
void insertionSort(int[], int &, int &);
void generateRandomArray(int[],int[], int[]);
// global constant value to create three arrays of 5000 elements
const int MAX = 5000;
// main program
int main()
{
// declare an integer variable to zero
int comparisons = 0;
int itemAssignments = 0;
int list1[MAX], int list2[MAX],int list3[MAX];
// call to generateRandomArray
generateRandomArray(list1, list2, list3);
//call insertionSort
insertionSort(list3,comparisons,itemAssignments);
cout <<"Number of Comparisons are: " << comparisons << endl;
cout <<" Number of Assignments are: " << itemAssignments <<endl;
cout <<endl;
//pause the program output
system("pause");
return 0;
} //end of main
// generates random arrays with numbers between 1 to 500
void generateRandomArray(int arr1[],int arr2[],int arr3[])
{
srand(time_t(0));
for (int i =0;i<MAX;i++)
arr1[i]= arr2[i]= arr3[i]= rand()%500+1;
} // end generateRandomArray
//bubbleSort
void bubbleSort( int num[], int &comparisons, int &itemAssignments)
{
int index;
for (int iteration = 1; iteration < MAX; index++)
{
for (int index = 0; index < MAX-iteration;index++)
{
//increment comparisons by one
comparisons++;
if (num[index] > num[index + 1])
{
int temp = num[index];
num[index] = num[index + 1];
num[index + 1] = temp;
//increment itemAssignments value by one
itemAssignments++;
}
}
}
} //end of bubbleSort method
// selectionSort
void selectionSort( int num[], int &comparisons, int itemAssignments)
{
//local variables
int index;
int smallestIndex;
int location;
int temp;
for (index = 0; index < MAX-1;index++)
{
smallestIndex = index;
for (location = index +1; location < MAX;location++)
{
//increment comparisons by one
comparisons++;
if (num[location] < num[smallestIndex])
{
smallestIndex = location;
}
} //end of location loop
temp = num[smallestIndex];
num[smallestIndex] = num[index];
//increment assignments by three
itemAssignments = itemAssignments + 3;
num[index] = temp;
} //end of index for loop
} //end of selection sort
//insertionSort method
void insertionSort( int num[], int &comparisons, int &itemAssignments)
{
//local declarations of variables
int firstOutOfOrder,location;
int temp;
for (firstOutOfOrder = 1; firstOutOfOrder < MAX; firstOutOfOrder++)
{
//increment comparisons value by one
comparisons++;
if (num[firstOutOfOrder] < num[firstOutOfOrder-1])
{
comparisons++;
temp = num[firstOutOfOrder];
location = firstOutOfOrder;
//increment assignment value by two
itemAssignments = itemAssignments+2;