From 1b2a33d72defd3a21a235c9410f8c0322f8aa436 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Fri, 18 Mar 2022 22:32:56 +0800 Subject: [PATCH 1/3] quick sort --- .../ehlxr/algorithm/sort/QuickSort.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java index 2f4e383..98ba274 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java @@ -33,7 +33,7 @@ import java.util.Arrays; public class QuickSort { public static void main(String[] args) { int[] a = new int[]{3, 9, 5, 7, 1, 2}; - sort(a, 0, a.length - 1); + sort2(a, 0, a.length - 1); System.out.println(Arrays.toString(a)); } @@ -85,5 +85,32 @@ public class QuickSort { a[j] = temp; } + public static void sort2(int[] a, int l, int r) { + if (a == null || l >= r) { + return; + } + int i = l, j = r; + int p = l; // 选择最左边的元素为 pivot + while (l < r) { + // 如果选择 p = l 必须先从右边找到小于 a[p] 的第一个元素 + while (l < r && a[r] >= a[p]) { + r--; + } + swap(a, r, p); + p = r; + + // 从左边找到大于 a[p] 的第一个元素 + while (l < r && a[l] <= a[p]) { + l++; + } + swap(a, l, p); + p = l; + System.out.println(Arrays.toString(a)); + + } + + sort2(a, i, p - 1); + sort2(a, p + 1, j); + } } From b7da6e9537d88be0050de1e03b9540c44ce6a05c Mon Sep 17 00:00:00 2001 From: ehlxr Date: Fri, 18 Mar 2022 22:38:16 +0800 Subject: [PATCH 2/3] update at 2022-03-18 22:38:16 by ehlxr --- .../src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java index 98ba274..93d7d4f 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/sort/QuickSort.java @@ -108,7 +108,7 @@ public class QuickSort { p = l; System.out.println(Arrays.toString(a)); - } + } sort2(a, i, p - 1); sort2(a, p + 1, j); From aaccb2114b6817490bb546e4f909fd0643380117 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Thu, 24 Mar 2022 09:32:13 +0800 Subject: [PATCH 3/3] update at 2022-03-24 09:32:13 by ehlxr --- .../dp/{KnapSack3.java => KnapSack.java} | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) rename budd-common/src/main/java/io/github/ehlxr/algorithm/dp/{KnapSack3.java => KnapSack.java} (85%) diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack3.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java similarity index 85% rename from budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack3.java rename to budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java index c9711df..2014ce6 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack3.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/dp/KnapSack.java @@ -30,20 +30,38 @@ package io.github.ehlxr.algorithm.dp; * @author ehlxr * @since 2022-03-05 14:31. */ -public class KnapSack3 { +public class KnapSack { private final int[] weight = {2, 2, 4, 6, 3}; // 物品的重量 private final int[] value = {3, 4, 8, 9, 6}; // 物品的价值 private final int n = 5; // 物品个数 private final int w = 9; // 背包承受的最大重量 private int maxV = Integer.MIN_VALUE; // 结果放到 maxV 中 + public static void main(String[] args) { + System.out.println(knapsack3(new int[]{2, 2, 4, 6, 3}, new int[]{3, 4, 8, 9, 6}, 5, 9)); + System.out.println(knapsack(new int[]{2, 2, 4, 6, 3}, new int[]{3, 4, 8, 9, 6}, 5, 9)); + } + /** * 动态规划方式 * * @param weight 物品重量 * @param value 物品的价值 - * @param n: 物品个数 - * @param w: 背包可承载重量 + * @param n 物品个数 + * @param w 背包可承载重量 + * @return 最大价值 + */ + public static int knapsack(int[] weight, int[] value, int n, int w) { + int[][] dp = new int[][]; + } + + /** + * 动态规划方式 + * + * @param weight 物品重量 + * @param value 物品的价值 + * @param n 物品个数 + * @param w 背包可承载重量 * @return 最大价值 */ public static int knapsack3(int[] weight, int[] value, int n, int w) {