Title: Enter an array of integers to implement a function to adjust the order of the numbers in the array so that all the odd digits are in the first half of the array, and all the even digits are in the second half of the array.
1, general ideas, do not consider the complexity of time:
Each time the array is traversed, an even number is taken out, and all subsequent digits are moved forward, and the even is placed at the end.
2, using the idea of bubble sorting, two pointers, one before, if the former is even, after an odd number, on the exchange.
The algorithm is as follows:
void Reorder_array (int p[],int length) { if (NULL = = P | | length <= 0) return; int i,j; int temp; for (i = 0; i < length, i++) {for (j = length-1; j > i; j--) { if (p[i]%2 = = 0 && p[j]%2! = 0) /** head pointing even, tail pointing odd, exchanging both */ { temp = p[i]; P[i] = p[j]; P[J] = temp; } if (p[i]%2! = 0) The/** head pointer points to an odd number, jumps out of the inner loop, i++*/ {break ; } /** tail pointer points to even, what does not do, wait j--*/ } } for (i = 0; i < length; i++) { cout << p[i] << '; } cout << Endl;}
Here is the book on the wording, feel better than I wrote:
The logic is clearer, and the computation efficiency is improved by using bit and operation instead of the redundancy operation.
void Reorder_array_from_book (int *pdata, unsigned 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) /** take the remainder operation with & instead of */
pbegin++; while (Pbegin < pEnd && (*pend & 0x1) = = 0) pend--; if (Pbegin < pEnd) { int temp = *pbegin; *pbegin = *pend; *pend = temp;}} }
consider the bit and operation, and consider the extensibility of the algorithm, using a function pointer.
/** function pointer notation, can improve extensibility, need to pay attention to */bool IsEven (int pData) { return (PData & 0x1) = = 0;} void Reorder_array_pre (int p[],int length,bool (*func) (int)) { if (NULL = = P | | length <= 0) return; int i,j; int temp; for (i = 0, i < length; i++) {for (j = length-1; j > i; j--) { if (func (p[i]%2) &&!func (p[j ]%2)) /** head pointing to even, tail pointing odd, exchanging both */ { temp = p[i]; P[i] = p[j]; P[J] = temp; } The IF (!func (p[j]%2)) /** head pointer points to an odd number, jumps out of the inner loop, i++*/ {break ; } /** tail pointer points to even, what does not do, wait j--*/ } } for (i = 0; i < length; i++) { cout << p[i] << '; } cout << Endl; }
Test code:
int main () { //int p[11] = {1,2,3,4,5,9,3,11,66,55,68}; int p[10] = {0,1,2,3,4,5,6,7,8,9}; int p[8] = {2,2,2,2,1,1,1,1}; int p[1] = {1}; Reorder_array (p,8); Reorder_array_pre (P,10,iseven); /** here is the pointer, so it is changed the contents of the original array *//* for (int i = 0; i <; i++) { cout << p[i] << '; } cout << endl;*/ return 0;}
Results:
/* Little bit of accumulation, my small step O (∩_∩) o~*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The Offer_ of the sword 14_ adjusting the array order so that the odd digits precede the even number (function pointer usage)