-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtw1Merge.cpp
More file actions
73 lines (63 loc) · 1.24 KB
/
tw1Merge.cpp
File metadata and controls
73 lines (63 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
#include<time.h>
#define max 1000
int A[max] , B[max];
void Merge(int low , int mid , int high)
{
int i = low , j = mid + 1 , k = low;
while(i<=mid && j<=high)
{
if(A[i] <= A[j])
{
B[k++] = A[i++];
}
else{
B[k++] = A[j++];
}
}
while(i<=mid){
B[k++] = A[i++];
}
while(j<=high)
{
B[k++] = A[j++];
}
for(int i=0;i<=high ;i++)
A[i] = B[i];
}
void mergeSort(int low , int high)
{
if(low < high)
{
int mid = (low + high)/2;
mergeSort(low , mid);
mergeSort(mid+1 , high);
Merge(low , mid , high);
}
}
int main()
{
int n;
time_t s , e;
cout << "Enter the Number of Elements : ";
cin >> n;
cout << "Elements are : ";
for(int i=0;i<n;i++)
{
A[i] = rand()%200;
cout << A[i] << "\t";
}
cout << endl << endl;
s= clock();
for(int i=0;i<=100000;i++)
mergeSort( 0 , n-1);
e = clock();
cout << "Elements after sorting are : ";
for(int i=0;i<n;i++)
{
cout << A[i] << "\t";
}
double cpu_time = (double)(e-s)/CLK_TCK;
cout << endl << "Time : " << cpu_time;
}