水先生 發表於 2025-8-5 13:16:00

学习笔记:五种基础排序C语言实现

<h1 id="五种基础排序-升序实现">五种基础排序-升序实现</h1>
<h2 id="插入排序">插入排序</h2>
<p>构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。</p>
<pre><code class="language-c">
void InsertSort(int buf[], int bufsize)
{
    for (int i = 1; i &lt; bufsize; i++){
      int temp = buf;
      int j = i - 1;
      // 只移动,不插入
      while (j &gt;= 0 &amp;&amp; buf &gt; temp){
            buf = buf;
            j--;
      }
      // 统一插入:位置是 j+1
      buf = temp;
    }
}
</code></pre>
<hr>
<h2 id="冒泡排序">冒泡排序</h2>
<p>重复地走访要排序的数列,依次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复进行直到没有再需要交换,然后排序完成</p>
<pre><code class="language-c">void BubbleSort(int buf[], int bufsize)
{
        int Temp = 0;
        //i从1开始,表示第一轮比较
        for(int i=1;i&lt;bufsize;i++){
                //每轮比较完都会找到一个最大的数放在数组最后,下轮需要参与比较的数减低1
                for (int j=0;j&lt;bufsize-i;++j){

                        if(buf&gt;buf){
                                Temp = buf;
                                buf = buf;
                                buf = Temp;
                        }
                }
        }
}
</code></pre>
<hr>
<h2 id="选择排序">选择排序</h2>
<p>每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,放到已排序序列的末尾。直到所有元素均排序完毕。</p>
<pre><code class="language-c">void SelectSort(int buf[], int bufsize)
{
    //// 当只剩一个元素时,没比较对象,自身就是最小的,所以 n &lt; bufsize - 1,无需再比较
    for (int n = 0; n &lt; bufsize - 1; n++) {
      int index = n;// 假设当前无序部分的第一个元素(下标 n)是最小的
                        // index 用于记录当前找到的最小元素的下标
      for (int m = n + 1; m &lt; bufsize; m++) {
            // 如果发现比 buf 更小的元素
            if (buf &lt; buf) {
                index = m;
            }
      }
      // 使用临时变量安全交换
      if (index != n) {// 避免不必要的交换
            int temp = buf;
            buf = buf;
            buf = temp;
      }
    }
}
</code></pre>
<hr>
<h2 id="快速排序">快速排序</h2>
<p>把一个序列分为较小和较大的两个子序列,然后递归地排序两个子序列。具体过程是选择一个“基准”元素,将数组划分为比基准小的元素和比基准大的元素两部分,再对这两部分分别递归进行快速排序。</p>
<pre><code class="language-c">// buf[]: 待排序的数组
// low:当前排序区间的起始下标
// high: 当前排序区间的结束下标
void QuickSort(int buf[],int low,int high)
{       
        int l=low,h=high;
        int temp;
        // 递归终止条件:如果区间无效(low &gt;= high),直接返回
        if(low&lt;high)
        {
                temp=buf;// 选择第一个元素作为“基准值”
                while(l!=h)
                {
                        // 步骤1:从右往左找,跳过比基准大的元素
            // 直到找到一个小于等于基准的元素,准备“填坑”
                        while(l&lt;h&amp;&amp;buf&gt;temp)h--;
                        if(l&lt;h)
                        {
                                buf= buf;
                                ++l;
                        }
                        // 步骤2:从左往右找,跳过比基准小的元素
            // 直到找到一个大于等于基准的元素,准备“填坑”
                        while(l&lt;h&amp;&amp;buf&lt;temp)l++;
                        if(l&lt;h)
                        {
                                buf=buf;
                                --h;
                        }
                        // 如此反复,左右交替“填坑”,直到 l == h
                }
                buf = temp;
                // 此时,基准左边都小于它,右边都大于它
      // 递归对左右两个子区间进行快速排序
                QuickSort(buf,low,l-1);
                QuickSort(buf,l+1,high);
        }
}
</code></pre>
<hr>
<h2 id="计数排序">计数排序</h2>
<p>计数排序不是基于比较的排序算法,它的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。(该版本不适合有重复元素的稳定排序,且输出到B数组)</p>
<pre><code class="language-c">void CountSort(int A[],int B[],int size)
{
        int cnt;//用于统计A[]中比A小的元素个数
        for(int i = 0;i &lt; size;i++)
        {
                cnt = 0;
                for(int j=0;j&lt;size;j++)
                {
                        if(A&lt;A){
                                cnt++;
                        }
                }
      //A[]中比A小的元素个数为cnt个,A值就放到B
                B=A;
        }
}

</code></pre>
<hr>
<h2 id="测试">测试</h2>
<pre><code class="language-c">#include &lt;stdio.h&gt;

/**
        五种排序函数定义
*/

// 打印数组
void PrintArray(int arr[], int size) {
    for (int i = 0; i &lt; size; ++i) {
      printf("%d ", arr);
    }
    printf("\n");
}

int main(int argc, char const *argv[])
{
        int arr[] = {14, 23, 12, 5, 8, 3, 9, 10, 33, 6};
    int size = sizeof(arr) / sizeof(arr);

    printf("原始数组: ");
    PrintArray(arr, size);

    // 测试 InsertSort
    int arr1;
    for (int i = 0; i &lt; size; i++) arr1 = arr;
    InsertSort(arr1, size);
    printf("插入排序后: ");
    PrintArray(arr1, size);

    // 测试 BubbleSort
    int arr2;
    for (int i = 0; i &lt; size; i++) arr2 = arr;
    BubbleSort(arr2, size);
    printf("冒泡排序后: ");
    PrintArray(arr2, size);

    // 测试 SelectSort
    int arr3;
    for (int i = 0; i &lt; size; i++) arr3 = arr;
    SelectSort(arr3, size);
    printf("选择排序后: ");
    PrintArray(arr3, size);

    // 测试 QuickSort
    int arr4;
    for (int i = 0; i &lt; size; i++) arr4 = arr;
    QuickSort(arr4, 0, size - 1);
    printf("快速排序后: ");
    PrintArray(arr4, size);

    // 测试 CountSort
    int arr5;// 输出数组
    CountSort(arr, arr5, size);
    printf("计数排序后: ");
    PrintArray(arr5, size);

    return 0;
}
</code></pre>
<h3 id="测试结果">测试结果</h3>
<p><img src="https://img2024.cnblogs.com/blog/3677701/202508/3677701-20250805131626621-1350912943.png" alt="vmware_zLACP7zmGV" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/YueZone/p/19023092
頁: [1]
查看完整版本: 学习笔记:五种基础排序C语言实现