调整数组顺序使奇数位于偶数前面
题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。要求时间复杂度为O(n)。
分析:如果不考虑时间复杂度,最简单的思路应该是从头扫描这个数组,每碰到一个偶数时,拿出这个数字,并把位于这个数字后面的所有数字往前挪动一位。挪完之后在数组的末尾有一个空位,这时把该偶数放入这个空位。由于碰到一个偶数,需要移动O(n)个数字,因此总的时间复杂度是O(n2)。
要求的是把奇数放在数组的前半部分,偶数放在数组的后半部分,因此所有的奇数应该位于偶数的前面。也就是说我们在扫描这个数组的时候,如果发现有偶数出现在奇数的前面,我们可以交换他们的顺序,交换之后就符合要求了。
因此我们可以维护两个指针,第一个指针初始化为数组的第一个数字,它只向后移动;第二个指针初始化为数组的最后一个数字,它只向前移动。在两个指针相遇之前,第一个指针总是位于第二个指针的前面。如果第一个指针指向的数字是偶数而第二个指针指向的数字是奇数,我们就交换这两个数字。
基于这个思路,我们可以写出如下的代码:
void Reorder(int *pData, unsigned int length, bool (*func)(int));
bool isEven(int n);
/////////////////////////////////////////////////////////////////////////
// Devide an array of integers into two parts, odd in the first part,
// and even in the second part
// Input: pData - an array of integers
// length - the length of array
/////////////////////////////////////////////////////////////////////////
void ReorderOddEven(int *pData, unsigned int length)
{
if(pData == NULL || length == 0)
return;
Reorder(pData, length, isEven);
}
/////////////////////////////////////////////////////////////////////////
// Devide an array of integers into two parts, the intergers which
// satisfy func in the first part, otherwise in the second part
// Input: pData - an array of integers
// length - the length of array
// func - a function
/////////////////////////////////////////////////////////////////////////
void Reorder(int *pData, unsigned int length, bool (*func)(int))
{
if(pData == NULL || length == 0)
return;
int *pBegin = pData;
int *pEnd = pData + length - 1;
while(pBegin < pEnd)
{
// if *pBegin does not satisfy func, move forward
if(!func(*pBegin))
{
pBegin ++;
continue;
}
// if *pEnd does not satisfy func, move backward
if(func(*pEnd))
{
pEnd --;
continue;
}
// if *pBegin satisfy func while *pEnd does not,
// swap these integers
int temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
}
}
/////////////////////////////////////////////////////////////////////////
// Determine whether an integer is even or not
// Input: an integer
// otherwise return false
/////////////////////////////////////////////////////////////////////////
bool isEven(int n)
{
return (n & 1) == 0;
}
讨论:
上面的代码有三点值得提出来和大家讨论:
1.函数isEven判断一个数字是不是偶数并没有用%运算符而是用&。理由是通常情况下位运算符比%要快一些;
2.这道题有很多变种。这里要求是把奇数放在偶数的前面,如果把要求改成:把负数放在非负数的前面等,思路都是都一样的。
3.在函数Reorder中,用函数指针func指向的函数来判断一个数字是不是符合给定的条件,而不是用在代码直接判断(hard code)。这样的好处是把调整顺序的算法和调整的标准分开了(即解耦,decouple)。当调整的标准改变时,Reorder的代码不需要修改,只需要提供一个新的确定调整标准的函数即可,提高了代码的可维护性。例如要求把负数放在非负数的前面,我们不需要修改Reorder的代码,只需添加一个函数来判断整数是不是非负数。这样的思路在很多库中都有广泛的应用,比如在STL的很多算法函数中都有一个仿函数(functor)的参数(当然仿函数不是函数指针,但其思想是一样的)。如果在面试中能够想到这一层,无疑能给面试官留下很好的印象。
博主何海涛对本博客文章享有版权。网络转载请注明出处http://zhedahht.blog.163.com/。整理出版物请和作者联系。
------------------------------------------------------------------------------------------
以下c# 版本,原理一样的的,呵呵
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//作者:王海龙 地点:北京
//Emali||MSN:heaven.kaixin@gmail.com
namespace 调整数组顺序使奇数位于偶数前面
{
class Program
{
static void Main(string[] args)
{
int[] intList = { -29, 35, 22, 10, 40, 9, 3, 18, 63 };
QuickSort(intList, 0, intList.Length - 1);
foreach (int i in intList)
Console.WriteLine(i);
Console.ReadKey();
}
/// <summary>
/// </summary>
/// <param name="intList"></param>
/// <param name="low"></param>
/// <param name="hight"></param>
static void QuickSort(int[] intList, int low, int hight)
{
while (low < hight)
{
// (2&1)==0 (3&1)==1 (奇数&1)==1 (偶数&1)==0
while (low < hight && IsOdd(intList[low]))
low++;
while (low < hight && !IsOdd(intList[hight]))
hight--;
if (!IsOdd(intList[low]) && IsOdd(intList[hight]))
Swap(ref intList[low], ref intList[hight]);
}
}
static void Swap(ref int i, ref int j)
{
int temp = i;
i = j;
j = temp;
}
/// <summary>
/// 判断一个数是否为奇数
/// (2&1)==0 (3&1)==1 (奇数&1)==1 (偶数&1)==0
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
static bool IsOdd(int i)
{
if ((i & 1) == 1)
return true;
else
return false;
}
}
}