//(复旦2011在职研究生试题数据结构考试)c#版
//一整数数组,要求奇数排在偶数前面,并且时间复杂度为O(n),并要求写出思路
static void test1()
{
int[] a = { 1, 2, 4, 3, 6 };
int start = 0;
int temp;
int end = a.Length - 1;
int num = 0;
bool flag = true; //i是否前进标准位
while (start != end)
{
if ((a[start] % 2 == 0)) //偶数
{
if (a[end] % 2 != 0) //如果前面是偶数后面是奇数,交换
{
temp = a[start];
a[start] = a[end];
a[end] = temp;
flag = true;
}
else
{
flag = false;
}
end--;
}
if(flag)
start++;
//统计用了多少次
num++;
}
//输出结果
for (int i = 0; i < a.Length; i++)
{
Console.Write("{0}{1}", a[i], " ");
}
Console.WriteLine();
Console.WriteLine(num);
}
//一整数数组,要求奇数排在偶数前面,并且时间复杂度为O(n),并要求写出思路
static void test1()
{
int[] a = { 1, 2, 4, 3, 6 };
int start = 0;
int temp;
int end = a.Length - 1;
int num = 0;
bool flag = true; //i是否前进标准位
while (start != end)
{
if ((a[start] % 2 == 0)) //偶数
{
if (a[end] % 2 != 0) //如果前面是偶数后面是奇数,交换
{
temp = a[start];
a[start] = a[end];
a[end] = temp;
flag = true;
}
else
{
flag = false;
}
end--;
}
if(flag)
start++;
//统计用了多少次
num++;
}
//输出结果
for (int i = 0; i < a.Length; i++)
{
Console.Write("{0}{1}", a[i], " ");
}
Console.WriteLine();
Console.WriteLine(num);
}