写一个冒泡排序 用,net实现
时间: 2023-05-15 17:05:52 浏览: 45
以下是使用 C# 实现的冒泡排序代码:
```
using System;
class BubbleSort
{
static void Main()
{
int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
Console.WriteLine("原始数组:");
PrintArray(arr);
BubbleSortAlgorithm(arr);
Console.WriteLine("排序后的数组:");
PrintArray(arr);
}
static void BubbleSortAlgorithm(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void PrintArray(int[] arr)
{
foreach (int i in arr)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
```
这个程序使用了 C# 语言和 .NET 框架来实现冒泡排序。它首先定义了一个整数数组,然后调用 BubbleSortAlgorithm 函数来对数组进行排序。BubbleSortAlgorithm 函数使用嵌套的循环来比较和交换数组元素,直到数组被完全排序。最后,PrintArray 函数被调用来打印排序后的数组。