From 961415022b11a818c85dcdc3aba63bea5f0c6666 Mon Sep 17 00:00:00 2001 From: qtczz <93128217+bRidgeQTCZZ@users.noreply.github.com> Date: Wed, 6 Mar 2024 18:43:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=89=8B=E6=92=95=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=BD=92=E5=B9=B6=E6=8E=92=E5=BA=8FJava=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=AD=E6=B7=B7=E5=85=A5=E7=9A=84=E5=A0=86=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/algorithm-mandatory/handtearing.md | 50 -------------------------- 1 file changed, 50 deletions(-) diff --git a/src/algorithm-mandatory/handtearing.md b/src/algorithm-mandatory/handtearing.md index 3698e98..d2b6b58 100644 --- a/src/algorithm-mandatory/handtearing.md +++ b/src/algorithm-mandatory/handtearing.md @@ -469,56 +469,6 @@ public class MergeSort { } } } -public class HeapSort { - public static void heapSort(int[] arr) { - int n = arr.length; - - // 构建大顶堆 - for (int i = n / 2 - 1; i >= 0; i--) { - adjustHeap(arr, i, n); - } - - // 排序 - for (int i = n - 1; i >= 0; i--) { - // 交换堆顶元素和最后一个元素 - int temp = arr[0]; - arr[0] = arr[i]; - arr[i] = temp; - - // 调整堆 - adjustHeap(arr, 0, i); - } - } - - private static void adjustHeap(int[] arr, int i, int n) { - int temp = arr[i]; - - for (int k = 2 * i + 1; k < n; k = 2 * k + 1) { - // 找到左右子节点中较大的一个 - if (k + 1 < n && arr[k] < arr[k + 1]) { - k++; - } - - // 如果子节点大于父节点,则交换它们 - if (arr[k] > temp) { - arr[i] = arr[k]; - i = k; - } else { - break; - } - } - - arr[i] = temp; - } - - public static void main(String[] args) { - int[] arr = {3, 6, 8, 2, 4, 1, 9, 5, 7}; - heapSort(arr); - for (int num : arr) { - System.out.print(num + " "); - } - } -} ``` @tab golang