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); } }}
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
- int类型的冒泡排序_拓展_
- (第三季)205-int类型的冒泡排序 206-拓展的通用的冒泡排序方法
- 排序_冒泡排序
- 数据结构_冒泡排序
- 数据结构_冒泡排序
- 冒泡_排序
- python _ 冒泡排序
- 冒泡排序_升序
- 01_冒泡排序
- 排序算法_冒泡排序
- 排序学习_冒泡排序
- 选择排序_冒泡排序
- 122-123_容器_排序_冒泡排序
- 排序----冒泡排序的优化_选择排序_插入排序
- 经典算法_冒泡排序
- C语言_冒泡排序
- 算法_冒泡排序算法
- JAVA_算法_冒泡排序
- Go语言中的map用法
- 为什么输出对象时,调用toString()?
- 浏览器渲染页面过程描述,DOM编程技巧以及重排和重绘。
- Javascript 严格模式use strict详解
- Python之简易用户登陆代码
- int类型的冒泡排序_拓展_
- 洛谷P3129 [USACO15DEC]高低卡(白金)High Card Low Card (Platinum)
- HGDB视图和物化视图的区别
- JSP_response
- 最全Pycharm教程(21)——Pycharm编辑器功能之代码快速修改
- 快速启动VSCODE工作目录
- 串珠问题(今日头条笔试)
- 字符串排序组合
- Go语言中的map和slice