Skip to content

Commit

Permalink
practice question
Browse files Browse the repository at this point in the history
  • Loading branch information
NasreenParween committed Jun 30, 2022
1 parent 07484e4 commit 9fe98eb
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
40 changes: 40 additions & 0 deletions DAA_HEAPSORT/DAA_HEAPSORT.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="DAA_HEAPSORT" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/DAA_HEAPSORT" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/DAA_HEAPSORT" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
6 changes: 6 additions & 0 deletions DAA_HEAPSORT/DAA_HEAPSORT.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# depslib dependency file v1.0
1644418377 source:c:\users\nasreen parween\desktop\new folder\daa_heapsort\main.cpp
<iostream>
<fstream>
<stdlib.h>

20 changes: 20 additions & 0 deletions DAA_HEAPSORT/HeapSort.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
No. of Elements(n),No. of Comparisions(count),n*n,n*log( n)
50,540,2500,282.1928095
100,1260,10000,664.385619
150,2052,22500,1084.322804
200,2900,40000,1528.771238
250,3818,62500,1991.446071
300,4718,90000,2468.645607
350,5666,122500,2957.923889
400,6648,160000,3457.542476
450,7574,202500,3966.201536
500,8602,250000,4482.892142
550,9558,302500,5006.808295
600,10594,360000,5537.291214
650,11668,422500,6073.79234
700,12714,490000,6615.847778
750,13798,562500,7163.060089
800,14770,640000,7715.084952
850,15962,722500,8271.621176
900,17060,810000,8832.403072
950,18110,902500,9397.194518
Binary file added DAA_HEAPSORT/bin/Debug/DAA_HEAPSORT.exe
Binary file not shown.
116 changes: 116 additions & 0 deletions DAA_HEAPSORT/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
template<class t>
class HeapSort
{
t A[1000];
public:
int size, heap_size, count;
t temp;
void input();
void display();
void MAX_HEAPIFY(t A[], int i);
void BUILD_MAX_HEAP(t A[]);
void heapsort(t A[]);
int left(int i);
int right(int i);
};
template<class t>
void HeapSort<t>::input()
{
count=0;
cout<<"Enter the size of Array : ";
cin>>size;
cout<<"\nEnter the elements of the Array : ";
for(int i=1;i<=size;i++)
{
A[i]=(rand()%1000 + 1);
cout<<A[i]<<"\t";
}
heap_size=size;
heapsort(A);
}
template<class t>
void HeapSort<t>::display()
{
cout<<"\nSorted Array : ";
for(int i=1;i<=size;i++)
{
cout<<A[i]<<"\t";
}
cout<<"\nNo of Comparisons : "<<count;
}
template<class t>
void HeapSort<t>::MAX_HEAPIFY(t A[], int i)
{
int l,r,largest;
l=left(i);
r=right(i);
if(l<=heap_size && A[l]>A[i])
largest=l;
else
largest=i;
count++;
if(r<=heap_size && A[r]>A[largest])
largest=r;
count++;
if(largest!=i)
{
temp=A[i];
A[i]=A[largest];
A[largest]=temp;
MAX_HEAPIFY(A,largest);
}
}
template<class t>
void HeapSort<t>::BUILD_MAX_HEAP(t A[])
{
for(int i=size/2;i>=1;i--)
{
MAX_HEAPIFY(A,i);
}
}
template<class t>
void HeapSort<t>::heapsort(t A[])
{
BUILD_MAX_HEAP(A);
for(int i=size;i>=2;i--)
{
temp=A[1];
A[1]=A[i];
A[i]=temp;
heap_size=heap_size-1;
MAX_HEAPIFY(A,1);
}
}
template<class t>
int HeapSort<t>::left(int i)
{
return(i*2);
}
template<class t>
int HeapSort<t>::right(int i)
{
return(i*2+1);
}
int main()
{
HeapSort<int> h;
ofstream fout;
fout.open("HeapSort.csv");
char ch='y';
while(ch=='y'||ch=='Y')
{
h.input();
h.display();
fout<<h.size<<","<<h.count;
fout<<endl;
cout<<"\n\nDo you wish to continue(y/n) : ";
cin>>ch;
}
fout.close();
return 0;
}

Binary file added DAA_HEAPSORT/obj/Debug/main.o
Binary file not shown.

0 comments on commit 9fe98eb

Please sign in to comment.