バブルソート(繰り返し比較を行い、必要であれば配列中の隣り合う要素同士を入れ替える。
その操作を繰り返すと、小さい値は配列の一方の方向(主に左側)に移動し、大きな値はその反対側(主に右側)にずれていく。
結果、数の大きさの順にうまいこと並べ替えられる)
探していたら、見つけたので無くならないように覚書。
using System;
class Bubble
{
static void Main()
{
int[] nums = { 333, 23, 576, 569, 384, 758, 978, 123, 432, 657, 598 };
int start,end,tmp;
Console.Write(“元の配列:”);
for (int i = 0; i < nums.Length; i++)
{
Console.Write(" " + nums[i]);
}
Console.WriteLine();
for (start = 1; start < nums.Length; start++)
{
for (end = nums.Length-1; end >= start; end–)
{
if (nums[end – 1] > nums[end])
{
tmp = nums[end – 1];
nums[end – 1] = nums[end];
nums[end] = tmp;
}
}
}
Console.Write(“ソート後の配列:”);
for (int i = 0; i < nums.Length; i++)
{
Console.Write(" " + nums[i]);
}
Console.WriteLine();
}
}
元の配列: 333 23 576 569 384 758 978 123 432 657 598
ソート後の配列: 23 123 333 384 432 569 576 598 657 758 978