冒泡排序
冒泡排序就是相邻的两个数字进行比较,我们采用的冒泡排序是从前向后进行排序,如果前面的比后面的就进行交换位置。
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();
}
}
}