C#插入排序
建議新人不要直接復制,最好手打,最好看過一遍之后根據自己的記憶和理解碼出來,
不用有太大壓力,就兩個for回圈,最好不要背,靠理解,否則直接背過沒有任何幫助,
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] {5,9,46,7,35,49,65,2,18};
//這里是插入排序的核心代碼
for (int i = 1; i < arr.Length; i++)
{
int num = a[i];
int j = i - 1;
while(j >= 0 && num > a[j])
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = num;
}
//最后輸出一次最終排好序的陣列
foreach (int b in arr)
{
Console.Write(b);
}
Console.ReadLine();
}
}
}
輸出為以下結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/246627.html
標籤:其他
