您的位置:首页 > 编程语言 > C#

c#冒泡算法

2015-09-07 18:40 253 查看
int temp;
int[] a = { 2, 5, 8, 9, 10, 11, 7, 6 };
for (int i = 0; i < a.Length-1; i++) {  //总共的次数
for (int j = 0; j <a.Length-i-1; j++) {   //每次要比较的次数
if (a[j] > a[j+1]) {  //如果后一项比第一项大 则把最大数放在前面
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;

}

}

}
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadKey();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: