This is a code which sorts arrays.

I use clock_t to determine the processing time for my code. However, it always stacks with the time taken from before. It looks like it's just adding the results.

int main(int argc, char *argv[])
{unsigned long n;//size of array -- long for big numbers of n
int choice, choice2, sort;//numbers corresponding to the choices(switch)
clock_t Begin, End;
float elapTicks, elapMilli;
wrongchoice://goto function -- bringing back to this point after entering wrong choice
cout<<"===============================…
cout<<"\t\t\tChoose the order of the output:\n\t\t\t\t[1] Random-Descending\n\t\t\t\t[2] Ascending-Descending\n\t\t\t\t[3] Descending-Ascending\n\t\t\t\t[4] Random-Ascending\n\t\t\tChoice: ";
cin>>choice;
cout<<"\t\t\tIndicate size of array: ";
cin>>n;
long int array[n];//first and original array to be used all through the program
long int arrayb[n];//second/temporary array used to preserve/copy original values of first array
for (int i=0; i<n; i++)
{array[i]=rand()%99999;
arrayb[i]=array[i];//copy value of the first array into the second array
}
double temptime=0;
clock_t realend, realend2, realend3, realend4, realend5, realend6;
sorting:
cout<<"\n\t\t\t\t[1] Bubble Sort\n\t\t\t\t[2] Selection Sort\n\t\t\t\t[3] Insertion Sort\n\t\t\t\t[4] Quick Sort\n\t\t\t\t[5] Shell Sort\n\t\t\t\t[6] Merge Sort\n";
cout<<"\t\t\tChoose what sorting algorithm to use: ";
cin>>sort;
goto copyarray;//goto function to copy array elements(located at line 747)
sorting2://goto function -- used to resume back to this point after copying array elements
srand(time(NULL));
switch(choice)//first switch -- indicates the desired order of the output
{
case 1://Random-Descending
{cout<<"==============================…
cout<<"\t\t\tUnsorted Array: "<<endl;
for(int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\n\t\t\tSorted Array:\n";
switch(sort)//second swtich -- indicates what sorting algorithm to use
{
case 1:
{//Bubble Sort Descending
clock_t Begin1, End1;
realend=clock();
Begin1=clock();//indicates start of timer/clock counter(every start of cases for sorting)
BubbleSort2(array, n);//calling of function
End1=clock();//indicates end of timer/clock
float time1;
time1= (End1-Begin1/1000.00f);//used to convert value(taken from the clock code) to milliseconds(from seconds)
cout<<"Bubble Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time1<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 2:
{//Selection Sort Descending
clock_t Begin2, End2;
realend2=clock();
Begin2=clock();
double temptime2=Begin2;
SelectionSort2(array, n);//calling of function
End2=clock();
float time2;
time2 = (End2-Begin2/1000.00f);
cout<<"Selection Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time2<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}

That is some part of my code. I couldn't put it all here since it's too long. The time adds up with the time that is first taken when I choose another option from the switch code, even though I declared a new variable for clock_t. Please help T_T I really don't know what's wrong with the code.

Here's the entire code:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <time.h>

////////////////////////////////////////
//Submitted by://///////////////////////
//Leng Ann Christi T. Balberan//////////
//Riyah Mae S. Dellosa//////////////////
//Submitted to://///////////////////////
//Engr. Dwight Sabio////////////////////
////////////////////////////////////////
//Data Strctures and Algorithm Project//
//Six Sorting Algorithms////////////////
////////////////////////////////////////

using namespace std;
unsigned long opcount;//Operational count counter

void BubbleSort(long int array[], int n)//Bubble Sort Ascending
{bool sorted=false;
opcount++;
while(!sorted)
{sorted=true;
opcount++;
for (int i=n-1; i>0; i--)
{for (int j=0; j<i; j++)
{if (array[j]>array[j+1])
{int temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
sorted=false;
opcount+=5;}
opcount+=3;}
opcount+=3;}}
opcount+=2;
}

void BubbleSort2(long int array[], int n)//Bubble Sort Descending
{bool sorted=false;
opcount++;
while(!sorted)
{sorted=true;
opcount++;
for (int i=n-1; i>0; i--)
{for (int j=0; j<i; j++)
{if (array[j]<array[j+1])
{int temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
sorted=false;
opcount+=5;}
opcount+=3;}
opcount+=3;}}
opcount+=2;
} 

void SelectionSort(long int array[], int n)//Selection Sort Ascending
{int min;
for (int i=0; i<n; i++)
{min=i;
opcount++;
for (int j=i+1; j<n; j++)
{if (array[j]<array[min])
{min=j;
opcount+=2;}
else
opcount++;}
int temp=array[min];
array[min]=array[i];
array[i]=temp;
opcount+=6;}
opcount++;
}

void SelectionSort2(long int array[], int n)//Selection Sort Descending
{int max;
for (int i=0; i<n; i++)
{max=i;
opcount++;
for (int j=i+1; j<n; j++)
{if (array[j]>array[max])
{max=j;
opcount+=2;}
else
opcount++;}
int temp=array[max];
array[max]=array[i];
array[i]=temp;
opcount+=6;}
opcount++;
}

void InsertionSort(long int array[], long int n)//Insertion Sort Ascending
{int j, B;
for (long int i=1; i<n; i++)
{j=i;
B=array[i];
opcount+=2;
while (j>0&&array[j-1]>B)
{array[j]=array[j-1];
j--;
opcount+=3;}
array[j]=B;
opcount+=4;}
opcount++;
}

void InsertionSort2(long int array[], long int n)//Insertion Sort Descending
{int j, B;
for (long int i=1; i<n; i++)
{j=i;
B=array[i];
opcount+=2;
while (j>0&&array[j-1]<B)
{array[j]=array[j-1];
j--;
opcount+=3;}
array[j]=B;
opcount+=4;}
opcount++;
}  

void ShellSort(long int array[],int n)//Shell Sort Ascending
{int mid;
for(int m=n/2; m>0; m/=2)
{for(int j=m; j<n; j++)
{for(int i=j-m;i>=0;i-=m)
{if(array[i+m]>=array[i])
{opcount++;
break;}
else
{mid=array[i];
array[i]=array[i+m];
array[i+m]=mid;
opcount+=3;}
opcount+=4;}
opcount+=3;}
opcount+=3;}
opcount+=3;}

void ShellSort2(long int array[],int n)//Shell Sort Descending
{int mid;
for(int m=n/2; m>0; m/=2)
{for(int j=m; j<n; j++)
{for(int i=j-m;i>=0;i-=m)
{if(array[i+m]<=array[i])
{opcount++;
break;}
else
{mid=array[i+m];
array[i+m]=array[i];
array[i]=mid;
opcount+=3;}
opcount+=4;}
opcount+=3;}
opcount+=3;}
opcount+=3;}

void QuickSort(long int array[], int low, int high)//Quick Sort Ascending
{int i=low, j=high;
int temp;
int pivot=array[(low+high)/2];
opcount+=3;
while (i<=j)
{while (array[i]<pivot)
{i++;
opcount+=2;}
while (array[j]>pivot)
{j--;
opcount+=2;}
if (i<=j){
temp=array[i];
array[i]=array[j];
array[j]=temp;
i++;
j--;
opcount+=8;}
};
if (low<j)
{QuickSort(array, low, j);
opcount++;}
if (i<high)
{QuickSort(array, i, high);
opcount++;}
}

void QuickSort2(long int array[], int low, int high)//Quick Sort Descending
{int i=low, j=high;
int temp;
int pivot=array[(low+high)/2];
opcount+=3;
while (i<=j)
{while (array[i]>pivot)
{i++;
opcount+=2;}
while (array[j]<pivot)
{j--;
opcount+=2;}
if (i<=j){
temp=array[i];
array[i]=array[j];
array[j]=temp;
i++;
j--;
opcount+=8;}
};
if (low<j)
{QuickSort2(array, low, j);
opcount++;}
if (i<high)
{QuickSort2(array, i, high);
opcount++;}
}

void Merge(long int array[], long int tempArray[], int left, int center, int right)//Merge Sort Ascending
{
int i, leftEnd, numElements, tempPos;
leftEnd = center - 1;
tempPos = left;
numElements = right - left + 1;
opcount+=6;
while ((left <= leftEnd) && (center <= right))
{
if (array[left] <= array[center])
{
tempArray[tempPos] = array[left];
tempPos++;
left++;
opcount+=6;
}
else
{
tempArray[tempPos] = array[center];
tempPos++;
center++;
opcount+=5;
}
}
opcount++;
while (left <= leftEnd)
{
tempArray[tempPos] = array[left];
left++;
tempPos++;
opcount+=5;
}
opcount++;
while (center <= right)
{
tempArray[tempPos] = array[center];
center++;
tempPos++;
opcount+=5;
}
opcount++;
for (i = 0; i <= numElements; i++)
{
array[right] = tempArray[right];
right--;
opcount+=6;
}
opcount++;
}

void Merge2(long int array[], long int tempArray[], int left, int center, int right)//Merge Sort Descending
{
int i, leftEnd, numElements, tempPos;
leftEnd = center - 1;
tempPos = left;
numElements = right - left + 1;
opcount+=6;
while ((left <= leftEnd) && (center <= right))
{
if (array[left] > array[center])
{
tempArray[tempPos] = array[left];
tempPos++;
left++;
opcount+=6;
}
else
{
tempArray[tempPos] = array[center];
tempPos++;
center++;
opcount+=5;
}
}
while (left <= leftEnd)
{
tempArray[tempPos] = array[left];
left++;
tempPos++;
opcount+=5;
}
opcount++;
while (center <= right)
{
tempArray[tempPos] = array[center];
center++;
tempPos++;
opcount+=5;
}
opcount++;
for (i = 0; i <= numElements; i++)
{
array[right] = tempArray[right];
right--;
opcount+=6;
}
opcount++;
}

void MSort(long int array[], long int tempArray[], int left, int right, int choice)//Merge Sort Second Call
{
int center;
if (right > left)
{
center = (right + left) / 2;
MSort(array, tempArray, left, center, choice);
MSort(array, tempArray, (center + 1), right, choice);
opcount+=6;
if (choice>2)
{Merge(array, tempArray, left, (center + 1), right);
opcount+=2;}
else
{Merge2(array, tempArray, left, (center + 1), right);
opcount+=2;}
}
}

void MergeSort(long int array[], int n, int choice)//Merge Sort First Call
{
long int tempArray[n];
MSort(array, tempArray, 0, n-1, choice);
}

//End of functions

int main(int argc, char *argv[])
{unsigned long n;//size of array -- long for big numbers of n
int choice, choice2, sort;//numbers corresponding to the choices(switch)
clock_t Begin, End;
float elapTicks, elapMilli;
wrongchoice://goto function -- bringing back to this point after entering wrong choice
cout<<"===============================================================================\n";
cout<<"\t\t\tChoose the order of the output:\n\t\t\t\t[1] Random-Descending\n\t\t\t\t[2] Ascending-Descending\n\t\t\t\t[3] Descending-Ascending\n\t\t\t\t[4] Random-Ascending\n\t\t\tChoice: ";
cin>>choice;
cout<<"\t\t\tIndicate size of array: ";
cin>>n;
long int array[n];//first and original array to be used all through the program
long int arrayb[n];//second/temporary array used to preserve/copy original values of first array
for (int i=0; i<n; i++)
{array[i]=rand()%99999;
arrayb[i]=array[i];//copy value of the first array into the second array
}
double temptime=0;
clock_t realend, realend2, realend3, realend4, realend5, realend6;
sorting:
cout<<"\n\t\t\t\t[1] Bubble Sort\n\t\t\t\t[2] Selection Sort\n\t\t\t\t[3] Insertion Sort\n\t\t\t\t[4] Quick Sort\n\t\t\t\t[5] Shell Sort\n\t\t\t\t[6] Merge Sort\n";
cout<<"\t\t\tChoose what sorting algorithm to use: ";
cin>>sort;
goto copyarray;//goto function to copy array elements(located at line 747)
sorting2://goto function -- used to resume back to this point after copying array elements
srand(time(NULL));
switch(choice)//first switch -- indicates the desired order of the output
{
case 1://Random-Descending
{cout<<"===============================================================================\n";
cout<<"\t\t\tUnsorted Array: "<<endl;
for(int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\n\t\t\tSorted Array:\n";
switch(sort)//second swtich -- indicates what sorting algorithm to use
{
case 1:
{//Bubble Sort Descending
clock_t Begin1, End1;
realend=clock();
Begin1=clock();//indicates start of timer/clock counter(every start of cases for sorting)
BubbleSort2(array, n);//calling of function
End1=clock();//indicates end of timer/clock
float time1;
time1= (End1-Begin1/1000.00f);//used to convert value(taken from the clock code) to milliseconds(from seconds)
cout<<"Bubble Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time1<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 2:
{//Selection Sort Descending
clock_t Begin2, End2;
realend2=clock();
Begin2=clock();
double temptime2=Begin2;
SelectionSort2(array, n);//calling of function
End2=clock();
float time2;
time2 = (End2-Begin2/1000.00f);
cout<<"Selection Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time2<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 3:
{//Insertion Sort Descending
clock_t Begin3, End3;
realend3=clock();
Begin3=clock();
InsertionSort2(array, n);//calling of function
End3=clock();
float time3;
time3 = (End3-Begin3/1000.00f);
cout<<"Insertion Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time3<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 4:
{//Quick Sort Descending
clock_t Begin4, End4;
realend4=clock();
Begin4=clock();
QuickSort2(array, 0, (n-1));//calling of function
End4=clock();
float time4;
time4 = (End4-Begin4/1000.00f);
cout<<"Quick Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time4<<" milliseconds.";
cout<<realend4;
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 5:
{//Shell Sort Descending
clock_t Begin5, End5;
realend5=clock();
Begin5=clock();
ShellSort2(array, n);//calling of function
End5=clock();
float time5;
time5 = (End5-Begin5/1000.00f);
cout<<"Shell Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time5<<" milliseconds.";
cout<<realend5;
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 6:
{//Merge Sort Descending
clock_t Begin6, End6;
realend6=clock();
Begin6=clock();
MergeSort(array, n, choice);//calling of function
End6=clock();
float time6;
time6 = (End6-Begin6/1000.00f);
cout<<"Merge Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<time6<<" milliseconds.";
cout<<realend6;
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
default:
{cout<<"----- Cannot displayed sorted array -----\nInvalid input.\nTry again."<<endl;
goto sorting;//goto function -- resume back to line 348 after indicating a wrong value of sort
break;}}
break;}
case 2://Ascending-Descending
{cout<<"===============================================================================\n";
cout<<"Ascending Array:\n";
ShellSort(array, n);//Used to sort unsorted array into ascending order
opcount=0;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<endl<<endl;
cout<<"\n\nSorted Descending Array:\n";
switch(sort)
{
case 1:
{//Bubble Sort Descending
Begin=clock()*CLK_TCK;
BubbleSort2(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Bubble Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 2:
{//Selection Sort Descending
Begin=clock()*CLK_TCK;
SelectionSort2(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Selection Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 3:
{//Insertion Sort Descending
Begin=clock()*CLK_TCK;
InsertionSort2(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Insertion Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 4:
{//Quick Sort Descending
Begin=clock()*CLK_TCK;
QuickSort2(array, 0, (n-1));//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Quick Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 5:
{//Shell Sort Descending
Begin=clock()*CLK_TCK;
ShellSort2(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Shell Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 6:
{//Merge Sort Descending
Begin=clock()*CLK_TCK;
MergeSort(array, n, choice);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Merge Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
default:
{cout<<"----- Cannot displayed sorted array -----\nInvalid input.\nTry again."<<endl;
goto sorting;//goto function -- resume back to line 348 after indicating a wrong value of sort
break;}}
break;}
case 3:
{cout<<"===============================================================================\n";
cout<<"Descending Array:\n";//Descending-Ascending
ShellSort2(array, n);//Used to sort unsorted array into descending order
opcount=0;//opcount restored back to zero after sorting
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<endl<<endl;
cout<<"\n\nSorted Ascending Array:\n";
switch(sort)
{
case 1:
{//Bubble Sort Ascending
Begin=clock()*CLK_TCK;
BubbleSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Bubble Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 2:
{//Selection Sort Ascending
Begin=clock()*CLK_TCK;
SelectionSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Selection Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 3:
{//Insertion Sort Ascending
Begin=clock()*CLK_TCK;
InsertionSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Insertion Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 4:
{//Quick Sort Ascending
Begin=clock()*CLK_TCK;
QuickSort(array, 0, (n-1));//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Quick Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 5:
{//Shell Sort Ascending
Begin=clock()*CLK_TCK;
ShellSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Shell Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 6:
{//Merge Sort Ascending
Begin=clock()*CLK_TCK;
MergeSort(array, n, choice);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Merge Sort: "<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
default:
{cout<<"----- Cannot displayed sorted array -----\nInvalid input.\nTry again."<<endl;
goto sorting;//goto function -- resume back to line 348 after indicating a wrong value of sort
break;}}
break;
}

case 4:
{cout<<"===============================================================================\n";
cout<<"Unsorted Array: "<<endl;//Random-Ascending
for(int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nSorted Array:\n";
switch(sort)
{
case 1:
{//Bubble Sort Ascending
Begin=clock()*CLK_TCK;
BubbleSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Bubble Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 2:
{//Selection Sort Ascending
Begin=clock()*CLK_TCK;
SelectionSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Selection Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 3:
{//Insertion Sort Ascending
Begin=clock()*CLK_TCK;
InsertionSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Insertion Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 4:
{//Quick Sort Ascending
Begin=clock()*CLK_TCK;
QuickSort(array, 0, (n-1));//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Quick Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 5:
{//Shell Sort Ascending
Begin=clock()*CLK_TCK;
ShellSort(array, n);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Shell Sort:"<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
case 6:
{//Merge Sort Ascending
Begin=clock()*CLK_TCK;
MergeSort(array, n, choice);//calling of function
End=clock()*CLK_TCK;
elapTicks = End - Begin;
elapMilli = elapTicks/1000;
cout<<"Merge Sort: "<<endl;
for (int i=0; i<n; i++)
cout<<array[i]<<"\t";
cout<<"\n\nTotal operational count is: "<<opcount<<endl;
cout<<"\nAlgorithm Runtime is: "<<elapMilli<<" milliseconds.";
cout<<endl<<endl;
goto choose;//goto function to indicate what to do after sorting
break;}
default:
{cout<<"----- Cannot displayed sorted array -----\nInvalid input.\nTry again."<<endl;
goto sorting;//goto function -- resume back to line 348 after indicating a wrong value of sort
break;}}
break;}
default:
{cout<<"\nYou entered a wrong value along the program. Invalid. Please try Again.\n";
goto wrongchoice;//goto function -- resuming back to line 337 after indicating a wrong value of choice
break;}}

copyarray://goto function for copying array elements
for (int i=0; i<n; i++)
array[i]=arrayb[i];
goto sorting2;//goto function to resume back to line 330(right after copying array elements)

choose://goto function for every end of sort(to indicate what to do next) and to restore opcount back to zero
cout<<"[1] Choose again.\n[2] Exit.\nChoice: ";
cin>>choice2;
switch(choice2)
{case 1:
{system("cls");
opcount=0;
goto sorting;
break;}
case 2:
{cout<<"\n\nThank you for using the program.\n\n"<<endl;
break;}
default:
{cout<<"Invalid input."<<endl;
goto choose;
break;}}


    system("PAUSE");
    return EXIT_SUCCESS;
}

Feel free to paste it in any c++ debugger so you would understand the code better.

I hope someone can help me.

Edited 4 Years Ago by minxminx: n/a

That is a lot of code! I have not read over it, but I would suggest that this should work:

int main()
{
    time_t start=time(NULL);//get start time
    //TONS OF TIME CONSUMING CODE!!!
    time_t end=time(NULL);//get end time
    time_t deltatime=start-end;//this now stores how long the code took!
    //output?
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.