山东大学C#实验: 实验6 C#线程技术

实验内容:

编写选择、冒泡、插入三种排序算法,并采用多线程的方法比较三种算法的效率,程序执行的时间使用Stopwatch类打印出来.

实验代码:

using System;
using System.Threading;
using System.Diagnostics;

namespace test6
{
    //插入排序
    public class InsertionSorter
    {
        Stopwatch Time = new Stopwatch();
        public int[] list;
        public void Sort()
        {
            Time.Start();
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > t))
                {
                    list[j] = list[j - 1];
                    --j;
                }
                list[j] = t;
            }
            Time.Stop();
            Console.Write("Insertion done.");
            Console.WriteLine("程序的运行时间:{0} 毫秒", Time.Elapsed.TotalMilliseconds);
        }
    }
    //冒泡排序
    public class BubbleSorter
    {
        Stopwatch Time = new Stopwatch();
        public int[] list;
        public void Sort()
        {
            Time.Start();
            int i, j, temp;
            bool done = false;
            j = 1;
            while ((j < list.Length) && (!done))
            {
                done = true;
                for (i = 0; i < list.Length - j; i++)
                {
                    if (list[i] > list[i + 1])
                    {
                        done = false;
                        temp = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
            Time.Stop();
            Console.Write("Bubble done.");
            Console.WriteLine("程序的运行时间:{0} 毫秒", Time.Elapsed.TotalMilliseconds);
        }
    }
    //选择排序
    public class SelectionSorter
    {
        Stopwatch Time = new Stopwatch();
        private int min;
        public int[] list;
        public void Sort()
        {
            Time.Start();
            for (int i = 0; i < list.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (list[j] < list[min])
                        min = j;
                }
                int t = list[min];
                list[min] = list[i];
                list[i] = t;
            }
            Time.Stop();
            Console.Write("Select done.");
            Console.WriteLine("程序的运行时间:{0} 毫秒", Time.Elapsed.TotalMilliseconds);
        }
    }
    class MainClass
    {
        static void Main(string[] args)
        {
            InsertionSorter Sorter1 = new InsertionSorter();
            BubbleSorter Sorter2 = new BubbleSorter();
            SelectionSorter Sorter3 = new SelectionSorter();
            int iCount = 10000;
            Random random = new Random();
            Sorter1.list = new int[iCount];
            Sorter2.list = new int[iCount];
            Sorter3.list = new int[iCount];
            for (int i = 0; i < iCount; ++i)
            {
                Sorter1.list[i] = Sorter2.list[i] = Sorter3.list[i] = random.Next();
            }
            Thread sortThread1 = new Thread(new ThreadStart(Sorter1.Sort));
            Thread sortThread2 = new Thread(new ThreadStart(Sorter2.Sort));
            Thread sortThread3 = new Thread(new ThreadStart(Sorter3.Sort));
            sortThread1.Start();
            sortThread2.Start();
            sortThread3.Start();
            Console.Read();
        }
    }
}

实验运行结果:

© 版权声明
THE END
喜欢就支持以下吧
点赞1赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容