3 sorts of random arrays

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.

#include <iostream>
#include <time.h>
#include<stdlib.h>
#include "searchSortAlgorithms.h"


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);

cout << "******Bubble Sort******" <<endl;

// call bubbleSort
bubbleSort( list1,comparisons,itemAssignments);

// display results
cout << endl;
cout <<"Number of Comparisons are: " << comparisons << endl;
cout <<" Number of Assignments are: " << itemAssignments <<endl;
cout << endl;
comparisons = 0;
itemAssignments = 0;

cout << "******Selection Sort******" <<endl;
//call selectionSort
selectionSort(list2,comparisons,itemAssignments);
cout << endl;
cout <<"Number of Comparisons are: " << comparisons << endl;
cout <<" Number of Assignments are: " << itemAssignments <<endl;
cout <<endl;

comparisons = 0;
itemAssignments = 0;

cout <<"******Insertion Sort******" <<endl;
cout <<endl;

//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;

do
{
num[location] = num[location-1];
itemAssignments++;
location--;
comparisons++;
} while(location > 0 && num[location -1] > temp);
num[location] = temp;
itemAssignments++;
}
}
} //end insertion sort
int list1[MAX], int list2[MAX],int list3[MAX]; int list1[MAX], list2[MAX], list3[MAX];

1
2
void selectionSort( int num[], int &comparisons, int itemAssignments)
{

1
2
void selectionSort( int num[], int &comparisons, int &itemAssignments)
{


I can't understand your bubble sort,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void bubbleSort( int num[], int &comparisons, int &itemAssignments)
{
	//int index; ????
	/*for (int iteration = 0; iteration < MAX-1; /*index++???*/ iteration++)
	{*/
	bool isSort = false;
	while( !isSort ){
		isSort = true;
		for (int index = 0; index < MAX-1;index++)
		{
			//increment comparisons by one
			comparisons++;
			if (num[index] > num[index + 1])
			{
				isSort = false;
				int temp = num[index];
				num[index] = num[index + 1];
				num[index + 1] = temp;
				//increment itemAssignments value by one // 3
				itemAssignments += 3;
			}
		}
	}
	//}
} //end of bubbleSort method 


Didn't check for other sorting, but for me changed these make me able to run and get result.
Last edited on
Topic archived. No new replies allowed.