一.冒泡排序
口诀:
N 个数字来排队,两两相比小靠前。
案例:定义一个数组,输出后从大到小排列
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _04冒泡排序 { class Program { static void Main(string[] args) { //冒泡排序算法: //大的往上冒,小的往下沉 //对于一组数进行按照从大到小或从小到大的一个顺序排列算法 int[] array = { 23, 30, 18, 40, 21 }; //控制轮数 for (int i = 0; i < array.Length-1; i++) { for (int j=0;j<array.Length-1- i;j++) { if (array[j]<array[j+1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + " "); } Console.ReadLine(); } } }
二.字符串Replace方法
例如:
在聊天室中经常遇到屏蔽脏话功能,完成当用户输入一句话中带有“sb”,则将“sb”替换成“**”
Console.WriteLine("请输入一句带有sb的话:"); string rep = Console.ReadLine(); string a = rep.Replace("sb", "**"); Console.WriteLine(a); Console.ReadLine();
转载请标明出处:c#中的冒泡排序和字符串Replace方法
文章来源: https://blog.csdn.net/weixin_44870681/article/details/91339143