使用C#写一个冒泡程序
时间: 2023-12-10 12:06:30 浏览: 35
好的,下面是一个简单的C#冒泡排序程序:
```csharp
using System;
public class BubbleSort {
static void Main() {
int[] arr = {5, 2, 1, 6, 3};
int temp;
Console.WriteLine("排序前的数组:");
foreach (int element in arr) {
Console.Write(element + " ");
}
for (int i = 0; i < arr.Length - 1; i++) {
for (int j = 0; j < arr.Length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
Console.WriteLine("\n排序后的数组:");
foreach (int element in arr) {
Console.Write(element + " ");
}
}
}
```
输出结果:
```
排序前的数组:
5 2 1 6 3
排序后的数组:
1 2 3 5 6
```
这是一个简单的冒泡排序程序,它使用了两个嵌套的for循环来交换数组中的元素,以便将它们按升序排列。