<?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>算法 归档 - 初晴神社</title>
	<atom:link href="https://vki.cc/tag/%e7%ae%97%e6%b3%95/feed/" rel="self" type="application/rss+xml" />
	<link>https://vki.cc/tag/算法/</link>
	<description>白露的博客</description>
	<lastBuildDate>Sun, 13 Jun 2021 15:26:53 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>

<image>
	<url>https://cdn.vki.cc/wp-content/uploads/2018/03/cropped-icemaplen-1-32x32.png</url>
	<title>算法 归档 - 初晴神社</title>
	<link>https://vki.cc/tag/算法/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>一些排序算法源码</title>
		<link>https://vki.cc/greattruth/experience/385/</link>
					<comments>https://vki.cc/greattruth/experience/385/#comments</comments>
		
		<dc:creator><![CDATA[白露]]></dc:creator>
		<pubDate>Mon, 12 Mar 2018 04:06:43 +0000</pubDate>
				<category><![CDATA[咸鱼心得]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[算法]]></category>
		<guid isPermaLink="false">http://www.icemaplen.com/?p=385</guid>

					<description><![CDATA[<p>快速排序 归并排序 插入排序 冒泡排序 堆排序 希尔排序</p>
<p><a href="https://vki.cc/greattruth/experience/385/">一些排序算法源码</a>最先出现在<a href="https://vki.cc">初晴神社</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><img  fetchpriority="high"  decoding="async"  width="966"  height="588"  src="https://oss.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869.png"  alt=""  class="wp-image-866"  srcset="https://cdn.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869.png 966w, https://cdn.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869-800x487.png 800w, https://cdn.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869-120x73.png 120w, https://cdn.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869-90x55.png 90w, https://cdn.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869-320x195.png 320w, https://cdn.vki.cc/wp-content/uploads/2021/06/1623597968-849589-20171015233043168-1867817869-560x341.png 560w"  sizes="(max-width: 966px) 100vw, 966px" ></figure>



<figure class="wp-block-image size-large"><img  decoding="async"  width="528"  height="363"  src="https://oss.vki.cc/wp-content/uploads/2018/03/1623597981-849589-20171015233220637-1055088118.png"  alt=""  class="wp-image-867" ></figure>



<h2 id="%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f" class="wp-block-heading">快速排序</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_快速排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] data = { 123, 654, 21, 40, 4, 32, 7, 57, 575, 49, 16, 549, 648, 52, 49, 21, 897, 21, 1 };
            QuickSort(data);

            foreach (int i in data)
            {
                Console.Write(i.ToString() + "\t");
            }

            Console.ReadKey();
        }

        static void QuickSort&lt;T>(T[] data)
            where T : IComparable
        {
            if (data == null)
            {
                return;
            }

            QuickSortCore(data, 0, data.Length - 1);
        }

        static void QuickSortCore&lt;T>(T[] data, int beginIndex, int endIndex)
            where T : IComparable
        {
            if (beginIndex &lt; endIndex)
            {
                int begin = beginIndex;
                int end = endIndex;
                T baseT = data[begin];
                while (begin &lt; end)
                {
                    while (begin &lt; end &amp;&amp; baseT.CompareTo(data[end]) &lt;= 0)
                    {
                        end--;
                    }
                    if (begin &lt; end)
                    {
                        data[begin++] = data[end];
                    }

                    while (begin &lt; end &amp;&amp; baseT.CompareTo(data[begin]) > 0)
                    {
                        begin++;
                    }
                    if (begin &lt; end)
                    {
                        data[end--] = data[begin];
                    }
                }

                data[begin] = baseT;

                QuickSortCore(data, beginIndex, begin - 1);
                QuickSortCore(data, begin + 1, endIndex);
            }
        }
    }
}
</pre>



<h2 id="%e5%bd%92%e5%b9%b6%e6%8e%92%e5%ba%8f" class="wp-block-heading">归并排序</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02_归并排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 31, 4, 691, 674, 16, 165, 16, 14, 1, 3, 45, 849, 68, 64, 645, 79, 17 };
            MergeSort(array);
            foreach (int n in array)
            {
                Console.WriteLine(n);
            }

            Console.ReadKey();
        }

        private static void MergeSort&lt;T>(T[] data)
            where T : IComparable
        {
            if (data == null)
            {
                return;
            }

            T[] temp = new T[data.Length];
            MergeSortCore(data, 0, data.Length - 1, temp);
        }

        static void MergeSortCore&lt;T>(T[] data, int beginIndex, int endIndex, T[] temp)
            where T : IComparable
        {
            if (beginIndex &lt; endIndex)
            {
                int mid = (beginIndex + endIndex) / 2;
                MergeSortCore(data, beginIndex, mid, temp);
                MergeSortCore(data, mid + 1, endIndex, temp);
                MergeArray(data, beginIndex, mid, endIndex, temp);
            }
        }

        static void MergeArray&lt;T>(T[] data, int beginIndex, int midIndex, int endIndex, T[] temp)
            where T:IComparable
        {
            int i = beginIndex, j = midIndex + 1;
            int m = midIndex, n = endIndex;
            int index = 0;

            while (i &lt;= m &amp;&amp; j &lt;= n)
            {
                temp[index++] = data[i].CompareTo(data[j]) &lt; 0 ? data[i++] : data[j++];
            }

            while (i &lt;= m)
            {
                temp[index++] = data[i++];
            }

            while (j &lt;= n)
            {
                temp[index++] = data[j++];
            }

            for (i = 0; i &lt; index; i++)
            {
                data[beginIndex + i] = temp[i];
            }
        }
    }
}</pre>



<h2 id="%e6%8f%92%e5%85%a5%e6%8e%92%e5%ba%8f" class="wp-block-heading">插入排序</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03_插入排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 31, 4, 691, 674, 16, 165, 16, 14, 1, 3, 45, 849, 68, 64, 645, 79, 17 };
            InsertSort(null);
            foreach (int n in array)
            {
                Console.WriteLine(n);
            }

            Console.ReadKey();
        }

        static void InsertSort&lt;T>(T[] data)
            where T:IComparable
        {
            if (data == null)
            {
                return;
            }

            for (int i = 1; i &lt; data.Length; i++)
            {
                T tempT = data[i];
                int j = i - 1;
                while (j >= 0 &amp;&amp; data[j].CompareTo(tempT) > 0)
                {
                    data[j + 1] = data[j];
                    j--;
                }
                data[j + 1] = tempT;
            }
        }
    }
}</pre>



<h2 id="%e5%86%92%e6%b3%a1%e6%8e%92%e5%ba%8f" class="wp-block-heading">冒泡排序</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04_冒泡排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 31, 4, 691, 674, 16, 165, 16, 14, 1, 3, 45, 849, 68, 64, 645, 79, 17 };
            BubbleSort(array);
            foreach (int n in array)
            {
                Console.WriteLine(n);
            }

            Console.ReadKey();
        }

        static void BubbleSort&lt;T>(T[] data)
            where T : IComparable
        {
            if (data == null)
            {
                return;
            }

            for (int i = 0; i &lt; data.Length - 1; i++)
            {
                for (int j = 0; j &lt; data.Length - i - 1; j++)
                {
                    if (data[j].CompareTo(data[j + 1]) > 0)
                    {
                        T temp = data[j];
                        data[j] = data[j + 1];
                        data[j + 1] = temp;
                    }
                }
            }
        }
    }
}
</pre>



<h2 id="%e5%a0%86%e6%8e%92%e5%ba%8f" class="wp-block-heading">堆排序</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05_堆排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] data = { 123, 654, 21, 40, 4, 32, 7, 57, 575, 49, 16, 549, 648, 52, 49, 21, 897, 21, 1 };
            HeapSort(data);

            foreach (int i in data)
            {
                Console.Write(i.ToString() + "\t");
            }

            Console.ReadKey();
        }

        static void HeapSort&lt;T>(T[] data)
            where T : IComparable
        {
            if (data == null)
            {
                return;
            }

            for (int i = data.Length / 2 - 1; i >= 0; i--)
            {
                AdjastHeap(data, i, data.Length);
            }

            for (int i = data.Length - 1; i > 0; i--)
            {
                T tempT = data[i];
                data[i] = data[0];
                data[0] = tempT;

                AdjastHeap(data, 0, i);
            }
        }

        static void AdjastHeap&lt;T>(T[] data, int index, int length)
            where T : IComparable
        {
            T tempT = data[index];
            for (int i = index * 2 + 1; i &lt; length; i = i * 2 + 1)
            {
                if (i + 1 &lt; length &amp;&amp; data[i + 1].CompareTo(data[i]) > 0)
                {
                    i++;
                }

                if (tempT.CompareTo(data[i]) &lt; 0)
                {
                    data[index] = data[i];
                    index = i;
                }
                else
                {
                    break;
                }
            }
            data[index] = tempT;
        }
    }
}
</pre>



<h2 id="%e5%b8%8c%e5%b0%94%e6%8e%92%e5%ba%8f" class="wp-block-heading">希尔排序</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06_希尔排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] data = { 123, 654, 21, 40, 4, 32, 7, 57, 575, 49, 16, 549, 648, 52, 49, 21, 897, 21, 1 };
            SeeleSort(data);

            foreach (int i in data)
            {
                Console.Write(i.ToString() + "\t");
            }

            Console.ReadKey();
        }

        static void SeeleSort&lt;T>(T[] data)
            where T : IComparable
        {
            int length = data.Length;
            for (int h = length / 2; h > 0; h = h / 2)
            {
                for (int i = h; i &lt; length; i++)
                {
                    T temp = data[i];
                    if (temp.CompareTo(data[i - h]) &lt; 0)
                    {
                        for (int j = 0; j &lt; i; j += h)
                        {
                            if (temp.CompareTo(data[j]) &lt; 0)
                            {
                                temp = data[j];
                                data[j] = data[i];
                                data[i] = temp;
                            }
                        }
                    }
                }
            }
        }
    }
}
</pre>
<p><a href="https://vki.cc/greattruth/experience/385/">一些排序算法源码</a>最先出现在<a href="https://vki.cc">初晴神社</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vki.cc/greattruth/experience/385/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
