
编写一个C#控制台应用程序,实现冒泡排序(要求分别写出朴素算法)
2个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 5, 8, 3, 9, 2, 0, 12, 11 };
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
{
if (a[i] > a[j])
{
int b = a[i];
a[i]=a[j];
a[j]=b;
}
}
}
string c = "";
for (int k = 0; k < a.Length; k++)
{
c = string.Format("{0,-4}",a[k])+c;
}
Console.WriteLine(c);
Console.Read();
}
}
}
经运行检验 无问题
运行结果 12 11 9 8 5 3 2 1 0
具体内容自行修改
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 5, 8, 3, 9, 2, 0, 12, 11 };
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
{
if (a[i] > a[j])
{
int b = a[i];
a[i]=a[j];
a[j]=b;
}
}
}
string c = "";
for (int k = 0; k < a.Length; k++)
{
c = string.Format("{0,-4}",a[k])+c;
}
Console.WriteLine(c);
Console.Read();
}
}
}
经运行检验 无问题
运行结果 12 11 9 8 5 3 2 1 0
具体内容自行修改
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询