C#實現冒泡排序 堆棧 隊列

來源:互聯網
上載者:User

namespace BubbleSort

{

    class BubbleSortTest

    {

        /// <summary>

        /// 使用嵌套迴圈實現升序排序

        /// </summary>

        /// <param name="arrary"></param>

        public static void Sort(int[] arrary)

        {

            int temp = 0;

            for (int i = 0; i < arrary.Length - 1; i++)

            {

                for (int j = 0; j < arrary.Length - 1 - i; j++)

                {

                    if (arrary[j] > arrary[j + 1])     //將“>”改成“<”就實現降序排序

                    {

                        temp = arrary[j];

                        arrary[j] = arrary[j+1];

                        arrary[j + 1] = temp;

                    }

                }

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            int[] arrary = { 6, 4, 7, 2, 9, 1, 5, 12, 35, 74, 14, 57, 25, 75, 26, 95, 74, 35, 38, 73 };

            BubbleSortTest.Sort(arrary);

            foreach (int index in arrary)

            {

                Console.Write(index + "\t");

            }

            Console.ReadKey();

        }

    }

}

 

 

 

隊列程式

namespace Queue

{

    class QueueTest

    { 

        int[] arrary;

        int length = 0;

        public QueueTest(int num)

        {

            arrary = new int[num];

        }

        public int Length

        {

            get

            {

                return this.length;

            }

        }

        public void Push(int num)     //資料進隊列的方法

        {

            if (length > 0)

            {

                for (int i = length - 1; i >= 0; i--)    //使用迴圈將已有的資料後移一個位置,將第一個位置騰出來放置剛進入隊列的資料,讀資料時從後面讀取實現資料的先進先出,後進後出

                {

                    arrary[i + 1] = arrary[i];

                }

            }

            arrary[0] = num;

            length++;

        }

        public int Pop()     //資料出隊列的方法

        {

            return arrary[--length];      //取出最後面的一個數

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            QueueTest queuetest = new QueueTest(100);

            queuetest.Push(4);

            queuetest.Push(8);

            queuetest.Push(6);

            queuetest.Push(17);

            queuetest.Push(2);

            queuetest.Push(1);

            queuetest.Push(9);

            while (queuetest.Length > 0)

            {

                Console.Write(queuetest.Pop() + "\t");

            }

            Console.ReadKey();

        }

    }

}

 

 

 

堆棧程式

namespace Stack

{

    class StackTest

    {

        int[] arrary;

        int length = 0;

        public StackTest(int num)

        {

            arrary = new int[num];

        }

        public int Length

        {

            get

            {

                return this.length;

            }

        }

        public void Push(int num)     //資料進堆棧的方法,將新資料放入舊資料的後面,Pop()時從後面讀取數,實現先進後出,後進先出

        {

            arrary[length] = num;

            length++;

        }

        public int Pop()     //資料出堆棧的方法

        {

            return arrary[--length];      //取出最後面的一個數

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            StackTest stacktest = new StackTest(100);

            stacktest.Push(4);

            stacktest.Push(8);

            stacktest.Push(6);

            stacktest.Push(17);

            stacktest.Push(2);

            stacktest.Push(1);

            stacktest.Push(9);

            while (stacktest.Length > 0)

            {

                Console.Write(stacktest.Pop() + "\t");

            }

            Console.ReadKey();

        }

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.