Adjust array so that all odd numbers are in front of even numbers

Source: Internet
Author: User

Topic:

Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits in the array are in the first half of the array, and all the even digits are in the second half of the array.

 #include <stdio.h> #include <stdlib.h>void move_arr (int &NBSP;ARR[],&NBSP;INT&NBSP;SZ) {int*left = arr;int*right = arr + sz - 1 ;while  (left < right) {while  (left < right)  &&  (*left)  % 2 == 1) {left++;} while  ((left < right)  &&  (*right)  % 2 == 0) {right--;} if  (left != right) {int tmp = *right;*right = *left;*left =  tmp;left++;right--;}}} Int main () {int i = 0;int arr[] = { 5, 2, 3, 4, 1,  6 };move_arr (arr, sizeof (arr)  / sizeof (*arr));for  (i = 0; i  < sizeof (arr)  / sizeof (*arr);  i++) {printf ("%d ",  arr[i]);} printf ("\ n"); system ("pause"); return 0;} 


Results ;

5 1 3 4 2 6

Please press any key to continue ...


Adjust array so that all odd numbers are in front of 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.