int类型的冒泡排序_拓展_

来源:互联网 发布:杀肖数据统计资料区 编辑:程序博客网 时间:2024/05/21 08:38

int类型的冒泡排序_拓展_<13/9/2017>

每一次都将最大的放在后面,每次循环完后最后的一个最大根据循环次数依次向前递减,直到循环没有发生任何交换,则为有序且从小到大的数组.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace int类型的冒泡排序_拓展_{    class Program    {        static void Main(string[] args)        {            int[] sortInt = new int[10] {3,5,1,4,5,6,7,3,2,1};            bool isOut = true;            do            {                //如果都是按顺序从小到大,也就是没有经过任何依次排序的话,false就不会改变了                isOut = false;                for(int i = 0; i < sortInt.Length - 1; i++)                {                    //判断是否发生交换,一旦发生交换就得再次循环(isOut变成true)                    if (sortInt[i] > sortInt[i + 1])                    {                        int temp = sortInt[i];                        sortInt[i] = sortInt[i + 1];                        sortInt[i + 1] = temp;                        isOut = true;                    }                }            } while (isOut);            foreach(int i in sortInt)            {                Console.Write(i+" ");            }        }    }}

实例讲解:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace int类型的冒泡排序_拓展_{    class Program    {        static void Main(string[] args)        {            Employee[] employees = new Employee[] {                new Employee("dgs",12),                new Employee("ddgs",1212),                new Employee("dags",11232),                new Employee("dss",1123),                new Employee("ddggs",1233),                new Employee("dgwws",14442)            };            CommonSort<Employee>(employees, Employee.Compare);            foreach(Employee em in employees)            {                Console.WriteLine(em.ToString());            }            Console.ReadKey();        }        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);        }    }}


定义一个Employee

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace int类型的冒泡排序_拓展_{    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;        }        public static bool Compare(Employee a, Employee b)        {            if (a.Salary > b.Salary) { return true; }            return false;        }        public override string ToString()        {            //return base.ToString();            return Name+":"+Salary;        }    }}