-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion or Heap Sort.cpp
118 lines (113 loc) · 2.06 KB
/
Insertion or Heap 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
#include <iostream>
int a[110], b[110], c[110];
int insert(int n)
{
for (int i = n; i > 0; i--)
{
if (a[i] < a[i - 1])
{
int temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
}
}
return 0;
}
int heap(int m, int n)
{
int p = m;
while (p * 2 + 1 < n)
{
int temp;
if (p * 2 + 2 < n)
{
temp = b[p * 2 + 1] > b[p * 2 + 2] ? p * 2 + 1 : p * 2 + 2;
}
else
temp = p * 2 + 1;
if (b[temp] > b[p])
{
int x = b[temp];
b[temp] = b[p];
b[p] = x;
p = temp;
}
else
{
break;
}
}
return 0;
}
int main()
{
int n;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i < n; i++)
{
std::cin >> c[i];
}
int i;
for (i = 1; i < n; i++)
{
insert(i);
int j;
for (j = 0; j < n; j++)
{
if (a[j] != c[j])
break;
}
if (j == n)
{
break;
}
}
if (i < n)
{
std::cout << "Insertion Sort" << std::endl;
insert(i + 1);
for (int j = 0; j < n - 1; j++)
{
std::cout << a[j] << " ";
}
std::cout << a[n - 1];
return 0;
}
std::cout << "Heap Sort" << std::endl;
for (int i = (n - 2) / 2; i >= 0; i--)
{
heap(i, n);
}
for (i = 0; i < n; i++)
{
int j;
for (j = 0; j < n; j++)
{
if (b[j] != c[j])
break;
}
if (j == n)
{
break;
}
int temp = b[0];
b[0] = b[n - 1 - i];
b[n - 1 - i] = temp;
heap(0, n - 1 - i);
}
int temp = b[0];
b[0] = b[n - 1 - i];
b[n - 1 - i] = temp;
heap(0, n - 1 - i);
for (int j = 0; j < n - 1; j++)
{
std::cout << b[j] << " ";
}
std::cout << b[n - 1];
return 0;
}