使用c#写一个冒泡排序
以下是使用C#编写的冒泡排序示例代码:
using System;
class BubbleSort
{
static void Main()
{
int[] arr = { 5, 3, 8, 4, 2 };
int temp = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
Console.WriteLine("Sorted array:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
}
}
输出:
Sorted array:
2 3 4 5 8
``
原文地址: http://www.cveoy.top/t/topic/dquA 著作权归作者所有。请勿转载和采集!