ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
> 时间复杂度O(nlog(n)),辅助空间O(nlog(n)),不稳定 ```java public static <T extends Comparable> void heapSort(T[] a) { int n = a.length; for (int k = n / 2; k >= 1; k--) { sink(a, k, n); } while (n > 1) { swap(a, 1 - 1, (n--) - 1); sink(a, 1, n); } } private static <T extends Comparable> void sink(T[] pq, int k, int n) { while (2 * k <= n) { int j = 2 * k; if (j < n && less(pq[j - 1], pq[j])) { j++; } if (!less(pq[k - 1], pq[j - 1])) { break; } swap(pq, k - 1, j - 1); k = j; } } ```