• 两种排序方法(选择排序和冒泡排序)


    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;
                        }
                    }
                   
                }
            }

  • 相关阅读:
    ckeditor+粘贴word
    java+批量下载大文件
    链接批量下载文件
    java+大文件上传解决方案
    php+提高大文件上传速度
    富文本粘贴word文档内容图片处理
    软件开发工具(三)——理论与开发过程
    c结构体里的数组与指针
    Java解析Property文件
    怎样高速编译mediatekoperator以下代码
  • 原文地址:https://www.cnblogs.com/sk2016/p/6086144.html
Copyright © 2020-2023  润新知