-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_merge_sort.cpp
120 lines (105 loc) · 1.71 KB
/
insert_merge_sort.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
#include <iostream>
using namespace std;
template <class T>
class Array
{
private:
T *A;
size_t num;
void mergesort(T *array, int lower_bound, int upper_bound);
void merge(T *arry, int p, int q, int r);
public:
Array(T *array, size_t size){
A = new T[size];
num = size;
for(size_t i = 0; i < size; i++){
A[i] = array[i];
}
}
void insertion_sort();
void merge_sort();
void display();
~Array(){
delete [] A;
}
};
template <class T>
void Array<T>::insertion_sort()
{
T key;
size_t i,j;
for (i = 0; i < num; i++)
{
key = A[i];
for (j = i-1; j >= 0; j--)
{
if(A[j] > key) A[j+1] = A[j];
else break;
}
A[j+1] = key;
}
}
template <class T>
void Array<T>::merge_sort()
{
mergesort(A, 0, num - 1);
}
template <class T>
void Array<T>::mergesort(T *A, int p, int r){
int q;
if (p < r)
{
q = (p + r)/2;
mergesort(A, p, q);
mergesort(A, q+1, r);
merge(A, p, q, r);
}
}
template <class T>
void Array<T>::merge(T *arry, int p, int q, int r)
{
T *left = new T[q - p + 1];
T *right = new T[r - q];
int i,j,k;
int n1 = q - p + 1, n2 = r - q;
for(i = p; i < p + n1; i++) left[i-p] = arry[i];
for (j = q + 1; j < q + n2 + 1; j++) right[j - q -1] = arry[j];
i=0,j=0,k=p;
while(i < n1 && j < n2){
if(left[i] < right[j]) {
arry[k] = left[i];
i++;
}
else{
arry[k] = right[j];
j++;
}
k++;
}
while(i < n1){
arry[k] = left[i];
k++;
i++;
}
while(j < n2){
arry[k] = right[j];
k++;
j++;
}
delete [] left;
delete [] right;
}
template <class T>
void Array<T>::display()
{
for (size_t i = 0; i < num; i++) cout << A[i];
}
int main()
{
int A[8]={2,9,6,5,4,2,3,1};
Array<int> a(A,8);
//a.insertion_sort();
a.merge_sort();
a.display();
return 0;
}