-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomMerge - Copy.h
89 lines (66 loc) · 1.41 KB
/
customMerge - Copy.h
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
#ifndef customMerge
#define customMerge
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <typeinfo>
#include <regex>
using namespace std;
//TEMPLATED MERGE SORT
template <class customType> void merge(vector<customType>& larray,vector<customType>& rarray,vector<customType>& oarray){
int l=0;
int r=0;
int a=0;
int lsize=larray.size();
int rsize=rarray.size();
while(l<lsize && r<rsize){
if(larray[l]<rarray[r]){
oarray[a]=larray[l];
l++;
a++;
}
else{
oarray[a]=rarray[r];
r++;
a++;
}
}
if(l<lsize){
for(l;l<lsize;l++){
oarray[a]=larray[l];
a++;
}
}
if(r<rsize){
for(r;r<rsize;r++){
oarray[a]=rarray[r];
a++;
}
}
}
template <class customType> void mergeSort(vector<customType>& oarray){
int size=oarray.size();
if(size>1){
int mid=size/2;
int lsize=mid;
int rsize=size-lsize;
vector<customType> leftarray;
int a=0;
for(a;a<lsize;a++){
leftarray.push_back(oarray[a]);
}
//printarray(leftarray,leftsize);
vector<customType> rightarray;
for(int r=0;r<rsize;r++){
rightarray.push_back(oarray[a]);
a++;
}
//nl();
//printarray(rightarray,rightsize);
mergeSort(leftarray);
mergeSort(rightarray);
merge(leftarray,rightarray,oarray);
}
}
#endif