摘要:主题描述使用C#编写控制台应用程序。在数组中输入10个整数,然后使用气泡排序算法将一维数组的元素从小到大排序,然后输出。
题目描述
使用C#编写一个控制台应用。输入10个整数存入数组中,然后使用冒泡排序算法对一维数组的元素从小到大进行排序,并输出。
输入
在控制台中输入数字,存入一维数组
输出
输出排序后的数组
样例输入
87
85
89
84
76
82
90
79
78
68
样例输出
68
76
78
79
82
84
85
87
89
90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[10];
for (int i = 0; i < 10; ++i)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
sort(array);
for (int i = 0; i < 10; ++i)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
static void sort(int[] nums)
{
int temp = 0;
for (int i = 0; i < nums.Length - 1; ++i)
{
for (int j = 0; j < nums.Length - 1 - i; ++j)
{
if (nums[j] > nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
}
}