[C#]간단한 버블정렬 예제
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class OjcSorter
{
public static void BubbleSort(int[] obj)
{
int tmp;
for (int i = 0; i < obj.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (obj[j] > obj[i])
{
tmp = obj[i];
obj[i] = obj[j];
obj[j] = tmp;
}
}
}
}
class Program
{
static void Main(string[] args)
{
int[] iArray = { 16, 5, 7, 9, 6, 45, 33 };
foreach (int i in iArray)
{
Console.Write(i + " ");
}
Console.WriteLine();
OjcSorter.BubbleSort(iArray);
foreach (int i in iArray)
{
Console.Write(i + " ");
}
}
}
}
}
[결과]
16 5 7 9 6 45 33
5 6 7 9 16 33 45
댓글 없음:
댓글 쓰기