Adjust array order to make odd digits before even numbers

Source: Internet
Author: User

Problem: Adjusting the array order is preceded by an even number of odd digits.

Without considering the complexity of the time, the simplest idea is to scan the array from scratch, take it out whenever an even number is encountered, and then move all the digits after that even to one position, and then place that even on the last slot of the array. At this point, the time complexity of the algorithm is O (n^2).

This topic requires that the odd number be placed in the first half of the array, even in the second half of the array, so that all the odd numbers should precede the even number. That is, if you are scanning an array, and you find an even digit and an odd number in front of it, the odd and even positions are swapped.

"Fast pointer": so we can maintain two pointers, when the first pointer is initialized to the first digit of the array, it moves forward only, and the second pointer initializes the last digit of the array, and it moves backwards. The first pointer is always in front of the second pointer. If the first pointer points to an even number and the second pointer is an odd number, the two digits are exchanged.

Attention:

a) Put the first pointer point to the first number, point the second pointer to the last digit

b) Move the first pointer backward until it points to an even number, at which point the second pointer moves forward until it points to an odd

c) Exchange two digits pointing to the pointer

d) continue repeating B) until the second pointer is in front of the first pointer, at which point all the odd numbers are in front of the even one. The algorithm ends.

Adjusts the array order, which is the odd digit before even
#include <iostream>
using namespace Std;

void Reorderoddeven (int *pdata, int length)
{
if (PData = = NULL | | length <= 0)
Return
int *pbegin = PData;
int *pend = PData + length-1;
while (Pbegin < pEnd)
{
while (Pbegin < pEnd && (*pbegin & 0x1)! = 0)
pbegin++;
while (Pbegin < pEnd && (*pend & 0x1) = = 0)
pend--;
if (Pbegin < pEnd)
{
int tmp = *pbegin;
*pbegin = *pend;
*pend = tmp;
}
}
}

int main ()
{
int data[5] = {1, 2, 3, 4, 5};
Reorderoddeven (data, 5);
for (int i = 0; i < 5; i++)
cout << Data[i] << "";
cout <<endl;

System ("pause");
return 0;
}

Solutions to consider extensibility: (a universal solution to a range of problems)

Extract the logical framework of the problem and turn the criteria of judgment into a function pointer, which is to use a separate function to determine whether a number conforms to the standard. At this point, the entire function can be decoupled into two parts: (a) determine whether the number should be in the first half of the array or the second half of the standard; (ii) The operation of the split fraction group.

//decoupling operation: Using function pointer (*fun) (int)
void ReOrder (int *pdata, int length, BOOL (*fun) (int))
{
 if (pData = = NULL | | length <= 0)
  return;
 int *pbegin = PData;
 int *pend = PData + length-1;
 while (Pbegin < pEnd)
 {
  while (Pbegin < pEnd &&!fun (*pbegin))// When the first pointer points to an odd number, the first pointer continues to move backwards
   pbegin++;
  while (Pbegin < pEnd && Fun (*pend))
   pend--;
  if (Pbegin < pEnd)
  {
   int temp;
   temp = *pbegin;
   *pbegin = *pend;
   *pend = temp;
  }
 }
}
//iseven is the specific criterion for determining whether a number is even.
BOOL IsEven (int n)
{
 return (n & 1) ==0;
}

void Reorderoddeven (int *pdata, int *pend, int length)
{
ReOrder (pData, length, isEven);
}

Adjust array order to make odd digits before even numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.