当前位置:编程学习 > C#/ASP.NET >>

基本排序算法(冒泡排序,选择排序,插入排序)后续[时间测试]

using System; 
using System.Diagnostics; 
 
namespace TestCArray 

    class Program 
    { 
        static void Main(string[] args) 
        {           
            Timing sortTime = new Timing(); 
 
            // int numItems = 1000;  
            // int numItems = 10000;  
            int numItems = 100000; 
            CArray theArray;  
                   
            theArray = new CArray(numItems); 
            sortTime.StartTime(); 
            theArray.InsertionSort(); 
            sortTime.StopTime(); 
            Console.WriteLine("InsertionSort ----->" + sortTime.Result().TotalMilliseconds); 
            theArray.Clear(); 
 
            theArray = new CArray(numItems); 
            sortTime.StartTime(); 
            theArray.BubbleSort(); 
            sortTime.StopTime(); 
            Console.WriteLine("BubbleSort ----->"+sortTime.Result().TotalMilliseconds); 
            theArray.Clear(); 
 
            theArray = new CArray(numItems); 
            sortTime.StartTime(); 
            theArray.SelectionSort(); 
            sortTime.StopTime(); 
            Console.WriteLine("SelectionSort ----->" + sortTime.Result().TotalMilliseconds); 
            theArray.Clear(); 
        } 
    } 
 
 
    public class CArray 
    { 
        private int[] arr; 
        private int upper; 
        private int numElements; 
 
        Random rnd = new Random(100); 
 
        public CArray(int size) 
        { 
            arr = new int[size]; 
            upper = size - 1; 
            numElements = 0; 
 
            Init(); 
        } 
 
        public void Insert(int item) 
        { 
            arr[numElements] = item; 
            numElements++; 
        } 
 
        public void Init() 
        { 
            for (int i = 0; i <= upper; i++) 
            { 
                Insert(rnd.Next() * 100); 
            } 
        } 
 
        public void DisplayElements() 
        { 
            Console.Write("---->"); 
            for (int i = 0; i <= upper; i++) 
            { 
                Console.Write(arr[i] + " "); 
            } 
            Console.WriteLine(); 
        } 
 
        public void Clear() 
        { 
            for (int i = 0; i <= upper; i++) 
            { 
                arr[i] = 0; 
            } 
 
            numElements = 0; 
        } 
 
        // 冒泡排序  
        public void BubbleSort() 
        { 
            int temp; 
 
            for (int outer = 0; outer <= upper; outer++) 
            { 
                for (int inner = 0; inner < upper-outer; inner++) 
                { 
                    if (arr[inner+1]<arr[inner]) 
                    { 
           &n
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,