我正在嘗試使用while創建氣泡排序。我已經在下面發佈了我的課程。爲什麼在這種排序中不顯示9的最後一個int。使用while循環進行氣泡排序。最後一種排序不存在輸出
namespace BubbleSort {
class Program
{
static void Main(string[] args)
{
int[] i = {9, 2, 7, 6, 1, 3, 5, 4, 8};
int va = 0, vb = 0;
//loop through all numbers in the array.
while (va < i.Length)
{
//loop through all numbers in the array trailing the first loop by 1.
while (vb < i.Length)
{
//compare the two values.
if (i[vb] < i[va]) {
Console.WriteLine(vb);
}
vb++; //increment
}
va++; //increment
}
Console.ReadLine();
}
}
}
該方法是否正確?
你想使用氣泡排序的特定原因嗎?爲什麼不只是i.OrderBy(x => x); –
這不是一個冒泡排序,另外你輸出的索引,而不是價值。泡泡排序涉及多次傳遞 –
i.Length是9,你正在增加一個打印vb,而它比i.Length更小,所以你永遠不會打印出一個9. – Jonny