C#高级编程--冒泡排序

 
 
int类型冒泡排序的简单实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 冒泡排序拓展
{
    class Program
    {
        /// <summary>
        /// 冒泡排序方法
        /// </summary>
        /// <param name="Array">要排序的数组</param>
        static void sorting(int []Array)
        {
            bool IsSorted=true ; //结束循环的标志位
            do
            {
                IsSorted = false;
                for (int i = 0; i < Array.Length - 1; i++)
                {
                    if (Array[i] > Array[i + 1])
                    {
                        //实现位置的互换
                        int temp = Array[i];
                        Array[i] = Array[i + 1];
                        Array[i + 1] = temp;
                        IsSorted = true;
                    }
                }
            } while (IsSorted);
           
        }
        static void Main(string[] args)
        {
            int[] test = new int[]{ 1, 5, 88, 6, 95, 44, 9, };
            sorting(test );
            foreach (var temp in test)
            {
                Console.WriteLine(temp+"" );
            }
            
            Console.ReadKey ();
        }
    }
}

冒泡排序拓展方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 冒泡排序拓展
{
    class Program
    {
        /// <summary>
        /// 声明一个静态方法
        /// </summary>
        /// <typeparam name="T">表示泛型</typeparam>
        /// <param name="sortArray">泛型里要比较的成员</param>
        /// <param name="compareMethod">薪水比较的方法</param>
        static void CommonSort<T>(T[] sortArray, Func<T, T, bool> compareMethod)
        {
            bool swapped = true;
            do
            {
                swapped = false;
                for (int i = 0; i < sortArray.Length - 1; i++)
                {
                    if (compareMethod(sortArray[i], sortArray[i + 1]))
                    {
                        T temp = sortArray[i];
                        sortArray[i] = sortArray[i + 1];
                        sortArray[i + 1] = temp;
                        swapped = true;
                    }
                }
            } while (swapped);
        }
        static void Main(string[] args)
        {
            Employee[] employees = new Employee[]
            {
                new Employee("hzq",1200),
                new Employee("dnf",2340),
                new Employee("lol",1400),
                new Employee("pubg",4000),
                new Employee("cs",9000)
            };
            //调用 CommonSort方法,指定类型,比较的成员,比较的方法
            CommonSort<Employee>(employees, Employee.Compare);
            foreach (Employee em in employees)
            {
                Console.WriteLine(em);
            }
            Console.ReadKey();
        }
    }
  
        //新建雇员类,类成员有雇员名字和薪水
        class Employee
        {
            public string Name { get; private set; }
            public int Salary { get; private set; }

            public Employee(string name, int salary)
            {
                this.Name = name;
                this.Salary = salary;
            }
        /// <summary>
        /// 新建比较方法
        /// </summary>
        /// <param name="e1">雇员1</param>
        /// <param name="e2">雇员2</param>
        /// <returns></returns>
        //如果e1大于e2的话,返回true,否则返回false
        public static bool Compare(Employee e1, Employee e2)
            {
                if (e1.Salary > e2.Salary) return true;
                return false;
            }
         /// <summary>
         /// 重写ToString方法,返回名字和薪水
         /// </summary>
         /// <returns></returns>
         public override string ToString()
            {
                return Name + ":" + Salary;
            }
        }
    }


这个方法可以作任意类型的冒泡排序,还是不太会用所以写了很多注释,以后用到的时候好再研究。目前想到的就是可以用来做游戏的排行榜。

猜你喜欢

转载自blog.csdn.net/qq_35711014/article/details/79627145
今日推荐