<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>算法 &#8211; chang的个人博客</title>
	<atom:link href="https://www.qiqin-chang.cn/category/%e7%ae%97%e6%b3%95/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.qiqin-chang.cn</link>
	<description></description>
	<lastBuildDate>Tue, 03 Jun 2025 04:36:49 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.qiqin-chang.cn/wp-content/uploads/2025/04/cropped-无背景-圆形-32x32.png</url>
	<title>算法 &#8211; chang的个人博客</title>
	<link>https://www.qiqin-chang.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>密码保护：位运算</title>
		<link>https://www.qiqin-chang.cn/%e4%bd%8d%e8%bf%90%e7%ae%97/</link>
					<comments>https://www.qiqin-chang.cn/%e4%bd%8d%e8%bf%90%e7%ae%97/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:35:44 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=501</guid>

					<description><![CDATA[无法提供摘要。这是一篇受保护的文章。]]></description>
										<content:encoded><![CDATA[<form action="https://www.qiqin-chang.cn/znyz" class="post-password-form" method="post"><input type="hidden" name="redirect_to" value="https://www.qiqin-chang.cn/%e4%bd%8d%e8%bf%90%e7%ae%97/" /></p>
<p>此内容受密码保护。如需查阅，请在下方输入密码。</p>
<p><label for="pwbox-501">密码： <input name="post_password" id="pwbox-501" type="password" spellcheck="false" required size="20" /></label> <input type="submit" name="Submit" value="提交" /></p>
</form>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e4%bd%8d%e8%bf%90%e7%ae%97/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>密码保护：字符串</title>
		<link>https://www.qiqin-chang.cn/%e5%ad%97%e7%ac%a6%e4%b8%b2/</link>
					<comments>https://www.qiqin-chang.cn/%e5%ad%97%e7%ac%a6%e4%b8%b2/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:35:19 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=499</guid>

					<description><![CDATA[无法提供摘要。这是一篇受保护的文章。]]></description>
										<content:encoded><![CDATA[<form action="https://www.qiqin-chang.cn/znyz" class="post-password-form" method="post"><input type="hidden" name="redirect_to" value="https://www.qiqin-chang.cn/%e5%ad%97%e7%ac%a6%e4%b8%b2/" /></p>
<p>此内容受密码保护。如需查阅，请在下方输入密码。</p>
<p><label for="pwbox-499">密码： <input name="post_password" id="pwbox-499" type="password" spellcheck="false" required size="20" /></label> <input type="submit" name="Submit" value="提交" /></p>
</form>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e5%ad%97%e7%ac%a6%e4%b8%b2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>二叉树遍历</title>
		<link>https://www.qiqin-chang.cn/%e4%ba%8c%e5%8f%89%e6%a0%91%e9%81%8d%e5%8e%86/</link>
					<comments>https://www.qiqin-chang.cn/%e4%ba%8c%e5%8f%89%e6%a0%91%e9%81%8d%e5%8e%86/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:30:46 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=493</guid>

					<description><![CDATA[二叉树遍历：]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">二叉树遍历：</h2>



<pre class="wp-block-code"><code>import java.util.*;
​
public class Main {
    public static void main(String&#91;] args) {
​
        Node nodeA = new Node("A");
        Node nodeB = new Node("B");
        Node nodeC = new Node("C");
        Node nodeD = new Node("D");
        Node nodeE = new Node("E");
        Node nodeF = new Node("F");
        Node nodeG = new Node("G");
​
        nodeA.left = nodeB;
        nodeA.right = nodeC;
        nodeB.left = nodeD;
        nodeB.right = nodeE;
        nodeC.right = nodeF;
        nodeE.left = nodeG;
​
        //递归方式
        preOrder(nodeA);
        System.out.println();
        midOrder(nodeA);
        System.out.println();
        postOrder(nodeA);
        System.out.println();
        //非递归方式
        preOrder2(nodeA);
        System.out.println();
        midOrder2(nodeA);
        System.out.println();
        postOrder2(nodeA);
        System.out.println();
        //层序遍历
        bfs(nodeA);
        System.out.println();
        List&lt;List&lt;String>> lists = bfs2(nodeA);
        for (List&lt;String> list : lists) {
            for (String str : list) {
                System.out.print(str);
            }
            System.out.println();
        }
    }
​
    public static class Node {
        private String value;
        private Node left;
        private Node right;
​
        public Node(String value) {
            this.value = value;
        }
    }
​
    //递归方式
    //先序遍历
    private static void preOrder(Node root) {
        if (root == null) return;
        System.out.print(root.value);
        preOrder(root.left);
        preOrder(root.right);
    }
​
    //中序遍历
    private static void midOrder(Node root) {
        if (root == null) return;
        midOrder(root.left);
        System.out.print(root.value);
        midOrder(root.right);
    }
    //后序遍历
    private static void postOrder(Node root) {
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.value);
    }
​
    //非递归方式
    //先序遍历
    private static void preOrder2(Node root) {
        if (root == null) return;
        Stack&lt;Node> stack = new Stack&lt;>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            System.out.print(node.value);
            if (node.right != null) stack.push(node.right);
            if (node.left != null) stack.push(node.left);
        }
    }
​
    //中序遍历
    private static void midOrder2(Node root) {
        if (root == null) return;
        Stack&lt;Node> stack = new Stack&lt;>();
        Node cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            Node node = stack.pop();
            System.out.print(node.value);
            if (node.right != null) cur = node.right;
        }
    }
    //后序遍历
    private static void postOrder2(Node root) {
        if (root == null) return;
        Stack&lt;Node> stack = new Stack&lt;>();
        Stack&lt;Node> stack2 = new Stack&lt;>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            stack2.push(node);
            if (node.left != null) stack.push(node.left);
            if (node.right != null) stack.push(node.right);
        }
        while (!stack2.isEmpty()) {
            System.out.print(stack2.pop().value);
        }
    }
​
    //层序遍历
    private static void bfs(Node root) {
        if (root == null) return;
        Queue&lt;Node> queue = new LinkedList&lt;>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            System.out.print(node.value);
            if (node.left != null) queue.add(node.left);
            if (node.right != null) queue.add(node.right);
        }
    }
​
    //层序遍历按层输出
    private static List&lt;List&lt;String>> bfs2(Node root) {
        List&lt;List&lt;String>> res = new ArrayList&lt;>();
        Queue&lt;Node> queue = new LinkedList&lt;>();
        queue.add(root);
​
        List&lt;String> temp;
        while (!queue.isEmpty()) {
            int size = queue.size();
            temp = new ArrayList&lt;>();
            while (size-- > 0) {
                Node node = queue.poll();
                temp.add(node.value);
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
            }
            res.add(temp);
        }
​
        return res;
    }
​
​
}</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e4%ba%8c%e5%8f%89%e6%a0%91%e9%81%8d%e5%8e%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>递归算法</title>
		<link>https://www.qiqin-chang.cn/%e9%80%92%e5%bd%92%e7%ae%97%e6%b3%95/</link>
					<comments>https://www.qiqin-chang.cn/%e9%80%92%e5%bd%92%e7%ae%97%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:27:42 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=491</guid>

					<description><![CDATA[全排序： 斐波那契数列： 汉诺塔：]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">全排序：</h2>



<pre class="wp-block-code"><code>import java.util.LinkedList;
import java.util.Scanner;
​
public class Main {
    public static void main(String&#91;] args) {
        Scanner sc = new Scanner(System.in);
        String&#91;] arr = sc.nextLine().split(" ");
        LinkedList&lt;String> list = new LinkedList&lt;>();
        dfs(arr,list);
    }
    
    public static void dfs(String&#91;] arr,LinkedList&lt;String> list) {
        if(list.size()>=arr.length){
            System.out.println(list);
            return;
        }
        for(int i=0;i&lt;arr.length;i++){
            if(!list.contains(arr&#91;i])){
                list.add(arr&#91;i]);
                dfs(arr,list);
                list.removeLast();
            }
        }
    }    
}</code></pre>



<h2 class="wp-block-heading">斐波那契数列：</h2>



<pre class="wp-block-code"><code>import java.util.Scanner;
​
//斐波那契数列
public class Main {
    public static void main(String&#91;] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
​
        System.out.println(fb(n));
    }
​
    static int fb(int n) {
        if(n == 1||n == 2){
            return 1;
        }
        return fb(n-1) + fb(n-2);
    }
}</code></pre>



<h2 class="wp-block-heading">汉诺塔：</h2>



<pre class="wp-block-code"><code>import java.util.Scanner;
​
public class Main {
    public static void main(String&#91;] args) {
        Scanner sc = new Scanner(System.in);
​
        int n = sc.nextInt();
        printHanoiTower(n,"A","B","C");
​
    }
​
    static void printHanoiTower(int n,String from,String to,String help) {
        if(n==1) {
            System.out.println(n+":"+from+"->"+to);
        }else{
            printHanoiTower(n-1,from,help,to);  //将前N-1个盘子移至辅助空间
            System.out.println(n+":"+from+"->"+to);
            printHanoiTower(n-1,help,to,from);  //将N-1个盘子从辅助空间移回源空间
        }
    }
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e9%80%92%e5%bd%92%e7%ae%97%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>动态规划</title>
		<link>https://www.qiqin-chang.cn/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92/</link>
					<comments>https://www.qiqin-chang.cn/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:24:46 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=489</guid>

					<description><![CDATA[线性DP: 最长单调子序列： 以最最长递增子序列为例： 说明：输出最长递增子序列的长度，递增子序列为数组中单调 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">线性DP:</h2>



<h3 class="wp-block-heading">最长单调子序列：</h3>



<p>以最最长递增子序列为例：</p>



<p>说明：输出最长递增子序列的长度，递增子序列为数组中单调递增的元素的最大个数，元素不必相邻，但相对位置必须一致</p>



<pre class="wp-block-code"><code>import java.util.Scanner;
​
public class Main {
    public static void main(String&#91;] args) {
        //输入
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int&#91;]&#91;] arr = new int&#91;n]&#91;2];
        for (int i = 0; i &lt; n; i++) {
            arr&#91;i]&#91;0] = sc.nextInt();
        }
​
        //计算
        for (int i = 0; i &lt; n; i++) {
            arr&#91;i]&#91;1] = 1;
            for (int j = 0; j &lt; i; j++) {
                if(arr&#91;i]&#91;0] > arr&#91;j]&#91;0]){
                    arr&#91;i]&#91;1] = Math.max(arr&#91;i]&#91;1], arr&#91;j]&#91;1]+1); //赋值比当前元素小的前一个元素的长度+1与1之间的最大值
                }
            }
        }
​
        //获取最大递增子序列长度
        int max = 0;
        for (int i = 0; i &lt; n; i++) {
            max = Math.max(max, arr&#91;i]&#91;1]);
        }
        //输出
        System.out.println(max);
        
    }
}</code></pre>



<h3 class="wp-block-heading">最大连续子序列和：</h3>



<pre class="wp-block-code"><code>import java.util.Scanner;
​
//最大连续子序列和
//测试用例 -2,11,-4,13,-5,-2
public class study05 {
    public static void main(String&#91;] args) {
        //输入
        Scanner sc = new Scanner(System.in);
        String&#91;] s = sc.nextLine().split(",");
        int&#91;] a = new int&#91;s.length+1];
        a&#91;0] = 0;
        for(int i=1;i&lt;=s.length;i++){
            a&#91;i] = Integer.parseInt(s&#91;i-1]);
        }
​
        //计算
        int&#91;] dp = new int&#91;a.length];
        for(int i=1;i&lt;s.length;i++){
            dp&#91;i] = 0;
            //转移方程
            dp&#91;i] = Math.max(dp&#91;i]+a&#91;i],a&#91;i]);
        }
​
        //输出
        int max = 0;
        for(int i=1;i&lt;a.length;i++){
            if(dp&#91;i]>max){
                max = dp&#91;i];
            }
        }
        System.out.println(max);
    }
}</code></pre>



<h3 class="wp-block-heading">最长公共子序列：(双序列)（二维dp）</h3>



<pre class="wp-block-code"><code>import java.util.Scanner;
​
//最长公共子序列
//测试用例 BDCABC ABCBDAB 
public class Main {
    public static void main(String&#91;] args) {
        //输入
        Scanner sc = new Scanner(System.in);
        String&#91;] s1 = sc.nextLine().split("");
        String&#91;] s2 = sc.nextLine().split("");
        int&#91;]&#91;] arr = new int&#91;s1.length+1]&#91;s2.length+1];
        //计算
        for(int i=1;i&lt;arr.length;i++){
            for(int j=1;j&lt;arr&#91;i].length;j++){
                //转移方程
                if(s1&#91;i-1].equals(s2&#91;j-1])){
                    arr&#91;i]&#91;j] = arr&#91;i-1]&#91;j-1]+1;
                }else {
                    arr&#91;i]&#91;j] = Math.max(arr&#91;i-1]&#91;j], arr&#91;i]&#91;j-1]);
                }
            }
        }
        //输出
        System.out.println(arr&#91;arr.length-1]&#91;arr&#91;0].length-1]);
    }
}</code></pre>



<h2 class="wp-block-heading">背包DP：</h2>



<h3 class="wp-block-heading">01背包：</h3>



<pre class="wp-block-code"><code>//01背包
public class Main {
    public static void main(String&#91;] args) {
        //输入
        int&#91;] w = {2,3,4}; //物品重量
        int&#91;] v = {3,5,6}; //物品价值
        int n = 6; //背包容量
​
        int&#91;]&#91;] arr = new int&#91;w.length+1]&#91;n+1];
​
        //计算
        for (int i = 1; i &lt; w.length+1; i++) {
            for (int j = 1; j &lt; n+1; j++) {
                //转移方程
                if (j&lt;w&#91;i-1]) {
                    arr&#91;i]&#91;j]=arr&#91;i-1]&#91;j];
                }else{
                    arr&#91;i]&#91;j]=Math.max(arr&#91;i-1]&#91;j],arr&#91;i-1]&#91;j-w&#91;i-1]]+v&#91;i-1]);
                }
            }
        }
​
        //输出
        System.out.println(arr&#91;w.length]&#91;n]);
    }
}</code></pre>



<h3 class="wp-block-heading">完全背包：</h3>



<pre class="wp-block-code"><code>//完全背包
public class Main {
    public static void main(String&#91;] args) {
        //输入
        int&#91;] w = {2,3,4}; //物品重量
        int&#91;] v = {3,5,6}; //物品价值
        int n = 6; //背包容量
​
        int&#91;]&#91;] arr = new int&#91;w.length+1]&#91;n+1];
​
        //计算
        for (int i = 1; i &lt; w.length+1; i++) {
            for (int j = 1; j &lt; n+1; j++) {
                //转移方程
                if (j&lt;w&#91;i-1]) {
                    arr&#91;i]&#91;j]=arr&#91;i-1]&#91;j];
                }else{
                    arr&#91;i]&#91;j]=Math.max(arr&#91;i-1]&#91;j],arr&#91;i]&#91;j-w&#91;i-1]]+v&#91;i-1]);
                }
            }
        }
​
        //输出
        System.out.println(arr&#91;w.length]&#91;n]);
    }
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>数学计算</title>
		<link>https://www.qiqin-chang.cn/%e6%95%b0%e5%ad%a6%e8%ae%a1%e7%ae%97/</link>
					<comments>https://www.qiqin-chang.cn/%e6%95%b0%e5%ad%a6%e8%ae%a1%e7%ae%97/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:20:19 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=487</guid>

					<description><![CDATA[素数判断： 最大公约数+最小公倍数：]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">素数判断：</h2>



<pre class="wp-block-code"><code>public class Main {
    public static void main(String&#91;] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int flag = 1;
        if (n &lt; 2) {
            flag = 0;
        }else{
            for (int i = 2; i &lt;= Math.sqrt(n); i++) {
                if (n % i == 0) {
                    flag = 0;
                }
            }
        }
        if (flag == 1) {
            System.out.println("这是素数");
        }else{
            System.out.println("这不是素数");
        }
    }
}</code></pre>



<h2 class="wp-block-heading">最大公约数+最小公倍数：</h2>



<pre class="wp-block-code"><code>import java.util.Scanner;
​
//辗转相除法
public class Main {
    public static void main(String&#91;] args) {
        Scanner sc = new Scanner(System.in);
        //除数/被除数=商+余数
        int A = sc.nextInt();
        int B = sc.nextInt();
        int a = A;
        int b = B;
        while (b != 0) {
            int temp = a;
            a = b;
            b = temp%a;
        }
        
        //最大公约数=A
        System.out.println("最大公约数是："+ a);
        int c = (A*B)/a;
        //最小公倍数=A*B/最大公约数
        System.out.println("最小公倍数是："+ c);
    }
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e6%95%b0%e5%ad%a6%e8%ae%a1%e7%ae%97/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>排序算法</title>
		<link>https://www.qiqin-chang.cn/%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95/</link>
					<comments>https://www.qiqin-chang.cn/%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:18:01 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=485</guid>

					<description><![CDATA[冒泡排序： 说明：以此比较相邻元素，并将较大元素移至后方 选择排序： 说明：每次选择一个最小元素与未排序元素首 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">冒泡排序：</h2>



<p>说明：以此比较相邻元素，并将较大元素移至后方</p>



<pre class="wp-block-code"><code>public class BubbleSort {
    public static void bubbleSort(int&#91;] arr) {
        int n = arr.length;
        for (int i = 0; i &lt; n - 1; i++) {
            for (int j = 0; j &lt; n - i - 1; j++) {
                if (arr&#91;j] > arr&#91;j + 1]) {
                    int temp = arr&#91;j];
                    arr&#91;j] = arr&#91;j + 1];
                    arr&#91;j + 1] = temp;
                }
            }
        }
    }
​
    public static void main(String&#91;] args) {
        int&#91;] arr = {5, 4, 3, 2, 1};
        bubbleSort(arr);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}</code></pre>



<h2 class="wp-block-heading">选择排序：</h2>



<p>说明：每次选择一个最小元素与未排序元素首部进行替换</p>



<pre class="wp-block-code"><code>public class SelectionSort {
    public static void selectionSort(int&#91;] arr) {
        int n = arr.length;
        for (int i = 0; i &lt; n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j &lt; n; j++) {
                if (arr&#91;j] &lt; arr&#91;minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr&#91;minIndex];
            arr&#91;minIndex] = arr&#91;i];
            arr&#91;i] = temp;
        }
    }
​
    public static void main(String&#91;] args) {
        int&#91;] arr = {3, 2, 5, 4, 1};
        selectionSort(arr);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}</code></pre>



<h2 class="wp-block-heading">插入排序：</h2>



<p>说明：每次将未排序的首元素插入已排序的队列中</p>



<pre class="wp-block-code"><code>public class InsertionSort {
  
    public static int&#91;] insertionSort(int&#91;] a){
        if(a.length&lt;=1) return a;
        for(int i = 1; i&lt;a.length; ++i){
            int value = a&#91;i];
            int j = i-1;
            //查找插入的位置
            while(j>=0&amp;&amp;value&lt;a&#91;j]){
                a&#91;j+1] = a&#91;j];
                j--;
            }
            a&#91;j+1] = value;
        }
        return a;
    }
​
    public static void main(String&#91;] args) {
        int&#91;] a = {10, 9, 7, 4, 3, 2, 1, 8};
        System.out.println("之前的排序：");
        System.out.println(Arrays.toString(a));
​
        insertionSort(a);
        System.out.println("之后的排序：");
        System.out.println(Arrays.toString(a));
    }
}</code></pre>



<h2 class="wp-block-heading">希尔排序：</h2>



<pre class="wp-block-code"><code>import java.util.Arrays;
​
public class Main {
​
    public static int&#91;] shellSort(int&#91;] a){
        //不断缩小增量
        for(int x=a.length/2;x>0;x=x/2){
            //增量为1的插入排序
            for(int i=x; i&lt;a.length; i++){
                int value = a&#91;i];
                int j = i-x;
                //查找插入的位置
                while(j>=0&amp;&amp;value&lt;a&#91;j]){
                    a&#91;j+x] = a&#91;j];
                    j-=x;
                }
                a&#91;j+x] = value;
            }
        }
​
        return a;
    }
​
    public static void main(String&#91;] args) {
        int&#91;] a = {10, 9, 7, 4, 3, 2, 1, 8};
        System.out.println("之前的排序：");
        System.out.println(Arrays.toString(a));
​
        shellSort(a);
        System.out.println("之后的排序：");
        System.out.println(Arrays.toString(a));
    }
}</code></pre>



<h2 class="wp-block-heading">计数排序：</h2>



<p>说明：使用位置+偏移量的方式，用数组确定位置用出现次数表示偏移量，逆序确定每个数字的位置</p>



<pre class="wp-block-code"><code>//计数排序
public class Main {
    public static void main(String&#91;] args) {
        int&#91;] a = {8, 10, 12, 9, 8, 12, 8}; //输入的数组
        int&#91;] r = new int&#91;a.length];        //输出的数组
        int min = Integer.MAX_VALUE;        //数组中的最小值
        int max = Integer.MIN_VALUE;        //数组中的最大值
        for (int i = 0; i &lt; a.length; i++) {
            min = Math.min(min, a&#91;i]);
            max = Math.max(max, a&#91;i]);
        }
        int&#91;] s = new int&#91;max - min + 1];   //计数数组 统计每个数字出现的次数
        for (int i = 0; i &lt; a.length; i++) {
            s&#91;a&#91;i]-min]++;
        }
​
        int&#91;] s1 = new int&#91;max - min + 1];  //排序数组 表示数字应在排序后数组的位置 当前值=前一位的值+计数数组的值
        s1&#91;0]=s&#91;0];
        for (int i = 1; i &lt; s.length; i++) {
            s1&#91;i]=s1&#91;i-1]+s&#91;i];
        }
        
        //逆序填写数字到相应位置
        for (int i = a.length-1; i >=0; i--) {
            r&#91;s1&#91;a&#91;i]-min]-1]=a&#91;i];
            s1&#91;a&#91;i]-min]--;
        }
​
        //输出数组
        for(int j=0;j&lt;r.length;j++){
            System.out.print(r&#91;j]+" ");
        }
    }
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>查找算法</title>
		<link>https://www.qiqin-chang.cn/%e6%9f%a5%e6%89%be%e7%ae%97%e6%b3%95/</link>
					<comments>https://www.qiqin-chang.cn/%e6%9f%a5%e6%89%be%e7%ae%97%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[乐章]]></dc:creator>
		<pubDate>Tue, 03 Jun 2025 04:08:54 +0000</pubDate>
				<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.qiqin-chang.cn/?p=480</guid>

					<description><![CDATA[顺序查找： 二分查找：]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">顺序查找：</h2>



<pre class="wp-block-code"><code>public class Main {
    public static void main(String&#91;] args) {
        int&#91;] arr = {1,2,3,4,5,6,7,8,9};
        System.out.println(SequentialSearch(arr,6));
    }
    
    //顺序查找
    static int SequentialSearch(int&#91;] arr,int target){
        for(int i=0;i&lt;arr.length;i++){
            if(arr&#91;i]==target){
                return i;
            }
        }
        return -1;
    }
}</code></pre>



<h2 class="wp-block-heading">二分查找：</h2>



<pre class="wp-block-code"><code>public class Main {
    public static void main(String&#91;] args) {
        int&#91;] arr = {1,2,3,4,5,6,7,8,9};
        System.out.println(binarySearchBasic(arr,7));
        System.out.println(binarySearch(arr,10));
    }
    /**
     * 二分查找基础版
     * Params:arr-待查找的升序数组  target-待查找的目标值
     * Return:找到则返回索引       找不到返回-1
     */
    public static int binarySearchBasic(int&#91;] arr,int target){
        int i=0,j=arr.length-1;//设置指针和初值 1.i=0，j=arr.length;
        while(i&lt;=j){//范围内有东西2.(i&lt;j)
            int m = (i+j)>>>1;
            if(target&lt;arr&#91;m]){//目标在左边
                j=m-1;//3.j=m
            }else if(arr&#91;m]&lt;target){//目标在右边
                i=m+1;
            }else{
                return m;
            }
        }
        return -1;
    }
​
    //大量数据改进版
    public static int binarySearch(int&#91;] arr, int target) {
        int i=0,j=arr.length;
        while(j-i>1){
            int m=(i+j)>>>1;
            if(target&lt;arr&#91;m]){
                j=m;
            }else{
                i=m+1;
            }
        }
        if(target==arr&#91;i]){
            return i;
        }else{
            return -1;
        }
    }
}</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.qiqin-chang.cn/%e6%9f%a5%e6%89%be%e7%ae%97%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
