C# codes as blow:
static int[] Method(int[] array)
{
while(array==null || array.Length==0)
return array;
int i=0;
int j=array.Length-1;
while (i < j)
{
while (array[i] % 2 == 1 && i < array.Length - 1)
i++;
while (array[j] % 2 == 0 && j > 0)
j--;
if (i < j)
{
int number = array[i];
array[i] = array[j];
array[j] = number;
}
}
return array;
}
本文介绍了一个使用C#实现的简单算法,该算法能够将一个整数数组中的所有奇数移动到前面,所有偶数移动到后面。通过遍历数组并交换元素的位置来达到目的。
5390

被折叠的 条评论
为什么被折叠?



