-
Notifications
You must be signed in to change notification settings - Fork 4
/
merge_sort(divide&conquer).cpp
executable file
·128 lines (108 loc) · 2.16 KB
/
merge_sort(divide&conquer).cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Code
#include<iostream>
using namespace std;
class merge_sort
{
private:
int n,z;
int A[20];
public:
void accept();
void display(int arr[]);
void mergesort(int arr[], int p, int r);
void merge(int arr[], int p, int q, int r);
};
void merge_sort::accept()
{
cout<<"\nEnter the number of elements : ";
cin>>n;
cout<<"\nEnter the elements : \n";
for(z=0;z<n;z++)
cin>>A[z];
cout<<"\nUnsorted elements are : \n";
display(A);
mergesort(A,0,n-1);
cout<<"\nSorted elements are : \n";
display(A);
}
void merge_sort::display(int arr[])
{
for(z=0;z<n;z++)
cout<<A[z]<<"\t";
cout<<endl;
}
void merge_sort::mergesort(int arr[], int p, int r)
{
if(p<r)
{
int q = p+(r-p)/2;
mergesort(arr,p,q);
mergesort(arr,q+1,r);
merge(arr,p,q,r);
}
}
void merge_sort::merge(int arr[], int p, int q, int r)
{
int i, j, k;
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[p + i];
for (j = 0; j < n2; j++)
R[j] = arr[q + 1+ j];
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = p; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
int main()
{
merge_sort m;
m.accept();
return 0;
}
/*OUTPUT
safir@safir-Predator-PH315-51:~/Desktop/DAA codes$ g++ merge.cpp
safir@safir-Predator-PH315-51:~/Desktop/DAA codes$ ./a.out
Enter the number of elements : 5
Enter the elements :
4
2
7
9
8
Unsorted elements are :
4 2 7 9 8
Sorted elements are :
2 4 7 8 9
*/