梓骏

导航

C# 经典算法 冒泡排序法(正序,倒序)

 /// 冒泡排序法
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int[] arr = new int[] { 6, 1, 2, 5, 9, 11, 0, 4, 14 };
            int temp = 0;
            for (int i = 0; i < arr.Length - 1; i++)//外部循环arr的长度
            {
                for (int j = i + 1; j < arr.Length; j++)//比较大
                {
                    if (arr[j] < arr[i])//正序
                    {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                    //if (arr[j] > arr[i])//倒叙相反显示
                    //{
                    //    temp = arr[j];
                    //    arr[j] = arr[i];
                    //    arr[i] = temp;
                    //}
                }
            }
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
        }

posted on 2012-08-04 17:41  梓骏  阅读(882)  评论(0编辑  收藏  举报