• C# 四种排序(没有写希尔排序)


     protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        //它的原理就是相邻的两个两个的比较,如果前面的数比后面的大,那么交换,它这个在比较完一次的时候可以得到最大的一个数,然后接着循环,每次外循环中内循环的次数比原来少一次。
        protected void btn_MaoPao_Click(object sender, EventArgs e)
        {
            int[] arr = new int[] { 56, 12, 34, 45, 22, 34, 56, 23, 45, 67 };
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr.Length - i - 1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            for (int k = 0; k < arr.Length; k++)
            {
                Response.Write(arr[k].ToString() + "<br />");
            }
        }
        //选择排序  向后冒泡获取最小值得索引保存最小值MinIndex  ,然后和第i个值进行数据交换
        protected void btn_Select_Click(object sender, EventArgs e)
        {
            int[] arr = new int[] { 56, 12, 34, 45, 22, 34, 56, 23, 45, 67 };
            int minIndex = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                minIndex = i;
                for (int j = i; j < arr.Length; j++)
                {
                    if (arr[j] < arr[minIndex])
                    {
                        minIndex = j;
                    }
                }
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
            for (int k = 0; k < arr.Length; k++)
            {
                Response.Write(arr[k].ToString() + "<br />");
            }
        }
        //插入排序
        //此种算法是一次循环每一个元素,然后在每一个为基值再一次循环小于基值的每一个元素,
        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            int[] arr = new int[] { 56, 12, 34, 45, 22, 34, 56, 23, 45, 67 };
            for (int i = 0; i < arr.Length; i++)
            {
                int temp = arr[i];
                int j = i;
                while (j > 0 && arr[j - 1] > temp)
                {
                    arr[j] = arr[j - 1];
                    --j;
                }
                arr[j] = temp;
            }
            for (int k = 0; k < arr.Length; k++)
            {
                Response.Write(arr[k].ToString() + "<br />");
            }
        }
        protected void btn_Fast_Click(object sender, EventArgs e)
        {
            int[] arr = new int[] { 56, 12, 34, 45, 22, 34, 56, 23, 45, 67 };
            quick_sort(arr, 0, arr.Length - 1);
            for (int k = 0; k < arr.Length; k++)
            {
                Response.Write(arr[k].ToString() + "<br />");
            }
        }
        //快速排序   //从右向左找第一个小于x的数   //从左向右找第 一个大于x的数
        private void quick_sort(int[] s, int l, int r)
        {
             if (l < r)
            {
                int i = l, j = r, x = s[l];
                while (i < j)
                {
                    while (i < j && s[j] >= x)//从右向左找第一个小于x的数
                    {
                        j--;
                    }
                    if (i < j)
                    {
                        s[i] = s[j];
                        i++;
                    }
                    while (i < j && s[i] < x)//从左向右找第 一个大于x的数
                    {
                        i++;
                    }
                    if (i < j)
                    {
                        s[j] = s[i];
                        j--;
                    }
                }
                s[i] = x;
                quick_sort(s, l, i - 1);
                quick_sort(s, i + 1, r);
            }
  • 相关阅读:
    poj 1321 棋盘问题 (dfs)
    poj 3274 Gold Balanced Lineup(哈希 )
    poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
    zoj 3351 Bloodsucker(概率 dp)
    poj 1840 Eqs (hash)
    poj 2418 Hardwood Species (map)
    poj 2151 Check the difficulty of problems(概率dp)
    poj 2442 Sequence(优先队列)
    poj 1442 Black Box(堆 优先队列)
    两个STL网址 总结的很好 && c++堆的网址
  • 原文地址:https://www.cnblogs.com/TNSSTAR/p/2955660.html
Copyright © 2020-2023  润新知