# Make an array of odd digits in front of even numbers

Source: Internet
Author: User

`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//first method: #include  <stdio.h>void  Reverse (Int *p,int len) { int *start = p; int *end = p +  len - 1; int tmp = 0; while  (start < end)  {   if  ((*start)  % 2 == 1)   {   start++;   }  else  {   if  ((*end % 2 == 0))     {    end--;   }   else   {     tmp = *start;    *start = *end;    * End = tmp;   }  } }}int main () { int arr[10] =  { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int  Len = sizeof (arr) &Nbsp;/ sizeof (Arr[0]);  reverse (Arr,len); int i = 0; for  (i =  0; i < 10; i++)  {  printf ("%d ",  arr[i]); }  return 0;} The second method: #include  <stdio.h>void swap (INT&NBSP;*PA,&NBSP;INT*PB) { int tmp =  *pa; *pa = *pb; *pb = tmp;} Int main () { int arr[] = { 1, 2, 3, 4, 5, 6, 7,  8, 9 }; int left = 0; int right = sizeof (arr)  /  sizeof (Arr[0]) -1; while  (left < right)  {  while  ((left  < right)  &&  (arr[left] % 2 == 1)   {    left++;  }  while  (left < right)  &&  (arr[right]  % 2 == 0))   {   right--;  }  if  (left < right)   {   swap (&arr[ Left], &arr[right]);   left++;   right--;  }    }  int i = 0; for  (i = 0; i < sizeof (arr)  / sizeof (Arr[0]);  i++)  {  printf ("%d ",  arr[i]);  } printf (" \ n ");  return 0;}`

Make an array of odd digits in front of even numbers

Related Keywords:

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.