-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort
More file actions
76 lines (64 loc) · 1.95 KB
/
MergeSort
File metadata and controls
76 lines (64 loc) · 1.95 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
74
75
76
#include <iostream>
#include <ctime>
using namespace std;
void mergeSort(double *array, int lenght_of_arr) {
if (lenght_of_arr > 1) {
int lef_size = lenght_of_arr / 2;
int right_size = lenght_of_arr - lef_size;
mergeSort(&array[0], lef_size);
mergeSort(&array[lef_size], right_size);
int left_index = 0, right_index = lef_size, temporary_index = 0;
double *temp_arr = new double[lenght_of_arr];
while (left_index < lef_size || right_size < lenght_of_arr) {
if (array[left_index] < array[right_index]) {
temp_arr[temporary_index] = move(array[left_index]);
temporary_index++;
left_index++;
} else {
temp_arr[temporary_index] = move(array[right_index]);
temporary_index++;
right_index++;
}
if (left_index == lef_size) {
for (int i = right_index; i < lenght_of_arr; i++) {
temp_arr[temporary_index] = array[i];
temporary_index++;
}
break;
}
if (right_index == lenght_of_arr) {
for (int i = left_index; i < lef_size; i++) {
temp_arr[temporary_index] = array[i];
temporary_index++;
}
break;
}
}
copy(&temp_arr[0], &temp_arr[lenght_of_arr], array);
}
}
void TestSort(){
srand(time(NULL));
int n;
bool test_pass_or_failed = true;
n = rand() % 10;
double* array=new double[n];
for(int i=0;i<n;i++){
array[i]=rand();
}
mergeSort(array,n);
for(int i=0;i<n-1;i++){
if(array[i]>array[i+1]) {
test_pass_or_failed=false;
}
}
if(test_pass_or_failed){
cout<<"test passed"<<endl;
}else{
cout<<"test failed"<<endl;
}
}
int main() {
TestSort();
return 0;
}