熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

數據結構與算法之C#插入排序

2022-06-13   來源: .NET編程 

   using System;

   using SystemCollectionsGeneric;

   using SystemText;

  

   namespace BubbleSort

   {

       class Program

       {

           static void Main(string[] args)

           {

               TestArray nums = new TestArray();

               #region  初始化數組

               Random rnd = new Random();

               for (int num = ; num < ; num++)

               {

                   numsInsert(rndNext());

               }

               #endregion

               ConsoleWriteLine(Before Sorting: );

               numsDisplayElements();

               ConsoleWriteLine(Durring Sorting: );

               numsInsertionSort();

               ConsoleWriteLine(After Sorting: );

               numsDisplayElements();

               ConsoleReadLine();

           }

       }

   }

   public class TestArray

   {

       private int[] arr;

       private int upper;

       private int numElements;

       public TestArray(int size)

       {

           arr = new int[size];

           upper = size ;

           numElements = ;

       }

       public void Insert(int item)

       {

           arr[numElements] = item;

           numElements++;

       }

       public void DisplayElements()

       {

           for (int num = ; num <= upper; num++)

           {

               ConsoleWrite(arr[num]+ );

           }

           ConsoleWriteLine();

       }

       public void Clear()

       {

           for (int num = ; num <= upper; num++)

           {

               arr[num] = ;

           }

           numElements = ;

       }

       //插入排序算法

       public void InsertionSort()

       {

           int inner temp;

           for(int outer=;outer<=upper;outer++)

           {

               inner=outer;

               temp=arr[outer];

               while(inner> && arr[inner]>=temp)

               {

                   arr[inner]=arr[inner];

                   inner=;

               }

               arr[inner]=temp;

               thisDisplayElements();

           }

       }

   }


From:http://tw.wingwit.com/Article/program/net/201311/13854.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.