欢迎访问我的博客 https://javascript.shop

排序 冒泡排序法

原文发布时间为:2009-03-06 —— 来源于本人的百度文章 [由搬家工具导入]

using System;//冒泡排序

namespace sorts
{
   public class Class3
    {
       public static void Main()
       {
           int[] a=new int[]{2,8,4,6,9,4};
           BubbleSort arr=new BubbleSort();
           arr.Sort(a);
           for(int i=0;i<a.Length;i++)
               Console.Write("{0} ",a[i]);
           Console.WriteLine();
           Console.ReadLine();
       }

    }
    public class BubbleSort
    {
        public void Sort(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
                for (int j = 0; j < arr.Length-i- 1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        int temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    }
                }
        }
    }
}

posted @ 2017-07-12 00:05  孑孓子  阅读(101)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop