Use C language programming to adjust the array so that all the odd numbers are in the front of the even number,

Source: Internet
Author: User

Use C language programming to adjust the array so that all the odd numbers are in the front of the even number,
Function requirements: Adjust the array so that all the odd numbers are in front of the even number. Requirement: enter an integer array and implement a function to adjust the order of numbers in the array so that all the odd numbers in the array are located in the first half of the array, and all the even numbers are located in the second half of the array. Think about it. To implement this function, first traverse the Function Array, starting from the two ends, one is to check whether the array element is an odd number from the front to the back, and one is to check whether the array element is an even number from the back to the back, if the forward is an even, but the last is an odd one, change it! The procedure is as follows:

/*** 2. Adjust the array so that all the odd numbers are in front of the even number. ** Subject: ** enter an integer array to implement a function. ** adjust the order of numbers in the array so that all odd numbers in the array are located in the first half of the array, ** all even numbers are located in the second half of the array. */# Include <stdio. h> # include <stdlib. h> void adjust_arr (int arr [], int size) {int temp; int I, j;/* place the odd digits in front, place the double digits behind * // if (size % 2 = 1) // {// I = 1; j = size-1; //} // else // {// I = 1; j = size-2; //} // for (; I <= j; I + = 2, j-= 2) // {// temp = arr [I]; // arr [I] = arr [j]; // arr [j] = temp; ///}/* place the odd number in front and the even number in the back */for (I = 0, j = size-1; I <j ;) {if (arr [I] % 2 = 0) & (arr [j] % 2 = 1) // if an odd number is in front of an even number, then {temp = arr [I]; arr [I] = arr [j]; arr [j] = temp ;} if (arr [I] % 2 = 1) // if arr [I] is an odd number, I ++; if (arr [j] % 2 = 0) // if arr [j] is an even number, j -- ;}} int main () {int arr [] = {2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 8, 9, 17,18, 19,20}; int I; int size = sizeof (arr)/sizeof (arr [0]); adjust_arr (arr, size); // function call, sort the team array, for (I = 0; I <size; I ++) printf ("% d", arr [I]); printf ("\ n"); system ("pause"); return 0 ;}

 


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.