这是一个插入排序算法
我理解将最大的 no 转发到下一个索引,但无法理解当它向前移动时,它的先前位置(索引)如何被刚刚比较的较小数字占据,例如在列表 [2,1] 2 移动到下一个索引列表 [ j+1]=列表[j];但是 1 如何向后移动或移动到上一个索引
//unsorted array
int[] list = new int[] { 5, 2, 4, 6, 1 };
// the key element being sorted
int key;
//
//start looping starting from the second element
for (int i = 1; i < list.Length; i++)
{
key = list[i];//store the key
int j = i - 1;//get the previous index
//
//loop until you meet a smaller number or 0
while (j >= 0 && list[j] > key)
{
//move the greater number forward
list[j + 1] = list[j];
// Decrementing
j--;
}
//set the key in the proper index
list[j + 1] = key;
}