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

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值