冒泡排序
冒泡排序就是相邻的两个数字进行比较,我们采用的冒泡排序是从前向后进行排序,如果前面的比后面的就进行交换位置。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 冒泡排序
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 0, 1, 2, 23, 4, 15, 26, 779, 118, 109 };
BubbleSort(arr);
PrintArr(arr);
}
static void BubbleSort(int[] arr)
{
for (int i = 0; i < arr.Length -1; i++)
{
for (int j = 0; j < arr.Length-1-i; j++)
{
if (arr[j]> arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void PrintArr(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i]+" ");
}
Console.WriteLine();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
选择排序
折半查找