C#,用选择排序来对一个数组进行排序,应该怎么写?

2025-06-23 05:52:02
推荐回答(1个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 知识点回顾
{
class ChooseSort
{
static void Main(string[] args)
{
int[] array = { 1, 5, 7, 8, 4, 3, 10 };
ChooseSort1(array);
Console.WriteLine();
}
public static void ChooseSort1(params int[] args)
{
int mark = 0;
int temp;
for (int i = 0; i < args.Length - 1; i++)
{
mark = i;//mark从i开始,是因为除去排好的数以外的数
for (int j = i; j < args.Length; j++)
{
if (args[mark] > args[j])
{
mark = j;
}
}
if (mark != i)//说明该数已经改变了位置。
{
temp = args[mark];
args[mark] = args[i];
args[i] = temp;
}
}
Console.WriteLine("选择排序以后的数组为:");
foreach (int x in args)
{
Console.Write("{0,6}", x);
}
}
}
}