0

```
#include "Sorts.h"
Sorts::Sorts(void)
{
}
Sorts::~Sorts(void)
{
}
void Sorts::insert()
{ ofstream myfile;
myfile.open ("InsertSorted.txt");
starttime=(long)time(NULL);
int A[ELEMENTS]={5,2,4,6,1,3};
insertsort(A,ELEMENTS);
cout<<endl<<"Sorted list "<<endl;
for(x=0;x<ELEMENTS;x++)
{
cout<<A[x];
}
endtime=(long)time(NULL);
totalseconds=endtime-starttime;
minutes=totalseconds/60;
seconds=totalseconds%60;
cout<<"\nStart time: "<<starttime;
cout<<"\nEnd Time: "<<endtime;
cout<<"\nElapsed Time: "<<minutes<<" min, "<<seconds<<" seconds"<<endl;"\n\n";
}
void Sorts::insertsort(int A[],int length)
{
for(int j=1;j<length;j++)
{
key=A[j];
i=j-1;
while(i>=0 && A[i]>key)
{
A[i+1]=A[i];
i--;
}
A[i+1]=key;
}
}
void Sorts::adjust (int list [], int i , int n)
{
for(x=0;x<ELEMENTS; x++)
{
cout<<list[x]<<",";
}
cout<<endl;
int lchild, rchild, larger, temp;
larger = lchild = 2 * i;
if ( lchild <= n)
{
// root not a leaf
rchild = lchild + 1;//2*i+1
// if root has rchild, find larger child
if ((rchild <= n) && (list[rchild] > list[lchild]))
{
larger = rchild;
}
// if root value smaller than larger child, swap
if (list[larger] > list[i])
{
temp = list[i];// swap the root and larger child nodes
list[i] = list[larger];
list[larger] = temp;
//larger=temp;
// transform new subtree into heap
adjust(list,larger, n);
} // then swap
} // then not a leaf
// if root is a leaf, do nothing
} // end Adjust()*/
void Sorts:: SWAP(int list,int n)
{
int temp=list;
list=n;
n=temp;
}
void Sorts::heapsort (int list [], int n)
{
// Perform a heapsort on the array
int i;
for (i = n/2; i>=0; i--) // build heap
adjust(list, i , n);
for (i = n-1; i>=0; i--) // sort the heap
{
/*temp = list[i+1];
list[i+1] = list[0];
list[0] = temp;temp = list[1];
list[1] = list[i+1];
list[i+1] = temp;*/
SWAP(list[0],list[i+1]);// Its not Swooping the Elements
adjust(list, list[0],--i);
}
} // end heapsort
void Sorts::inserth()//i
{ ofstream myfil;
myfil.open ("HeapSorted.txt");
starttime=(long)time(NULL);//beginning time
int list[ELEMENTS]={6,5,3,1,8,7,2,4};
heapsort(list,ELEMENTS);
cout<<endl<<"Heap list: "<<endl;
for(x=0;x<ELEMENTS;x++)
{
cout<<list[x];
}
endtime=(long)time(NULL);
totalseconds=endtime-starttime;
minutes=totalseconds/60;
seconds=totalseconds%60;
cout<<"\nit took "<<minutes<<" min, "<<seconds<<" seconds"<<endl;
}
//header
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
#define ELEMENTS 8
const int Full=10;//numbers perline
const char BLANK=' ';
class Sorts
{
private:
int online;
int key,i,x;
long starttime, endtime, minutes,seconds, totalseconds;
public:
void adjust (int list[], int i , int n);
void makeheap();
void insert();
void inserth();
void SWAP(int list,int n);
void insertsort(int A[],int length);
void heapsort(int list[], int n);
Sorts(void);
~Sorts(void);
};
// Main Program
#include <iostream>
#include "Sorts.h"
using namespace std;
void main (void)
{
Sorts b;
b.insert();
b.inserth();
}
```