-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort_v0.1.cpp
174 lines (153 loc) · 2.93 KB
/
merge_sort_v0.1.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Consolearrpplication1.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include <iostream>
void swap(int &a, int &b);
void sort(int *p_arr, int n);
void checklength(int &a_size, int &b_size, int stage, int idx, int n);
void compare(int *p_a, int *p_b, int a_size, int b_size, int *p_new_arr, int &new_idx);
void choose(int *p_comp, int *p_input_arr, int stage, int &idx, int n);
int main()
{
int arr[10] = { 2, 10, 12, 18, 25, 6, 16, 20, 32, 34 };
int new_arr[10], input_arr[10];
int n = sizeof(arr) / sizeof(int);
int idx = 0, stage = 1;
int a_size, b_size;
int round = 1;
int new_idx = -1;
/*sorting*/
sort(arr, n);
for (int i = 0; i < n; i++)
{
new_arr[i] = arr[i];
}
/*merging*/
while (pow(2, stage) < n)
{
for (int i = 0; i < n; i++)
{
input_arr[i] = new_arr[i];
}
while (idx < n)
{
checklength(a_size, b_size, stage, idx, n);
int *a;
int *b;
a = new int[a_size];
b = new int[b_size];
choose(a, input_arr, stage, idx, n);
choose(b, input_arr, stage, idx, n);
compare(a, b, a_size, b_size, new_arr, new_idx);
delete[] a;
delete[] b;
}
idx = 0;
stage++;
new_idx = -1;
}
/*result*/
std::cout << "The sorted array is as following: " << std::endl;
for (int i = 0; i < n; i++)
std::cout << new_arr[i] << " ";
std::cout << std::endl;
}
void sort(int *p_arr, int n)
{
int i = 0;
while (i < n)
{
if (i + 1 < n)
{
if (p_arr[i] > p_arr[i + 1])
swap(p_arr[i], p_arr[i + 1]);
}
i += 2;
}
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void checklength(int &a_size, int &b_size, int stage, int idx, int n)
{
int remainder = n - idx;
int set_size = pow(2, stage);
if (remainder >= 2 * set_size)
{
a_size = set_size;
b_size = set_size;
}
else if (2 * set_size > remainder && remainder >= set_size)
{
a_size = set_size;
b_size = remainder - a_size;
}
else if (set_size > remainder)
{
a_size = remainder;
b_size = 0;
}
}
void choose(int *p_comp, int *p_input_arr, int stage, int &idx, int n)
{
int round = 1;
while (round <= pow(2, stage) && idx <= n - 1)
{
p_comp[round - 1] = p_input_arr[idx];
idx++;
round++;
}
round = 1;
}
void compare(int *p_a, int *p_b, int a_size, int b_size, int *p_new_arr, int &new_idx)
{
int i = 0, j = 0, k = 0;
if (b_size != 0)
{
while (k < a_size + b_size)
{
if (i < a_size && j < b_size)
{
if (p_a[i] <= p_b[j])
{
new_idx++;
p_new_arr[new_idx] = p_a[i];
i++;
}
else if (p_a[i]>p_b[j])
{
new_idx++;
p_new_arr[new_idx] = p_b[j];
j++;
}
}
else if (i != a_size || j != b_size)
{
if (i == a_size)
{
new_idx++;
p_new_arr[new_idx] = p_b[j];
j++;
}
else if (j == b_size)
{
new_idx++;
p_new_arr[new_idx] = p_a[i];
i++;
}
}
k++;
}
}
else if (b_size == 0)
{
for (int ii = 0; ii < a_size; ii++)
{
new_idx++;
p_new_arr[new_idx] = p_a[ii];
}
}
}