您现在的位置是:首页 >

数据结构堆排序 数据结构与算法之C#插入排序

火烧 2022-06-05 18:19:56 1062
数据结构与算法之C#插入排序    u i g Sy tem    u i g Sy tem Collectio Ge eric    u i g Sy tem Text      ame ace B

数据结构与算法之C#插入排序  

   using System;

   using System Collections Generic;

   using System Text;

  

   namespace BubbleSort

   {

        class Program

        {

            static void Main(string[] args)

            {

                TestArray nums = new TestArray( );

                #region  初始化数组

                Random rnd = new Random( );

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

                {

                    nums Insert(rnd Next( ));

                }

                #endregion

                Console WriteLine( Before Sorting: );

                nums DisplayElements();

                Console WriteLine( Durring Sorting: );

                nums InsertionSort();

                Console WriteLine( After Sorting: );

                nums DisplayElements();

                Console ReadLine();

            }

        }

   }

   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++)

            {

                Console Write(arr[num]+ );

            }

            Console WriteLine();

        }

        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;

数据结构堆排序 数据结构与算法之C#插入排序

                temp=arr[outer];

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

                {

                    arr[inner]=arr[inner ];

                    inner = ;

                }

                arr[inner]=temp;

                this DisplayElements();

            }

        }

lishixinzhi/Article/program/net/201311/13854  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码