• C#实现冒泡排序 堆栈 队列


    冒泡排序程序

    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();

            }

        }

    }

     

  • 相关阅读:
    圣杯局部和双飞翼布局
    解决css英文换行问题
    存在即合理--字体样式大全
    存在即合理---个别软件下载
    sublime Text3常见插件汇总
    省略的方法
    JS数组方法与python列表方法的比较
    vue中数据改变,强制视图更新,视图不更新的原因和解决办法
    Vue通信
    路由的缓存
  • 原文地址:https://www.cnblogs.com/lijiangyanyu/p/2252892.html
Copyright © 2020-2023  润新知