冒泡排序

Posted jiangxiaoming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了冒泡排序相关的知识,希望对你有一定的参考价值。

//int类型的冒泡排序
using
System; namespace 冒泡排序 { class Program { static void Sort(int[] sortArray) { bool swapped = true; do { swapped = false; for (int i = 0; i <sortArray.Length-1; i++) { if (sortArray[i]>sortArray[i+1]) { int temp = sortArray[i]; sortArray[i] = sortArray[i + 1]; sortArray[i + 1] = temp; swapped = true; } } } while (swapped); } static void Main(string[] args) { int[] sortArray = new int[] { 1, 5, 486, 887, 313, 33 }; Sort(sortArray); foreach (var temp in sortArray) { Console.Write(temp + " "); } Console.ReadKey(); } } }

拓展通用的冒泡排序

namespace _007_冒泡排序拓展 {
    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;
        }
        //如果e1大于e2的话,返回true,否则返回false
        public static bool Compare(Employee e1, Employee e2)
        {
            if (e1.Salary > e2.Salary) return true;
            return false;
        }

        public override string ToString()
        {
            return Name + ":" + Salary;
        }
    }
    class Program {
        static void Sort(int[] sortArray)
        {
            bool swapped = true;
            do 
            {
                swapped = false;
                for (int i = 0; i < sortArray.Length - 1; i++)
                {
                    if (sortArray[i] > sortArray[i + 1])
                    {
                        int temp = sortArray[i];
                        sortArray[i] = sortArray[i + 1];
                        sortArray[i + 1] = temp;
                        swapped = true;
                    }
                }
            } while (swapped);
        }

        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("dsf",12), 
                new Employee("435dsf",234), 
                new Employee("234dsf",14), 
                new Employee("ds234f",234), 
                new Employee("dssfdf",90)
            };
            CommonSort<Employee>(employees,Employee.Compare);
            foreach (Employee em in employees)
            {
                Console.WriteLine(em);
            }
            Console.ReadKey();
        }
    }
}

 

以上是关于冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章

java冒泡排序法代码

python代码实现鸡尾酒排序(双向冒泡排序)

冒泡排序python代码

视频+图文+动画详解冒泡排序

交换排序(冒泡排序快速排序的算法思想及代码实现)

C语言冒泡排序。