int[] arr = { 30, 12, 56, 78, 12125, 56, 44, 212, 45, 787, 21, 22, 56, 65 }; //待排序陣列
for (int i = 1; i < arr.Length; i++)
{
int a = arr[i]; //首先記住這個預備要插入的數
int b = i - 1; //找出它前一個數的下標(等下 準備插入的數 要跟這個數做比較)
//如果這個條件滿足,說明,我們還沒有找到適當的位置
while (b >= 0 && a > arr[b]) //這里小于是升序,大于是降序
{
arr[b + 1] = arr[b]; //同時把比插入數要大的數往后移
b--; //指標繼續往后移,等下插入的數也要跟這個指標指向的數做比較
}
arr[b + 1] = a;
}
foreach (int item in arr)
Console.WriteLine(item);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/146546.html
標籤:其他
上一篇:正確配置Android Studio和Flutter、Dart插件版本 Start a new Flutter project
