void adjust_down(int *array, int size, int pos) { //Adjust down int left = 2*pos + 1; int right = 2*pos + 2; int temp, temp2; //f**k making code readable if(right<size) { if(array[right] < array[left]) { temp = left; } else { temp = right; } if(array[pos] < array[temp]) { temp2 = array[pos]; array[pos] = array[temp]; array[temp] = temp2; adjust_down(array, size, temp); } } if(left<size) { temp = left; if(array[pos] < array[temp]) { temp2 = array[pos]; array[pos] = array[temp]; array[temp] = temp2; adjust_down(array, size, temp); } } }
void heap_sort(int* arr, int len) { int index ; for(index = (len - 2) >> 1 ; index >= 0; index --) { adjust_down(arr, index, len - 1); } for(index = len - 1; index >= 1; index --) { swap(arr[index], arr[0]); adjust_down(arr, 0, index - 1); } }
void heap_sort(int *array, int size) { if(size == 0) { //We're done! return; } //Swap to back int temp = array[size-1]; array[size-1] = array[0]; array[0] = temp; size--; adjust_down(array, size, 0); heap_sort(array, size); }