-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
167 lines (123 loc) · 3.67 KB
/
Copy pathsort.cpp
File metadata and controls
167 lines (123 loc) · 3.67 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
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
#include <iostream>
#define SWAP(A,B,temp) ( temp = A, A=B, B=temp )
using namespace std;
void selectiocSort(int* list, int n){
int minIndex, temp;
for( int i = 0; i < n-1 ; i++){
minIndex = i;
for(int j = i+1 ; j < n ; j++){
if(list[j] < list[minIndex]){
minIndex = j;
}
}
if( minIndex != i )
SWAP(list[minIndex], list[i], temp);
}
}
void bubbleSort(int* list, int n){
int temp;
for( int i = 0 ; i < n ; i++){
for( int j = 0 ; j < n - i -1 ; j++){
if( list[j] > list[j+1])
SWAP(list[j], list[j+1], temp);
}
}
}
void insertionSort(int* list, int n){
int temp;
for(int i = 1 ; i < n ; i++){
int key = list[i];
for(int j = i -1 ; (j >= 0 && key < list[j] ) ; j-- ){
list[j+1] = list[j];
}
list[i] = key;
}
}
// pivot값 중심으로 왼-작, 오-큰 부분 배열로 분할, pivot의 위치 반환
int partition(int* list, int left, int right){
int low, high, temp;
int pivot = list[left];
low = left+1;
high = right;
//왼-작, 오-큰 으로 분할해야하니까 왼-큰, 오-작 을 찾아야함
while(low <= high){
while( list[low] < pivot && low <= right ) low++;
while( list[high] > pivot && high <= left ) right--;
// 왼-큰, 오-작 인 경우 바꿔주기
if(low <= high){
SWAP(list[low], list[high], temp);
//바꿔줬으므로 인덱스는 이동하게끔
low++;
right--;
}
}
SWAP(list[left], list[high], temp);
return high;
}
void quickSort(int* list, int left, int right){
int pivotIndex;
if(left < right){
pivotIndex = partition(list, left, right);
}
quickSort(list, left, pivotIndex -1);
quickSort(list, pivotIndex+1, right);
}
int* merge(int* list, int left, int mid, int right){
int* sorted = (int*)malloc( (*list) );
int low = left;
int high = mid +1;
int index = left;
while(low <= mid && high <= right ){
if(list[mid] < list[high])
sorted[index++] = list[low++];
else
sorted[index++] = list[high++];
}
// 남아있는 데이터가 있는 경우 ( 즉, 어느 한 쪽이 다 들어간 경우 )
if( low > mid){
for( int i = high ; i <= right ; i++)
sorted[index++] = list[i];
}
else{
for( int i = low ; i <= mid ; i++)
sorted[index++] = list[i];
}
return sorted;
}
void mergeSort(int* list, int left, int right){
int* sorted;
if( left < right){
int mid = ( left + right ) / 2; // Divide
mergeSort(list, left, mid -1); // Conquer
mergeSort(list, mid, right); // Conquer
sorted = merge(list, left, mid, right);
}
}
void heapify(int* list, int index, int size){
int temp;
int largest = index;
int leftChild = (index*2);
int rightChild = (index*2+1);
if( leftChild < size && list[leftChild] > list[largest])
largest = leftChild;
if( rightChild < size && list[rightChild] > list[largest])
largest = rightChild;
// 위치가 바뀌어야 한다면
if(largest != index){
SWAP(list[index], list[largest], temp);
// 자리가 바뀐 자식에 의해 한 번 더 heapify
heapify(list, largest, size);
}
}
void heapSort(int* list, int size){
for(int i = size/2 ; i >= 1 ; i--){
heapify(list, i, size);
}
}
int main(void){
int ary[6] = {0, 1, 2, 3, 4, 5};
heapSort(ary, 6);
for(int i = 1 ; i < 6 ; i++)
cout << ary[i];
return 0;
}