C#编程入门_简单算法_15

来源:互联网 发布:天意网络魔域一条龙 编辑:程序博客网 时间:2024/06/05 22:33

21篇C#博客的配套源码


冒泡排序

这里写图片描述
冒泡排序就是相邻的两个数字进行比较,我们采用的冒泡排序是从前向后进行排序,如果前面的比后面的就进行交换位置。

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();        }     }}

选择排序


折半查找