-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds23_a.cpp
93 lines (78 loc) · 1.82 KB
/
ds23_a.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
#include <stdio.h>
#include <stdlib.h>
void heapify(int numberset[], int rootnumber, int N) {
int pivot = numberset[rootnumber];
int childindex = 2 * rootnumber;
while (childindex <= N) {
if (childindex < N && numberset[childindex] > numberset[childindex + 1])
childindex++;
if (pivot < numberset[childindex])
break;
else {
numberset[childindex / 2] = numberset[childindex];
childindex *= 2;
}
numberset[childindex / 2] = pivot;
}
}
void Heapsort(int numberset[], int N) {
for (int i = N / 2; i > 0; i--)
heapify(numberset, i, N);
for (int i = N - 1; i > 0; i--) {
int temp = numberset[i + 1];
numberset[i + 1] = numberset[1];
numberset[1] = temp;
heapify(numberset, 1, i);
}
for (int l = 1; l <= N; l++)
printf("%d ", numberset[l]);
printf("\n");
}
void P1() {
FILE* heapnumber;
fopen_s(&heapnumber, "in1.txt", "r");
int size;
fscanf_s(heapnumber, "%d", &size);
int* heap = (int*)malloc(sizeof(int) * (size + 1));
int index = 1;
while (!feof(heapnumber)) {
fscanf_s(heapnumber, "%d", &heap[index++]);
}
fclose(heapnumber);
printf("¹®Á¦ 1 : ");
Heapsort(heap, size);
}
void Insertsort(int* numberset, int N) {
for (int j = 2; j <= N; j++) {
int pivot = numberset[j];
numberset[0] = pivot;
int k = j - 1;
while (pivot > numberset[k]) {
numberset[k + 1] = numberset[k];
k--;
}
numberset[k + 1] = pivot;
}
for (int k = 1; k <= N; k++)
printf("%d ", numberset[k]);
printf("\n");
}
void P2() {
FILE* insertnumber;
fopen_s(&insertnumber, "in2.txt", "r");
int size;
fscanf_s(insertnumber, "%d", &size);
int* insert = (int*)malloc(sizeof(int) * (size + 1));
int index = 1;
while (!feof(insertnumber)) {
fscanf_s(insertnumber, "%d", &insert[index++]);
}
fclose(insertnumber);
printf("¹®Á¦ 2 : ");
Insertsort(insert, size);
}
int main() {
P1();
P2();
return 0;
}