冒泡法

来源:互联网 发布:魔力宝贝更新网络错误 编辑:程序博客网 时间:2024/04/27 15:13

usingSystem;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace冒泡法
{
class Program
{
static void Main(string[] args)
{
{
string[] arr = new string[] { "今天","从入门到精通","爱你一万年","成都市四川省的省会城市","地铁二号线开通了" };
string temp = "";//定义TEMP用来存放临时交换的汉字
for (int i = 1; i <= arr.Length-1; i++)//i表示比较的趟数
{
for (int j = 1; j <= arr.Length - i; j++)//j表示比较的次数
{
if (arr[j - 1].Length<= arr[j].Length)//判断字的长度
{
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}

}
foreach (string vari in arr)//输出结果
{
Console.Write(vari + " ");

}
Console.WriteLine();
}
}
}
}

 

原创粉丝点击