using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool sorted = false;
int[] data = new int[100];
data = GenerateData(data);
while (sorted == false)
{
int count = Sort(data);
if (count == 0)
{
sorted = true;
}
else
{
Console.WriteLine("{0}", count);
}
}
}
public static int[] GenerateData(int[] data)
{
Random num = new Random();
for (int x = 0; x < 100; x++)
{
data[x] = num.Next(0, 99);
}
return data;
}
public static int Sort (int[] data)
{
int TempA = 0;
int TempB = 101;
int count = 0;
for (int x =0; x<100; x++)
{
TempA = data[x];
if ((x + 1) < 100)
{
TempB = data[(x + 1)];
}
else
{
TempB = 101;
}
if ( TempA > TempB)
{
data[x++] = TempA;
data[x] = TempB;
count++;
}
}
return count;
}
}
}
答案 0 :(得分:1)
我认为这两行存在问题
data[x++] = TempA;
data[x] = TempB;
它应该是
data[x++] = TempA;
data[x--] = TempB;
或者
data[x+1] = TempA;
data[x] = TempB;
否则您的for
循环将最终跳过元素。