堆排序
堆排序:
template<class T> void _AdjustDown(T* arr, size_t size, size_t index) { size_t child = index * 2 + 1; while (child < size) { if (child + 1 < size && arr[child + 1] > arr[child]) { ++child; } if (arr[child]>arr[index]) { swap(arr[child], arr[index]); index = child; child = child * 2 + 1; } else { break; } } } template<class T> T* HeapSort(T* arr, size_t size) { assert(arr); for (int i = (size - 2) / 2; i >= 0; --i) { _AdjustDown(arr, size, i); } while (size > 1) { swap(arr[0], arr[size - 1]); --size; _AdjustDown(arr, size, 0); return arr; } }