// 堆排,0号位置不存数
void head_adjust(int arr[], int k, int size_adjust){
arr[0] = arr[k];
int i;
for (i=2*k ; i <= size_adjust ; i*=2){
if (i+1<= size_adjust && arr[i] <arr[i+1])
i +=1;
if (arr[0]>= arr[i])
break;
else{
arr[k] = arr[i];
k = i;
}
}
arr[k] = arr[0];
}
void build_max_heap(int *arr, int size){
for (int i= size/2; i>0; i--){
head_adjust(arr, i, size);
}
}
void heap_sort(int arr[], int size){
int new[size+1],i;
for (i = 1; i<= size; i++)
new[i] = arr[i-1];
build_max_heap( new,size);
for (i = 1; i<size; i ++){
new[0] = new[1];
new[1] = new[size - i+1];
new[size - i+1] = new[0];
head_adjust(new, 1, size-i);
}
for (i =0; i <=size; i++)
arr[i] = new[i+1];
}
// 快排,0号位被利用,start=0,end = size-1
int partition(int arr[],int low, int high){
int pivot = arr[low];
while (low <high){
while(low <high && arr[high] >=pivot) high--;
arr[low] = arr[high];
while(low <high && arr[low] <= pivot) low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
void quick_sort_inside(int arr[], int start, int end){
int pivot = partition(arr,start,end);
if (start < pivot-1) quick_sort_inside(arr, start, pivot-1);
if (end > pivot+1) quick_sort_inside(arr, pivot+1, end);
}