Need help with a program, it must be in C++. Also, need to create arrayListType.
ID: 3688867 • Letter: N
Question
Need help with a program, it must be in C++. Also, need to create arrayListType.h
Declare an array (using arrayListType.h) hold from 60 to 70 integers.
Load the array with data from lab8.dat
Sort the array using the Selection sort (which is defined in arrayListType.h) and counting the number of loop iterations and the number of record moves.
Print the sorted array, the number of loop iterations and the number of record moves.
Do the same thing with the Insertion and Quick sorts.
This program uses the data file lab8.dat. Create the output file, YourIslandIDlab8.out, for all of your output.
Note, you must start with the original unsorted array each time.
///////////////////////////////////////////////////////////////////////////////////
Lab8.dat
735
341
646
229
842
620
741
222
165
182
943
150
250
350
228
344
828
110
987
777
191
545
878
900
351
291
854
404
607
305
199
395
809
504
841
149
492
613
386
929
481
853
729
205
482
774
338
194
743
108
Explanation / Answer
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
void InitialArray();
void InsertionSort();
void BubbleSort();
void selectionSort();
int ArraySize;
int* Array;
void InitialArray(int NextSize)
{
ArraySize=NextSize;
int *Arrayx = new int[NextSize];
Array=Arrayx; //reference this local variable to use it as Global
srand(5);
for(int Fill=0 ; Fill<ArraySize; Fill++)
{
Array[Fill]= rand();
}
}
int main()
{
for(int NewSize=1000; NewSize<=10000; NewSize=NewSize+1000)
{
InitialArray(NewSize); //Creat Array With New Size & Initialize It.
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
//Calculating Running Time -InsertionSort
clock_t start = clock(), diff;
InsertionSort(); //calling Algorithm
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time Taken %d Seconds & %d MilliSeconds By Insertion Sort Algorithm For %d Elements ", msec/1000, msec%1000,ArraySize);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
//Calculating Running Time -BubbleSort
start = clock(), diff;
BubbleSort(); //calling Algorithm
diff = clock() - start;
msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time Taken %d Seconds & %d MilliSeconds By Bubble Sort Algorithm For %d Elements ", msec/1000, msec%1000,ArraySize);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
//Calculating Running Time -selectionSort
start = clock(), diff;
selectionSort(); //calling Algorithm
diff = clock() - start;
msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time Taken %d Seconds & %d MilliSeconds By Selection Sort Algorithm For %d Elements ", msec/1000, msec%1000,ArraySize);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
system("pause");
}
void InsertionSort()
{
for (int j=1; j<ArraySize; j++)
{
int key=Array[j];
int i=j-1;
while (i >=0 && Array[i] > key)
{
Array[i+1]=Array[i];
i--;
}
Array[i+1]=key;
}
}
void BubbleSort()
{
int temp;
bool swap;
do
{
swap = false;
for (int count = 0; count < ArraySize; count++)
{
if (Array[count] > Array[count + 1])
{
temp = Array[count];
Array[count] = Array[count + 1];
Array[count + 1] = temp;
swap = true;
}//end if
}//end for
} //end do-while
while (swap);
}
void selectionSort()
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (ArraySize - 1); startScan++)
{ minIndex = startScan;
minValue = Array[startScan];
for(int index = startScan + 1; index < ArraySize; index++)
{ if (Array[index] < minValue)
{
minValue = Array[index];
minIndex = index;
}
}
Array[minIndex] = Array[startScan];
Array[startScan] = minValue;
}
}