⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Visual C#2005程序设计教程
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinApp8_6数组排序
{
    public partial class Form1 : Form
    {
        int[] intArray ={ 34, 91, 83, 56, 29, 93, 56, 12, 88, 72 };
        string s;
        public Form1()
        {
            InitializeComponent();
        }

        public void Ascending(int[] array)
        {
            int temp;
            for (int i = 0; i < array.Length; i++)
                for (int j = i; j < array.Length; j++)
                    if (array[i] > array[j])
                    { temp = array[i]; array[i] = array[j]; array[j] = temp; }

        }

        public void descend(int[] array)
        {
            int temp;
            for (int i = 0; i < array.Length; i++)
                for (int j = i; j < array.Length; j++)
                    if (array[i] < array[j])
                    { temp = array[i]; array[i] = array[j]; array[j] = temp; }
        }

        //sort ascending(升序)descend(降序)
        private void Form1_Load(object sender, EventArgs e)
        {
            lblInfo.Text="原顺序:";s=lblInfo.Text;
            for (int i = 0; i < intArray.Length; i++)
            { lblInfo.Text += intArray[i] + " "; s += intArray[i] + " "; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lblInfo.Text = s+"\n\n  升序:";
            Ascending(intArray);
            for (int i = 0; i < intArray.Length; i++)
                lblInfo.Text += intArray[i] + " ";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lblInfo.Text = s + "\n\n  降序:";
            descend(intArray);
            for (int i = 0; i < intArray.Length; i++)
                lblInfo.Text += intArray[i] + " ";
            /*数组与数组列表的Sort方法*/
            ArrayList arrayList =new ArrayList();
            for (int i = 0; i < 5; i++)
                arrayList.Add((5 - i));
            arrayList.Sort();
            int[] array={1,2,4,3,5};
            Array.Sort(array);
            string s1=" ",s2=" ";
            for (int i = 0; i < arrayList.Count; i++)
            { s1 += arrayList[i].ToString() + " "; s2 += array[i] + " "; }
            MessageBox.Show(s1);
            MessageBox.Show(s2);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -