/// <summary> /// 堆排序(nlgn) /// </summary> /// <returns></returns> public int[] HeapSort() { for (int i = _array.Length - 1; i > 0; i--) { SortUtil.Swap(args: _array, 0, i); HeapSize--; MaintainHeap(0); } return(_array); }
/// <summary> /// 维护堆的性质 /// </summary> /// <param name="i"></param> public void MaintainHeap(int i) { int l = Left(i); int r = Right(i); int min = i; if (l < HeapSize && _array[l] < _array[min]) { min = l; } if (r < HeapSize && _array[r] < _array[min]) { min = r; } if (min != i) { SortUtil.Swap(_array, i, min); MaintainHeap(min); } }