using System;
using System.Collections.Generic;
class Test
{
static void Main()
{
// ソート前
List<int> scoreList = new List<int> { 3, 5, 0, 1, 2, 6 };
// 「選択ソート」でソートする
// ソート完了リストに、まず「ソート前リスト」を格納
List<int> sortedList = scoreList;
for (int iSort = 0; iSort < sortedList.Count; iSort++)
{
// 現在の値を最大値として設定
int max = sortedList[iSort];
int index = iSort; // 現在のインデックス番号
// maxに代入した値より大きい値を探す
// このループを抜けたとき、maxに最大値、indexに最大値のインデックス番号が入っている
for (int jData = iSort; jData < sortedList.Count; jData++)
{
// 値をdataに代入し、maxよりも大きいならば、indexとmaxを更新する
int data = sortedList[jData];
if (max < data)
{
index = jData;
max = data;
}
}
// 見つけた最大値を一時保管
int temp = sortedList[index];
// 見つけた最大値をListから削除する
sortedList.RemoveAt(index);
// 見つけた最大値を挿入する
sortedList.Insert(iSort, temp);
// TEST: 今の状態を確認してみる
foreach (var item in sortedList)
{
Console.Write(item + ", ");
}
Console.Write("\n");
}
foreach (var item in sortedList)
{
Console.WriteLine(item);
}
}
}