C#的冒泡排序

来源:互联网 发布:广州番禺网络花店 编辑:程序博客网 时间:2024/05/11 03:19
using System;//引入命名空间using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace C_Sharp//命名空间定义{    class Program//类Program定义    {        static void Swap(ref int x,ref int y)        {            x ^= y;            y ^= x;            x ^= y;        }        static void Main(string[] args)        {            int[] a = new int[100];            Console.WriteLine("输入int数组里的元素数目");            string s = Console.ReadLine();            int len = Int32.Parse(s);            Console.WriteLine("输入元素");            for(int j = 0; j < len; ++j)            {                string s1 = Console.ReadLine();                a[j] = Int32.Parse(s1);            }            int limit = len - 1;            for(int pass = 0; pass < len - 1; pass++)            {                for(int j = 0; j < limit - pass; j++)                {                    if (a[j] > a[j + 1])                    {                        Swap(ref a[j], ref a[j + 1]);                    }                }            }            Console.WriteLine("排序完成,遍历得");            for(int i = 0; i < len; ++i)            {                Console.WriteLine(a[i]);            }            Console.ReadKey();        }    }}
0 0
原创粉丝点击