C#冒泡法对数组进行排序

从textbox2.读取字符串,然后利用string.split把字符串分割为一个字符数组,再转化成int数组。winform下例程如下

private void button3_Click(object sender, EventArgs e)
        {
            string[] s = textBox2.Text.Trim().Split(',');
            int[] arri = new int[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                arri[i] = Int32.Parse(s[i]);
            }
            int[] ai = mp(arri);
            string str = "";
            foreach (int i in ai)
            {
                str += Convert.ToString(i);

            }
            textBox3.Text = str;
        }
        public int[] mp(int[] arr)//接收一个int数组进行冒泡排序
        {
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[j - 1])
                    {
                        int temp = arr[j];
                        arr[j] = arr[j - 1];
                        arr[j - 1] = temp;
                    }
                }
            }
            return arr;
        }


你可能感兴趣的:(C#)