3.選擇排序
using System;
namespace SelectionSorter
{
/// <summary>
/// 選擇排序(Selection Sort)的基本思想是:每一趟從待排序的記錄中選出關鍵字最小的記錄,順序放在已排好序的子文件的最後,直到全部記錄排序完畢。
/// </summary>
public class SelectionSort
{
public static void Sort(int[] numArray)
{
int min, tmp;
for (int i = 0; i < numArray.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < numArray.Length; j++)
{
if (numArray[j] < numArray[min])
{
min = j;
}
}
tmp = numArray[i];
numArray[i] = numArray[min];
numArray[min] = tmp;
}
}
}
public class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 20, 41, 27, 14, 16, 1, 8, 55, 9, 35, 22, 14 };
SelectionSort.Sort(arr);
Console.WriteLine("Numbers after selectionsort:");
foreach (int i in arr)
{
Console.WriteLine(i);
}
Console.Read();
}
}
}