用C#编写的冒泡排序小程序

来源:互联网 发布:javlibrary永久域名 编辑:程序博客网 时间:2024/06/10 10:19

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace order
{
    public partial class FormOrder : Form
    {
        public FormOrder()
        {
            InitializeComponent();
        }
      
        private void buttonOrder_Click(object sender, EventArgs e)
        {
            int[] myArray = new int[] { 10, 8, -3, 5, 6, 7, 4, 4, 9 };
            String temp = "";
            for(int i=0; i<myArray.Length ; i++)
            {
                temp = temp +" "+ myArray[i].ToString();
            }
            textBoxAssay.Text = temp;

            for (int i = 1; i < myArray.Length - 1; i++)
            {
                for (int j = 0; j < myArray.Length - i; j++)
                {
                    if (myArray[j] > myArray[j + 1])
                    {
                        int temp2 = myArray[j];
                        myArray[j] = myArray[j + 1];
                        myArray[j + 1] = temp2;
                    }
                }
            }

            String temp1 = "";
            for (int i = 0; i < myArray.Length; i++)
            {
                temp1 = temp1 + " " + myArray[i].ToString();
            }
            textBoxShow.Text = temp1;

        }

        private void buttonSort_Click(object sender, EventArgs e)
        {
            String words = textBoxInput.Text;
            char[] wordsTemp = words.ToCharArray();
            int num = 0;
            int[] myArray = new int[8];
            String temp = "";
            char c;
         
            for (int i = 0; i < wordsTemp.Length; i++)
            {
                c = wordsTemp[i];
                temp = temp + wordsTemp[i].ToString();
                if (c == ' ')
                {
                    myArray[num++] = Int32.Parse(temp);
                    temp = "";
                }
            }

            for (int i = 1; i < myArray.Length - 1; i++)
            {
                for (int j = 0; j < myArray.Length - i; j++)
                {
                    if (myArray[j] > myArray[j + 1])
                    {
                        int temp2 = myArray[j];
                        myArray[j] = myArray[j + 1];
                        myArray[j + 1] = temp2;
                    }
                }
            }

            String temp1 = "";
            for (int i = 0; i < myArray.Length; i++)
            {
                temp1 = temp1 + " " + myArray[i].ToString();
            }
            textBoxOutput.Text = temp1;
        }

        private void textBoxInput_TextChanged(object sender, EventArgs e)
        {

        }


    }

原创粉丝点击