189 8069 5689

调整数组使奇数全部都位于偶数前面

#include

void show(int *arr, int len)//输出函数
{
    int i;
    for (i = 0; i < len; i++)
    {
        printf("%d ", arr[i]);
    }
}
void Swap_arr(int *arr, int len)
{
    int left = 0;
    int right = len - 1;
    while (left < right)
    {
        while (left < right&&arr[left] % 2 == 1)
        {
            left++;
        }
        while (left < right&&arr[right] % 2 == 0)
        {
            right--;
        }
        if (left < right)
        {
            int temp;
            temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
        }
    }
        show(arr, len);
}
int main()
{
    int arr[] = {1,2,3,4,5,6,7,8,9};
    int len = sizeof(arr) / sizeof(arr[0]);
    Swap_arr(arr, len);
    return 0;
}

网页标题:调整数组使奇数全部都位于偶数前面
URL网址:http://cdxtjz.cn/article/jcojie.html

其他资讯