
for (int i = n - 1; i > 0; i--) {
int max = arr[0];
int max_i = 0;
for (int j = 1; j <= i; j++)
if (arr[j] > max) {
max = arr[j];
max_i = j;
}
int temp = arr[i];
arr[i] = arr[max_i];
arr[max_i] = temp;
}

for(int i = 0; i < n; i++)
for(int j = 0; j < n - 1; j++)
if(arr[j] > arr[j + 1]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}

for (int i = 1; i < n; i++){
for (int j = i - 1; j >= 0; j--){
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
else break;
}
}

for (int i = 1; i < 6; i++) {
pos = i;
while (pos > 0 && A[pos] < A[(pos - 1) / 2]) {
swap(A[pos], A[(pos - 1) / 2]);
pos = (pos - 1) / 2;
}
while (pos > 0 && A[pos] < A[(pos - 2) / 2]) {
swap(A[pos], A[(pos - 2) / 2]);
pos = (pos - 2) / 2;
}
}
⠀⠀
while(len > 0) {
printf("%d ", A[0]);
len--;
temp = A[0];
A[0] = A[len];
A[len] = temp;
rearrange(len, A);
}
void rearrange(int len, int A[]) {
int pos = 0;
int left, right;
while (pos < len) {
left = pos * 2 + 1;
right = pos * 2 + 2;
if (left >= len)
return;
if (right >= len) {
if (A[left] < A[pos])
swap(A[left], A[pos]);
return;
}
else if ((A[left] <= A[right] && A[left] < A[pos])) {
swap(A[left], A[pos]);
pos = left;
}
else if (A[right] <= A[left] && A[right] < A[pos]) {
swap(A[right], A[pos]);
pos = right;
}
}
return;
}




많은 종류의 정렬을 다루다보니 분량이 많아졌다. 아직도 자료구조 수업 복습하는 느낌? 오히려 좋아.