c#中的排序算法(动态数组ArrayList)

private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan s = new TimeSpan();
            DateTime dt = DateTime.Now;
            SortingAlgorithm sort = new SortingAlgorithm();
            ArrayList iArrary = new ArrayList(1);
            Random r = new Random();
            for (int i = 0; i < 100; i++)
            {
                iArrary.Add(r.Next(1000));
            }
            //int[] iArrary = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
            richTextBox1.Text += "未排序数据: ";
            foreach (int i in iArrary)
            {
                richTextBox1.Text += i.ToString() + "   ";
            }
            ///冒泡排序
            sort.BubbleSorter(iArrary);
            richTextBox1.Text += "/n冒泡 排 序: ";
            foreach (int i in iArrary)
            {
                richTextBox1.Text += i.ToString() + "   ";
            }
            ///选择排序
            sort.SelectionSorter(iArrary);
            richTextBox1.Text += "/n选择 排 序: ";
            foreach (int i in iArrary)
            {
                richTextBox1.Text += i.ToString() + "   ";
            }
            ///插入排序
            sort.InsertionSorter(iArrary);
            richTextBox1.Text += "/n插入 排 序: ";
            foreach (int i in iArrary)
            {
                richTextBox1.Text += i.ToString() + "   ";
            }

            ///希尔排序
            sort.ShellSorter(iArrary);
            richTextBox1.Text += "/n希尔 排 序: ";
            foreach (int i in iArrary)
            {
                richTextBox1.Text += i.ToString() + "   ";
            }
            s = DateTime.Now - dt;
            richTextBox1.Text += "/n用时:" + s.TotalMilliseconds + " 毫秒";
        }

 

/*
 * C#中排序算法
 * 2009年7月27日
 * CEDAR(雪中松)
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace HC.Common
{
    public class SortingAlgorithm
    {
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="list"></param>
        public void BubbleSorter(ArrayList list)
        {
            int i, j, temp;
            bool done = false;
            j = 1;
            while ((j < list.Count) && (!done))
            {
                done = true;
                for (i = 0; i < list.Count - j; i++)
                {
                    if( Convert.ToInt32(list[i]) >Convert.ToInt32(list[i + 1]))
                    {
                        done = false;
                        temp = Convert.ToInt32(list[i]);
                        list[i] = Convert.ToInt32(list[i + 1]);
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
        }
        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="list"></param>
        public void SelectionSorter(ArrayList list)
        {
            int min;
            for (int i = 0; i < list.Count - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < list.Count; j++)
                {
                    if ( Convert.ToInt32(list[j]) <  Convert.ToInt32(list[min]))
                        min = j;
                }

                int t =  Convert.ToInt32(list[min]);
                list[min] =  Convert.ToInt32(list[i]);
                list[i] = t;
            }
        }
        /// <summary>
        /// 插入排序
        /// </summary>
        /// <param name="list"></param>
        public void InsertionSorter(ArrayList list)
        {
            int i, j, temp;
            bool done = false;
            j = 1;
            while ((j < list.Count) && (!done))
            {
                done = true;
                for (i = 0; i < list.Count - j; i++)
                {
                    if ( Convert.ToInt32(list[i]) >  Convert.ToInt32(list[i + 1]))
                    {
                        done = false;
                        temp =  Convert.ToInt32(list[i]);
                        list[i] =  Convert.ToInt32(list[i + 1]);
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
        }
        /// <summary>
        /// 希尔排序
        /// </summary>
        /// <param name="list"></param>
        public void ShellSorter(ArrayList list)
        {
            int inc;
            for (inc = 1; inc <= list.Count / 9; inc = 3 * inc + 1) ;
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= list.Count; i += inc)
                {
                    int t =  Convert.ToInt32(list[i - 1]);
                    int j = i;
                    while ((j > inc) && ( Convert.ToInt32(list[j - inc - 1]) > t))
                    {
                        list[j - 1] =  Convert.ToInt32(list[j - inc - 1]);
                        j -= inc;
                    }
                    list[j - 1] = t;
                }
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值