• 基本排序算法


          没有过多的说明,只为将来可能会用到的日子,能拿来做参考。

    1、冒泡(Bubble)

            7 5 2 1 5 9
            5 7 2 1 5 9
            5 2 7 1 5 9
            5 2 1 7 5 9
            5 2 1 5 7 9

            2 5 1 5 7 9
            2 1 5 5 7 9
            2 1 5 5 7 9

            1 2 5 5 7 9

    复制代码
            public static void BubbleSort(int[] a)
            {
                int n = a.Length - 1;//个数
                int i, j;//用于控制循环
                int tmp;//中间变量
                bool isChange;//记录冒泡一轮下来是否发生交换(如果没有发生交换证明已排好序)
                for (i = 0; i < n; i++)
                {
                    isChange = false;
                    for (j = 0; j < n - i; j++)//每轮冒泡过后要对比的数就少一个
                    {
                        if (a[j] > a[j + 1])//大的数向上冒
                        {
                            tmp = a[j];
                            a[j] = a[j + 1];
                            a[j + 1] = tmp;
                            isChange = true;
                        }
                    }
                    if (!isChange)
                    {
                        return;
                    }
    
                }
                return;
            }
    复制代码

    2、直接插入(Insert)

            7 5 2 1 5 9
            5 7 2 1 5 9
            2 5 7 1 5 9
            1 2 5 7 5 9
            1 2 5 5 7 9

    复制代码
            public static void InsertSort(int[] a)
            {
                int n = a.Length;
                for (int i = 1; i < n; i++)//循环从第二个数组元素开始,因为arr[0]作为最初已排序部分
                {
                    int temp = a[i];//temp标记为未排序第一个元素
                    int j = i - 1;
                    while (j >= 0 && a[j] > temp)//将temp与已排序元素从小到大比较,寻找temp应插入的位置
                    {
                        a[j + 1] = a[j];
                        j--;
                    }
                    a[j + 1] = temp;
                }
            }
    复制代码

    3、直接选择(Select)

            7 5 2 1 5 9
            1 5 2 7 5 9
            1 2 5 7 5 9
            1 2 5 7 5 9
            1 2 5 5 7 9

    复制代码
            public static void SelectSort(int[] a)
            {
                int n = a.Length;//个数
                int i, j;
                int tmp;
                int b;
                for (i = 0; i < n - 1; i++)
                {
                    tmp = i;
                    for (j = i + 1; j < n; j++)
                    {
                        if (a[tmp] > a[j])//每轮找到最小的元素
                            tmp = j;
                    }
                    if (i != tmp)
                    {
                        b = a[tmp];
                        a[tmp] = a[i];
                        a[i] = b;
                    }
                }
            }
    复制代码

    4、快速(Quick)

            7       5 2 1 5 9
            5   2 1 5     7 9
            2 1 5   5     7 9
            1 2 5   5     7 9

    View Code

    5、希尔(Shell)

            7 5 2 1 5 9
            7 5 2   1 5 9
            7 5   2   1 5  9
            5 7   2   1 5  9
            2 5   7   1 5  9
            2 5 7   1 5 9
            1 2 5 7 5 9
            1 2 5 5 7 9

    View Code

    6、归并(Merge)

            7 5 2 1 5 9
            7 5 2    1 5 9
            7   5 2   1   5 9
            7   2 5   1   5 9
            2 5 7    1 5 9
            1 2 5 5 7 9

    View Code

    7、堆(Heap)

            7 5 2 1 5 9
                7
               /
              5   2
             / /
             1 5 9

                9
               /
              5   7
             / /
             1 5 2

    View Code
    转自:http://www.cnblogs.com/tim-li/p/3327895.html
  • 相关阅读:
    2.Android之按钮Button和编辑框EditText学习
    《DSP using MATLAB》Problem 3.8
    《DSP using MATLAB》Problem 3.7
    《DSP using MATLAB》Problem 3.6
    《DSP using MATLAB》Problem 3.5
    《DSP using MATLAB》Problem 3.4
    《DSP using MATLAB》Problem 3.3
    《DSP using MATLAB》Problem 3.2
    《DSP using MATLAB》Problem 3.1
    《DSP using MATLAB》Problem 2.20
  • 原文地址:https://www.cnblogs.com/double405/p/5144700.html
Copyright © 2020-2023  润新知