c# 随机生成10个(0-100)的整数,并用冒泡法排序.
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/19 05:23:52
![c# 随机生成10个(0-100)的整数,并用冒泡法排序.](/uploads/image/z/1675641-57-1.jpg?t=c%23+%E9%9A%8F%E6%9C%BA%E7%94%9F%E6%88%9010%E4%B8%AA%EF%BC%880-100%EF%BC%89%E7%9A%84%E6%95%B4%E6%95%B0%2C%E5%B9%B6%E7%94%A8%E5%86%92%E6%B3%A1%E6%B3%95%E6%8E%92%E5%BA%8F.)
c# 随机生成10个(0-100)的整数,并用冒泡法排序.
c# 随机生成10个(0-100)的整数,并用冒泡法排序.
c# 随机生成10个(0-100)的整数,并用冒泡法排序.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rdm = new Random();
int[] arr = new int[10];
Console.WriteLine("排序前:");
for (int i = 0; i < 10; i++)
{
arr[i] = rdm.Next(0,100);
Console.Write(arr[i] + " ,");
}
Console.WriteLine("");
Console.WriteLine("排序后:");
Sort(arr);
}
public static void Sort(int[] arr)
{
for (int j = 1; j < arr.Length; j++)
{//外循环每次把参与排序的最大数排在最后
for (int i = 0; i < arr.Length - j; i++)
{ //内层循环负责对比相邻的两个数,并把最大的排在后面
if (arr[i] > arr[i + 1])
{ //如果前 一个数大于后一个数,则交换两个数
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
//用 一个循环访问数组里的元素并打印
for (int j = 0; j < arr.Length; j++)
{
Console.Write(arr[j] + " ,");
}
}
}
}