using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsolePractice
{
class CArray
{
private int[] arr;
//数组大小
private int upper;
//下标
private int numElements; /// <summary>
/// 初始化数组参数
/// </summary>
/// <param name="size"></param>
public CArray(int size)
{
arr = new int[size];
upper = size - ;
numElements = ;
} /// <summary>
/// 插入方法
/// </summary>
/// <param name="item">存储的数</param>
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
} /// <summary>
/// 输出方法
/// </summary>
public void DisplayElements()
{
for (int i = ; i <= upper; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
} /// <summary>
/// 清除数组
/// </summary>
public void Clear()
{
for (int i = ; i <= upper; i++)
{
arr[i] = ;
}
numElements = ;
} /// <summary>
/// 选择排序算法
/// </summary>
public void SelectionSort()
{
int min, temp;
//每一轮都可以确定一个最小值,排到最前面
for (int outer = ; outer <= upper; outer++)
{
min = outer;
//每一次拿自身数值与排在它后面的数组比较大小,若比它小则记录下标,一轮过后调换数值。
for (int inner = outer + ; inner <= upper; inner++)
{
if (arr[inner] < arr[min]) min = inner;
}
temp = arr[outer];
arr[outer] = arr[min];
arr[min] = temp;
this.DisplayElements();
}
}
} class C_shape
{
static void Main()
{
CArray nums = new CArray();
Random rnd = new Random();
for (int i = ; i < ; i++)
{
nums.Insert(rnd.Next(, ));
}
Console.WriteLine("Before sorting:");
nums.DisplayElements();
Console.WriteLine("During sorting:");
nums.SelectionSort();
Console.WriteLine("After sorting:");
nums.DisplayElements();
Console.ReadKey();
}
}
}
运行结果: