static void Main(string[] args)
        {            
            var arr = new int[] { 2, 8, 5, 1, 9, 9, 7, 7, 25, 35, 20 };
            Sort1(arr);
            foreach (var item in arr)
            {
                Console.WriteLine(item);

            }

            var arr2 = new int[] { 2, 8, 5, 1, 9, 9, 7, 7, 25, 35, 20 };
            Sort2(arr2);
            foreach (var item in arr2)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="arr"></param>
        static void Sort1(int[] arr)
        {
            for (var i = 0; i < arr.Length; i++)
            {
                int min=i;
                for (var j = i + 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[min])
                    {
                        min = j;
                    }
                }
                var temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="arr"></param>
        static void Sort2(int[] arr)
        {
            for (var i = 0; i < arr.Length; i++)
            {
                for (var j = i + 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[i])
                    {
                        var temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
               
            }
        }

posted on 2016-11-21 17:14  菜鸟Only  阅读(585)  评论(0编辑  收藏  举报