//insertion sort, for Kth element, make sure the first K elements are sorted
void insertion_sort(int[] A, int n) {
if (!n) return;
for(int i = 1; i < n; ++i) {
for(int j = i; j >= 0; --j) {
if (A[j] > A[j-1]) break;
else {
int tmp = A[j-1];
A[j-1] = A[j];
A[j] = tmp;
}
}
}
}
//selection sort, for Kth element, find the smallest element in the rest
void selection_sort(int[] A, int n) {
if (!n) return;
for(int i = 0; i < n; ++i) {
int max = i;
for(int j = i+1; j <= n; ++j) {
if (A[j] > A[max]) max = j;
}
int tmp = A[i];
A[i] = A[max];
A[max] = tmp;
}
}
//heap sort, treat the array as a heap and sort it
void heap_sort(int[] A, int n) {
for(int i = 0; i < n; ++i) {
int min = deleteMin(A, i, n); //operation defined in minBinary heap
A[i] = min;
}
}
//merge sort, divide and conquer
void merge_sort(int[] A, int n);
//quick sort
void quick_sort(int[] A, int start, int end) {
if (start >= end) return;
if (start + 1 == end) {
A[start] = min(A[start], A[end]);
A[end] = max(A[start], A[end]);
return;
}
int mid = start + ((end-start) >> 1);
int pivot = A[mid];
int i = start;
int j = end;
while (i <= j) {
while (A[i] < pivot) ++i;
while (A[j] > pivot) --j;
if (i <= j) {
swap(A[i], A[j]);
++i;
--j;
}
}
if (start < j) quick_sort(A, start, j);
if (i < end) quick_sort(A, i, end);
}