c#编程实现冒泡排序
时间: 2023-12-02 13:42:37 浏览: 47
以下是C#编程实现冒泡排序的代码示例:
```csharp
using System;
public class BubbleSort {
static void Main(string[] args) {
int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
int n = arr.Length;
BubbleSortFunc(arr, n);
Console.WriteLine("Sorted array: ");
PrintArray(arr, n);
}
static void BubbleSortFunc(int[] arr, int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (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, int size) {
int i;
for (i = 0; i < size; i++) {
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}
```
以上代码中,BubbleSortFunc()函数实现了冒泡排序算法,PrintArray()函数用于打印排序后的数组。在Main()函数中,我们定义了一个整型数组arr,并将其传递给BubbleSortFunc()函数进行排序。最后,我们使用PrintArray()函数打印排序后的数组。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)