算法练习三:奇偶分割
static void Main(string[] args)
{
int[] a = new int[] { 1,2,3,4,5,6,7,8,9,16,18};
int[] b = FenGei(a);
foreach (int i in b)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
static int[] FenGei(int[] a)
{
int low = 0;
int high = a.Length - 1;
while (low < high)
{
while (!IsEven(a[low]) && low < high)
{
low++;
}
while (IsEven(a[high]) && low < high)
{
high--;
}
int c = a[low];
a[low]=a[high] ;
a[high] = c;
}
return a;
}
static bool IsEven(int a)
{
if (a % 2 == 0)
{
return false;
}
else
{
return true;
}
}
posted on 2009-04-02 16:40 Love Fendi 阅读(220) 评论(0) 编辑 收藏 举报