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

Source: Internet
Author: User
#include <stdio.h>void sort (int *p, int len) {int *end = P;end = p + len-1;while (p<end) {if (*p% 2 = = 0) {while ( End) {if (*end% 2 = = 1) {break;} end--;} int tmp = *end;*end = *p;*p = tmp;p++;} else{p++;}}} int main () {int arr[] = {3,5,4,8,9,5,6,2,6};int len = sizeof (arr)/sizeof (arr[0]), sort (arr,len); int i = 0;for (i = 0; I & Lt Len i++) {printf ("%d", Arr[i]);} return 0;} Output as 3,5,5,9,8,4,6,2,6 This problem my idea is to set two marks, a point to the head of a point to the tail, the first element if it is an odd number regardless of him, if it is an even number and the last comparison, which is if the tail element is an odd number of the interchange or the trailing element moves forward, Until the odd number is found, loop in turn until two signs meet to stop.

This article is from the "Traces" blog, be sure to keep this source

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: 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.