class Program
{
delegate int Compare(int a, int b);
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
int [] array = { 3, 7, 4, 2, 10};
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
BubbleSort(array, new Compare(AscendCompare));
for (int j = 0; j < array.Length; j++)
{
Console.WriteLine(array[j]);
}
}
static int AscendCompare(int a, int b)
{
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
static void BubbleSort(int[] DataSet, Compare Comparer)
{
int i = 0;
int j = 0;
int temp = 0;
for (i = 0; i < DataSet.Length - 1; i++)
{
for (j = 0; j < DataSet.Length - (i + 1); j++)
{
if (Comparer(DataSet[j], DataSet[j + 1]) > 0)
{
// 자리바꾸기
temp = DataSet[j + 1];
DataSet[j + 1] = DataSet[j];
DataSet[j] = temp;
}
}
}
}
}
728x90
'C#' 카테고리의 다른 글
C# / 대리자 실습 - 대리자 체인 DelegateChain / 체인 연산 (0) | 2022.08.10 |
---|---|
C# / 대리자 실습 - Delegate / 정렬 메소드 구현 추가IComparable<T> 사용 (0) | 2022.08.10 |
C# / 대리자 실습 - Delegate / 덧셈 뺄셈을 위한 대리자 (0) | 2022.08.10 |
C# Study / 값(value) 타입, 참조(reference) 타입 변수 실습 및 관찰 (0) | 2022.08.01 |
C# Study / long, ulong, int, uint / 정수형의 최댓값을 넘으면? (0) | 2022.08.01 |
댓글