zoukankan      html  css  js  c++  java
  • 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();

            }

        }

    }

     

  • 相关阅读:
    小程序发展史
    ES6内置方法find 和 filter的区别在哪
    微信小程序开发踩坑记录
    小程序导航跳转一不小心踩进的坑
    谈谈如何对后台登陆界面进行渗透
    应急响应学习笔记
    php学习笔记
    代码审计学习笔记
    注入笔记(非sql注入)
    python安全编程学习
  • 原文地址:https://www.cnblogs.com/lijiangyanyu/p/2252892.html
Copyright © 2011-2022 走看看