将一整数序列按奇数在前,偶数在后的顺序重新排放,并要求奇偶两部分分别有序。

将一整数序列按奇数在前,偶数在后的顺序重新排放,并要求奇偶两部分分别有序。编写一函数实现。


#include <stdio.h>
#define N 15

void sort(int *,int);
void swap(int *, int *);

int main()
{
      int int_array[N] = {0,10,9,8,7,6,5,4,3,2,1,11,15,13,0} ;
      int i;

      sort(int_array,N);
      for (i=0;i<N;i++)
        printf("%5d/n",int_array[i]);

      return 0;
}

/* define the function sort()     */
/* parameter: int * , int         */
/* return: void                   */
/* function: rank a integer array */
void sort(int *ptr,int n)
{
    int i,j;  /* loop variable */
    /* rank odd number */
    for (i=0;i<n;i++)
      for (j=n-1;j>i;j--)
        if ( ptr[j] != 0 && ptr[j]%2 ==1 )
          if ( (ptr[j]%2+ptr[j-1]%2 != 2) || (ptr[j]<ptr[j-1]) )
            swap(&ptr[j],&ptr[j-1]);
    /* rank even number */
    for (i=n-1;i>0;i--)
      for (j=0;j<i;j++)
        if ( ptr[j] != 0 && ptr[j]%2 ==0 )
          if ( (ptr[j]%2+ptr[j+1]%2 != 2) || (ptr[j]>ptr[j+1]) )
            swap(&ptr[j],&ptr[j+1]);
    /* rank result: odd number before zero, even number after zero, */
    /*              there are form small to big!                    */
}


/* define the function swap() */
/* parameter: int * ,int *    */
/* return: void               */
/* function: swap two numbers */
void swap(int *fst, int *sec)
{
   int temp;
   temp = *fst;
   *fst = *sec;
   *sec = temp;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值