程序员面试100题之二十九,调整数组顺序使奇数位于偶数前面

#include "stdafx.h"



bool isEven(int n)

{

	return (n&1) == 0;

}



void reorder(int * arr, int len)

{

	int * begin = arr;

	int * end = arr+len-1;

	while(true)

	{

		while(!isEven(*begin))

		{

			begin++;

		}

		while(isEven(*end))

		{

			end--;

		}

		if(begin<end)

		{

			int temp = *begin;

			*begin = *end;

			*end = temp;

		}

		else

		{

			break;

		}

		



	}

}



int _tmain(int argc, _TCHAR* argv[])

{

	int arr[10] = {1,3,5,7,9,2,4,6,8,11};

	reorder(arr,10);

	return 0;

}



你可能感兴趣的:(程序员)