交叉排序

第一题:输入一串数字,奇数升序,偶数降序排列

#include<iostream>
using namespace std;
//输入一串数字,奇数升序,偶数降序排列
bool numberJudge(int n);
void bubbleSort(int * num, int n);
void quickSort(int *num, int left, int right);
int main()
{	
	//输入
	int array[100] = { 0 }, odd[50] = { 0 }, even[50] = {0},i=0,oddP=0,evenP=0;
	char c;
	cin >> array[i++];
	while ((c = getchar()) != '\n')
	{
		cin >> array[i++];
	}
	getchar();
	//进行遍历分类
	i = 0;
	while (array[i] != 0)
	{
		cout << array[i] << "";
		if (numberJudge(array[i]))
		{
			odd[oddP] = array[i];
			oddP++;
			
		}
		else
		{
			even[evenP]= array[i];
			evenP++;
			
		}
		i++;
	}
	//分别对奇偶数组进行排序
	//奇数升序
	bubbleSort(odd,oddP);
	
	//偶数降序
	quickSort(even,0,evenP-1);
	
	//合并数组
	oddP = 0;
	evenP = 0;
	i = 0;
	while (array[i] != 0)
	{
		if (numberJudge(array[i]))
		{
			array[i] = odd[oddP];
			oddP++;
		}
		else
		{
			array[i] = even[evenP];
			evenP++;
		}
		cout << array[i] << " ";
		i++;
	}
	return 0;
}

/**
*奇偶判断,true为奇数,false为偶数
*/
bool numberJudge(int n)
{
	if (n % 2 == 0)return false;
	return true;
}
/*
冒泡排序(升序)
*/
void bubbleSort(int * num,int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (num[i] > num[j])
			{
				int temp = num[i];
				num[i] = num[j];
				num[j] = temp;
			}
		}
	}
}
/*
快速排序(降序)
*/
void quickSort(int *num,int left,int right)
{
	int i = left, j = right;
	int key = num[left];
	while (i < j)
	{
		while (i<j&&num[j] <= key)j--;
		if (i < j)
		{
			int temp = num[j];
			num[j] = num[i];
			num[i] = temp;
			i++;
		}
		while (i<j&&num[i] >= key)i++;
		if (i < j)
		{
			int temp = num[j];
			num[j] = num[i];
			num[i] = temp;
			j--;
		}
	}
	if(left<i)quickSort(num,  left, i - 1);
	if(i<right)quickSort(num,i + 1, right);
}

测试数据:4 6 2 3 6  7 8 1

第二题:输入一串数字,将奇数位升序,将偶数位降序排列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值