用C sharp写一个冒泡排序
时间: 2023-05-13 11:04:43 浏览: 44
当然可以,以下是C#实现冒泡排序的代码:
```
public static void BubbleSort(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;
}
}
}
}
```
这段代码可以对一个整型数组进行冒泡排序,时间复杂度为O(n^2)。
相关问题
写一个C Sharp的冒泡排序
### 回答1:
我可以为您编写一个C Sharp的冒泡排序算法:
public static void BubbleSort(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])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
### 回答2:
冒泡排序是一种简单的排序算法,通过交换相邻的元素将较大(或较小)的值向右(或向左)移动到列表的末尾。下面是一个使用C Sharp编写的冒泡排序算法的示例:
```csharp
using System;
class BubbleSortExample
{
static void BubbleSort(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])
{
// 交换 arr[j] 和 arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void Main(string[] args)
{
int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
Console.WriteLine("排序前的数组:");
foreach (int item in arr)
{
Console.Write(item + " ");
}
BubbleSort(arr);
Console.WriteLine("\n排序后的数组:");
foreach (int item in arr)
{
Console.Write(item + " ");
}
Console.ReadLine();
}
}
```
以上代码定义了一个BubbleSortExample类,其中包含一个BubbleSort方法来实现冒泡排序。在Main方法中,定义了一个整数数组arr,并使用BubbleSort方法对其进行排序。
输出的结果为:
```
排序前的数组:
64 34 25 12 22 11 90
排序后的数组:
11 12 22 25 34 64 90
```
这是冒泡排序算法对输入数组进行排序后的结果。
### 回答3:
冒泡排序是一种简单的排序算法,在C#中可以很容易地实现。下面是一个示例代码:
```
using System;
class BubbleSort
{
static void Main()
{
int[] arr = { 5, 2, 8, 12, 1 };
Console.WriteLine("排序前的数组:");
PrintArray(arr);
BubbleSortAlgorithm(arr);
Console.WriteLine("\n排序后的数组:");
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 element in arr)
{
Console.Write(element + " ");
}
}
}
```
上述代码首先定义了一个整数数组 `arr`,包含了要排序的元素。然后,通过 `BubbleSortAlgorithm` 函数实现了冒泡排序算法。该函数通过两个嵌套的循环遍历数组,并比较相邻的元素,若前者比后者大,则交换它们的位置。最内层的 `j` 循环控制每一轮的比较和交换,而外层的 `i` 循环用于控制整个排序的轮数。
在 `Main` 方法中,首先输出未排序的数组,然后调用 `BubbleSortAlgorithm` 函数对数组进行排序,并输出排序后的结果。
最后,输出结果如下所示:
```
排序前的数组:
5 2 8 12 1
排序后的数组:
1 2 5 8 12
```
这个示例代码可以帮助你理解如何使用C#编写一个冒泡排序算法。
用c sharp写一个冒泡
### 回答1:
排序程序的示例代码:
```
using System;
class BubbleSort
{
static void Main()
{
int[] arr = {3, 60, 35, 2, 45, 320, 5};
Console.WriteLine("排序前的数组:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
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])
{
// 交换两个数
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
Console.WriteLine("\n排序后的数组:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
}
```
这是一个用 C# 语言编写的冒泡排序程序。它定义了一个数组 arr,并在两个循环中使用冒泡排序算法将其元素排序。两个 for 循环中的第二个循环用于比较相邻元素的大小,并在需要时交换它们。最后,程序打印出排序后的数组。
### 回答2:
冒泡排序是一种简单而经典的排序算法,使用C#语言编写冒泡排序程序可以如下:
```C#
using System;
class Program
{
static void Main(string[] args)
{
int[] arr = { 5, 3, 8, 2, 1 };
Console.WriteLine("排序前的数组:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
BubbleSort(arr);
Console.WriteLine("排序后的数组:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
static void BubbleSort(int[] arr)
{
int n = arr.Length;
bool swapped;
for (int i = 0; i < n - 1; i++)
{
swapped = false;
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;
swapped = true;
}
}
if (!swapped)
{
break;
}
}
}
}
```
这段代码首先定义了一个数组arr,然后通过调用BubbleSort方法对数组进行冒泡排序。冒泡排序的原理是从数组的第一个元素开始,依次比较相邻的两个元素,如果顺序错误就交换位置,直到整个数组有序。通过外层循环控制排序的轮数,通过内层循环逐个比较和交换元素,通过布尔变量swapped判断是否发生交换,如果没有发生交换,则可以提前结束排序。最后,输出排序后的数组。执行这段代码,就可以看到冒泡排序的结果。
### 回答3:
冒泡排序(Bubble Sort)是一种简单的排序算法,其基本思想是通过相邻元素之间的比较和交换来实现排序。下面是使用C#语言编写的冒泡排序算法的示例代码:
```csharp
using System;
class BubbleSort
{
static void Main(string[] args)
{
int[] arr = { 4, 9, 2, 6, 5 };
Console.WriteLine("排序前:");
PrintArray(arr);
BubbleSortAlgorithm(arr);
Console.WriteLine("\n排序后:");
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])
{
// 交换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 element in arr)
{
Console.Write(element + " ");
}
}
}
```
运行以上代码,将输出排序前后的数组元素。冒泡排序的核心是嵌套的两个循环,通过比较和交换相邻元素来实现排序。外层循环控制比较的轮数,内层循环用于在每一轮中进行相邻元素的比较和交换。