
C#冒泡排序算法开发实例
- 大小:0.13MB
- 分类:C#源码
- 环境:c#
- 更新:2025-07-15
热门排行
简介
冒泡排序(Bubble Sort)是最简单和最通用的排序方法,其基本思想是:在待排序的一组数中,将相邻的两个数进行比较,若前面的数比后面的数大就交换两数,否则不交换;如此下去,直至最终完成排序。由此可得,在排序过程中,大的数据往下沉,小的数据往上浮,就像气泡一样,于是将这种排序算法形象地称为冒泡排序。
核心代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 冒泡排序法 //n个数需排n-1次 { class Program { static void Main(string[] args) { int[] scores = { 18, 20, 48, 76, 33, 22, 101 }; for (int i = 0; i < scores.Length -1; i )//控制循环次数 { for (int j = 0; j < scores.Length - (i 1); j )//从大到小排序 { if (scores[j]<scores[j 1]) { int temp = scores[j]; scores[j] = scores[j 1]; scores[j 1] = temp; } } } for (int i = 0; i < scores.Length ; i ) { Console.WriteLine(scores[i]); } Console.ReadKey(); } } }