Skip to content

Commit

Permalink
merge sort
Browse files Browse the repository at this point in the history
  • Loading branch information
NasreenParween committed Jul 2, 2022
1 parent b634921 commit 1932ea5
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
38 changes: 38 additions & 0 deletions DAA_MERGE_SORT/DAA_MERGE_SORT.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="DAA_MERGE_SORT" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/DAA_MERGE_SORT" 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_MERGE_SORT" 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 />
</Project>
</CodeBlocks_project_file>
6 changes: 6 additions & 0 deletions DAA_MERGE_SORT/DAA_MERGE_SORT.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# depslib dependency file v1.0
1642052461 source:c:\users\nasreen parween\desktop\new folder\daa_merge_sort\main.cpp
<iostream>
<fstream>
<stdlib.h>

10 changes: 10 additions & 0 deletions DAA_MERGE_SORT/DAA_MERGE_SORT.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2000" topLine="75" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Binary file added DAA_MERGE_SORT/bin/Debug/DAA_MERGE_SORT.exe
Binary file not shown.
105 changes: 105 additions & 0 deletions DAA_MERGE_SORT/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;
template <class t>
class msort
{

public:
t a[1000];
int count;
int n;
void input();
void display();
t mergesort(t a[], int p, int r);
void Merge(t a[],int p, int q,int r);
};
template <class t>
void msort<t>:: input()
{
int i;
cout<<"\n ENTER THE NUMBER OF ELEMENTS :"<<endl;
cin>>n;
for(i=1;i<=n;i++)
a[i]=rand()%100;
}
template <class t>
void msort<t>::display()
{
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
}
template <class t>
t msort<t>:: mergesort(t a[],int p, int r)
{
if(p<r)
{
int q=(p+r-1)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
Merge(a,p,q,r);

}
return count;
}
template <class t>
void msort<t>:: Merge(t a[], int p, int q, int r)
{
int i,j;
int n1=q-p+1;
int n2=r-q;
t L[500] , R[500];
for(i=1;i<=n;i++)
L[i]=a[p+i-1];
for(j=1; j<=n;j++)
R[j]=a[q+j];
L[n1+1]=INT_MAX;
R[n2+1]=INT_MAX;
i=j=1;
for(int k=p; k<=r; k++)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
count++;
}
}
int main()
{
ofstream f("mergesort.csv");
int c;
char ch='y';
while(ch=='y'||ch=='y')
{
msort<int>m;

m.input();
cout<<"\n THE ELEMENT OF AN ARRAY ARE :";
m.display();
m.count=0;
c=m.mergesort(m.a,1,m.n);
cout<<"\n AFTER SORTHING THE ELEMENTS ARE:"<<endl;
m.display();
if(f)
{
f<<m.n<<","<<c;
f<<endl;
}
cout << "\n THE NUMBER OF COMPARISIONS ARE :" << c;
cout<<"\n DO YOU WANT TO CONTINUE ? (y/n) ";
cin>>ch;
}
cout<<"\n NUMBER OF COMPARISIONS :"<<c;
f.close();

return 0;
}
22 changes: 22 additions & 0 deletions DAA_MERGE_SORT/mergesort.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
No. of Elements (n),No. of Comparisions (count),n*n,log(n)
30,148,900,4.906890596
50,286,2500,5.64385619
70,432,4900,6.129283017
90,592,8100,6.491853096
110,752,12100,6.781359714
130,914,16900,7.022367813
150,1094,22500,7.22881869
170,1274,28900,7.409390936
190,1454,36100,7.569855608
210,1634,44100,7.714245518
230,1814,52900,7.845490051
250,1994,62500,7.965784285
270,2188,72900,8.076815597
290,2388,84100,8.17990909
310,2588,96100,8.276124405
330,2788,108900,8.366322214
350,2988,122500,8.451211112
370,3188,136900,8.531381461
390,3388,152100,8.607330314
410,3588,168100,8.6794801
430,3788,184900,8.74819285
Binary file added DAA_MERGE_SORT/obj/Debug/main.o
Binary file not shown.

0 comments on commit 1932ea5

Please sign in to comment.