每次用户输入值后,如何运行插入排序代码。请注意,我对编程知之甚少,因此将不胜感激展示示例或准备使用的代码。
Console.Write("How long the Insertion sort list should be?: ");
var countString = Console.ReadLine();
int count = Convert.ToInt32(countString);
int[] data = new int[count];
for (int i = 0; i < count; i++)
{
var input = Console.ReadLine();
data[i] = Convert.ToInt32(input);
Console.WriteLine(input); // << HERE THE SORTING SHOULD HAPPEN AFTER EACH VALUE THAT I ADD.
}
int j = 0;
int help = 0;
for (int i = 1; i < data.Length; i++)
{
j = i;
help = data[i];
while (j > 0 && help < data[j - 1])
{
data[j] = data[j - 1];
j--;
}
data[j] = help;
}
foreach (var i in data)
{
Console.Write("{0}, ", i);
}
}