大家好..首先我就很菜
今天突然想起以前学的东西...排序
我无聊就随便写了写...结果发现思路想的是从大到小,可是结果是从小到大...
以下是我的代码:
static void GetStoB(int[] myint)
{
for (int j = 0; j < myint.Length; j++)
{
for (int i = 0; i < myint.Length ; i++)
{
if (myint[j] <myint[i])
{
int a = myint[j];
myint[j] = myint[i];
myint[i] = a;
}
}
}
for (int i = 0; i < myint.Length; i++)
{
Console.WriteLine(myint[i]);
}
}
static void Main(string[] args)
{
int[] myInt ={ 100, 4, 3, 7, 75, 47, 34, 86, 3, 0, 54, 30, 86, 45, 38, 87, 45, 99 };
GetStoB(myInt);
Console.ReadKey();
}
请大家指点一下..我不知道我的思路乱在哪了....总之我总觉得这段代码是从大到小排序..可实际是从小到大....如果您只回答如何修改代码那就不必了..我也知道把if里的<换成>,可换成>后我就觉得应该是从小到大了..请各位帮忙
今天突然想起以前学的东西...排序
我无聊就随便写了写...结果发现思路想的是从大到小,可是结果是从小到大...
以下是我的代码:
static void GetStoB(int[] myint)
{
for (int j = 0; j < myint.Length; j++)
{
for (int i = 0; i < myint.Length ; i++)
{
if (myint[j] <myint[i])
{
int a = myint[j];
myint[j] = myint[i];
myint[i] = a;
}
}
}
for (int i = 0; i < myint.Length; i++)
{
Console.WriteLine(myint[i]);
}
}
static void Main(string[] args)
{
int[] myInt ={ 100, 4, 3, 7, 75, 47, 34, 86, 3, 0, 54, 30, 86, 45, 38, 87, 45, 99 };
GetStoB(myInt);
Console.ReadKey();
}
请大家指点一下..我不知道我的思路乱在哪了....总之我总觉得这段代码是从大到小排序..可实际是从小到大....如果您只回答如何修改代码那就不必了..我也知道把if里的<换成>,可换成>后我就觉得应该是从小到大了..请各位帮忙
static void GetStoB(int[] myint)
{
for (int j = 0; j < myint.Length; j++)
{
for (int i = j+1; i < myint.Length ; i++)
{
if (myint[j] <myint[i])
{
int a = myint[j];
myint[j] = myint[i];
myint[i] = a;
}
}
}
for (int i = 0; i < myint.Length; i++)
{
Console.WriteLine(myint[i]);
}
}