zoukankan      html  css  js  c++  java
  • 烙饼排序1(最基本的排序) 下

    我这个页面上有一个文本框一个按钮、一个listBox 点击按钮吧文本框中的字符串装换为一个列表  然后排序,最后把结果输出到listBox中,个人认为这个算法是最容易理解的,但是效率问题也是不高的

         List<int> intList = new List<int>();
            public frm_BingBaiXu()
            {
                InitializeComponent();
                textBox1.Text = "3216549870";
            }
    
            private void btn_cut_Click(object sender, EventArgs e)
            {
                FanZhuan();
            }
    
            #region 烙饼排序
            /// <summary>
            /// 烙饼排序
            /// 这个程序的思路是:
            /// 1.先找到最大的数,记录下他的索引
            /// 2.然后判断是不是就在最后位置
            /// 3.如果是最后位置不管它
            /// 4.如果不是那么判断它是不是第一位置
            /// 5.如果是第一位置不管他
            /// 6.如果不是第一位置那么,翻转它到第一位置
            /// 7.再把这个翻转到最后位置
            /// 8.进行下一次循环找除了最后一个中的第二打数重复上述操作
            /// 9.完成
            /// 这个程序最多是翻转(n-1)*2次,因为排序到倒数第二个的时候第一个也就排序好了
            /// </summary>
            /// <returns></returns>
            private List<int> FanZhuan()
            {
                InitInfo();
                bool isUpdate = false;
                ExportInfo();
                for (int i = 0; i < intList.Count; i++)
                {
                    int maxNum = intList[0];
                    int index = 0;
                    //找到最大的数
                    for (int j = 0; j < intList.Count - i; j++)
                    {
                        if (maxNum < intList[j])
                        {
                            maxNum = intList[j];
                            index = j;
                        }
                    }
                    //说明该数的位置不在未排序好中的最后一个
                    if (index != intList.Count - i)
                    {
                        if (index != 0)//说明不是第一个
                        {
                            isUpdate = false;
                            for (int j = 0; j < (index + 1) / 2; j++)
                            {
                                int temp;
                                temp = intList[j];
                                intList[j] = intList[index - j];
                                intList[index - j] = temp;
                                isUpdate = true;
                            }
                            if (isUpdate)
                            {
                                ExportInfo();
                            }
                        }
                        isUpdate = false;
                        for (int j = 0; j < (intList.Count - i) / 2; j++)
                        {
                            int temp;
                            temp = intList[j];
                            intList[j] = intList[intList.Count - i - j - 1];
                            intList[intList.Count - i - j - 1] = temp;
                            isUpdate = true;
                        }
                        if (isUpdate)
                        {
                            ExportInfo();
                        }
                    }
                }
                return intList;
            }
            #endregion
    
            #region 输出信息
            /// <summary>
            /// 输出信息
            /// </summary>
            private void ExportInfo()
            {
                string str;
                str = "";
                for (int j = 0; j < intList.Count; j++)
                {
                    str += intList[j] + "-";
                }
                listBox1.Items.Add(str);
            }
            #endregion
    
            #region 初始化信息
            /// <summary>
            /// 初始化信息
            /// </summary>
            private void InitInfo()
            {
                intList.Clear();
                for (int i = 0; i < textBox1.Text.Length; i++)
                {
                    intList.Add(Convert.ToInt16(textBox1.Text.Substring(i, 1)));
                }
                listBox1.Items.Clear();
            }
            #endregion
  • 相关阅读:
    iperf3命令使用
    python 使用多进程无法正常退出
    cfg 4 ocl
    opencv的CMakeLists.txt与makefile写法
    不需要打密码的sudo方法
    Fedora下rstudio-server安装
    Linux下突然不识别无线网卡
    Python使用opencv
    Python version 2.7 required, which was not found in the registry
    MySQL性能优化 — 实践篇1
  • 原文地址:https://www.cnblogs.com/haowuji/p/2840588.html
Copyright © 2011-2022 走看看