程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 基本排序算法(冒泡排序,選擇排序,插入排序)後續[時間測試]

基本排序算法(冒泡排序,選擇排序,插入排序)後續[時間測試]

編輯:C#入門知識

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]) 
                    { 
                        temp = arr[inner + 1]; 
                        arr[inner + 1] = arr[inner]; 
                        arr[inner] = temp; 
                    } 
                } 
                //this.DisplayElements();  
            }       
        } 
 
        // 選擇排序  
        public void SelectionSort() 
        { 
            int temp; 
            for (int outer = 0; outer < upper; outer++) 
            { 
                for (int inner = outer+1; inner <= upper; inner++) 
                { 
                    if (arr[outer] > arr[inner]) 
                    { 
                        temp = arr[outer]; 
                        arr[outer] = arr[inner]; 
                        arr[inner] = temp; 
                    } 
                } 
                //this.DisplayElements();  
            } 
        } 
 
        // 插入排序  
        public void InsertionSort() 
        { 
            int inner, temp; 
            for (int outer = 1; outer <= upper; outer++) 
            { 
                temp = arr[outer]; 
                inner = outer; 
 
                while (inner > 0 && arr[inner-1] >= temp) 
                { 
                    arr[inner] = arr[inner - 1]; 
                    inner -= 1; 
                } 
 
                arr[inner] = temp; 
 
                //this.DisplayElements();  
            } 
        } 
    } 
 
    /// <summary>  
    /// 時間測試類  
    /// </summary>  
    public class Timing 
    { 
        /// <summary>  
        /// 記錄開始時間  
        /// </summary>  
        private TimeSpan startingTime; 
 
        /// <summary>  
        /// 記錄方法的用時  
        /// </summary>  
        private TimeSpan duration; 
 
        /// <summary>  
        /// 初始化  
        /// </summary>  
        public Timing() 
        { 
            startingTime = new TimeSpan(0); 
            duration = new TimeSpan(0); 
        } 
 
        /// <summary>  
        /// 結束計時  
        /// </summary>  
        public void StopTime() 
        { 
            duration = Process.GetCurrentProcess().Threads[0] 
                .UserProcessorTime.Subtract(startingTime); 
        } 
 
        /// <summary>  
        /// 開始計時  
        /// </summary>  
        public void StartTime() 
        { 
            GC.Collect(); 
            GC.WaitForPendingFinalizers(); 
            startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime; 
        } 
 
        /// <summary>  
        /// 獲取結果  
        /// </summary>  
        /// <returns></returns>  
        public TimeSpan Result() 
        { 
            return duration; 
        } 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved