Adjust the array order so that the odd number is located before the even number, and the even number of Arrays

Source: Internet
Author: User

Adjust the array order so that the odd number is located before the even number, and the even number of Arrays
Question: enter an integer array to adjust the order of numbers in the array so that all odd numbers are in the first half of the array, and all even numbers are in the second half of the array. The time complexity is O (n ).

Analysis: If time complexity is not taken into account, the simplest idea should be to scan the array from the ground up. Each time an even number is reached, this number is taken out, and move all the numbers behind the number one by one. After moving, there is a blank space at the end of the array. Then, the even number is put into this space. Since an even number needs to be moved to an O (n) number, the total time complexity is O (n2 ).

It is required that the odd number be placed in the first half of the array, and the even number be placed in the second half of the array. Therefore, all odd numbers should be located in front of the even number. That is to say, when we scan this array, if an even number exists before the odd number, we can swap their order, and after the exchange, it will meet the requirements.

Therefore, we can maintain two pointers. The first pointer is initialized to the first number of the array, which moves only backward. The second pointer is initialized to the last number of the array, which only moves forward. Before the two pointers meet each other, the first pointer is always in front of the second pointer. If the first pointer points to an even number and the second Pointer Points to an odd number, we will exchange the two numbers.

1 # include <stdio. h> 2 # include <tchar. h> 3 4 void Reorder (int * pData, unsigned int length, bool (* func) (int); 5 bool isEven (int n); 6 7/* ideas: use two pointers, one pointing to a number in the array, and one moving backward. Eight pointing to the last number in the array, moving forward only. Before the other two pointers meet, 9 if the first pointer points to an even number and the second pointer to an odd number, exchange the two numbers 10 */11 void ReorderOddEven_1 (int * pData, unsigned int length) 12 {13 if (pData = NULL | length = 0) 14 return; 15 16 int * pBegin = pData; 17 int * pEnd = pData + Length-1; 18 19 while (pBegin <pEnd) 20 {21 while (pBegin <pEnd & (* pBegin & 0x1 )! = 0) 22 pBegin ++; 23 while (pBegin <pEnd & (* pEnd & 0x1) = 0) 24 pEnd --; 25 26 if (pBegin <pEnd) 27 {28 int temp = * pBegin; 29 * pBegin = * pEnd; 30 * pEnd = temp; 31} 32} 33} 34 35 // method 2 and method 1 have the same idea, but the function of method 1 is decoupled into two parts, improving code reusability. 36 void ReorderOddEven_2 (int * pData, unsigned int length) 37 {38 Reorder (pData, length, isEven); 39} 40 41 void Reorder (int * pData, unsigned int length, bool (* func) (int) 42 {43 if (pData = NULL | length = 0) 44 return; 45 46 int * pBegin = pData; 47 int * pEnd = pData + length-1; 48 49 while (pBegin <pEnd) 50 {51 while (pBegin <pEnd &&! Func (* pBegin) 52 pBegin ++; 53 54 while (pBegin <pEnd & func (* pEnd) 55 pEnd --; 56 57 if (pBegin <pEnd) 58 {59 int temp = * pBegin; 60 * pBegin = * pEnd; 61 * pEnd = temp; 62} 63} 64} 65 66 bool isEven (int n) 67 {68 return (n & 1) = 0; 69} 70 71 void PrintArray (int numbers [], int length) 72 {73 if (length <0) 74 return; 75 for (int I = 0; I <length; ++ I) 76 printf ("% d \ t", numbers [I]); 77 78 printf ("\ n"); 79} 80 81 int main () 82 {83 // use method 1 to test 84 int numbersOne [] = {1, 2, 3, 4, 5, 6, 7}; 85 int lengthOne = sizeof (numbersOne)/sizeof (int); 86 printf ("Test for solution 1: \ n "); 87 PrintArray (numbersOne, lengthOne); 88 ReorderOddEven_1 (numbersOne, lengthOne); 89 PrintArray (numbersOne, lengthOne); 90 printf ("\ n "); 91 92 // use method 2 to test 93 int numbersTwo [] = {2, 4, 6, 1, 3, 5, 7}; 94 int lengthTwo = sizeof (numbersTwo) /sizeof (int); 95 printf ("Test for solution 2: \ n"); 96 PrintArray (numbersTwo, lengthTwo); 97 ReorderOddEven_2 (numbersTwo, lengthTwo ); 98 PrintArray (numbersTwo, lengthTwo); 99 100 return 0; 101}

 

 

Another idea is to use two pointers, one of which is the first and the other. When the first is an odd number, the two pointers are exchanged with the latter. The details are as follows: if the array is an odd number at the beginning, the former and the former point to the same number. After the exchange, the array remains unchanged until the first even number is reached, the front pointer exceeds the back pointer.

1 # include <iostream> 2 // # include <algorithm> can omit swap 3 using namespace std; 4 5 void swap (int * num1, int * num2) 6 {7 int temp = * num1; 8 * num1 = * num2; 9 * num2 = temp; 10} 11 bool isEven (int n) 12 {13 return (n & 1) = 0; 14} 15 16 void Reorder (int * pData, unsigned int length, bool (* func) (int )) 17 {18 if (length <0 | pData = NULL) 19 return; 20 else {21 int I =-1; 22 for (int j = 0; j <length; j ++) 23 {24/* When the number in the array is an odd number, switch I. j points to two digits 25. When the Starting number is an odd number, the exchanged number is the same, 26 if there is an even number in the middle, the condition statement does not meet, Skip, j continues to increase, I does not change, 27 then when an odd number is encountered, j points to an odd number to meet the condition, I refers to an odd number forward. After I ++, 28 then I points to the even number next to this odd number and exchanges two numbers. 29 */30 if (! Func (* (pData + j) 31 {32 I ++; 33 swap (* (pData + I), * (pData + j )); 34} 35} 36} 37} 38 39 void PrintArray (int numbers [], int length) 40 {41 if (length <0) 42 return; 43 for (int I = 0; I <length; ++ I) 44 printf ("% d \ t", numbers [I]); 45 46 printf ("\ n"); 47} 48 49 int main () 50 {51 int numbers [] = {3, 4, 5, 6, 7, 8, 9, 10, 1, 2 }; 52 int length = sizeof (numbers)/sizeof (int); 53 54 PrintArray (numbers, length); 55 56 Reorder (numbers, length, isEven); 57 58 PrintArray (numbers, length); 59 60 return 0; 61}

Related Article

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.