//奇偶数排序:
#include<iostream>
using namespace std;
#define NULL 0
//void insert_sort(int *s, int length)
//{
// int temp;
// int j;
// for (int i = 2; i <= length; i++)
// {
// int temp = s[i];
// int j = i - 1;
// while (s[j] > temp)
// {
// s[j + 1] = s[j];
// j = j - 1;
// }
// s[j + 1] = temp;
// }
//}
bool isoddnumber(int n)
{
return n % 2 != 0;
}
void oddevensort(int *pdata, unsigned int length)
{
if (pdata == NULL|| length == 0)
{
return;
}
int* pbegin = pdata;
int* pend = pdata + length - 1;
while (pbegin < pend)
{
if (isoddnumber(*pbegin))
{
pbegin++;
}
else
if (!isoddnumber(*pend))
{
pend--;
}
else
{
swap(*pbegin, *pend);
}
}
}
//测试程序:
int main()
{
int s[] = { 1, 5, 6, 8, 3, 7, 14, 51 };
int num = sizeof(s) / sizeof(s[0]);
oddevensort(s, num);
for (int i = 0; i < num; i++)
cout << s[i] << " ";
cout << endl;
return 0;
}
#include<iostream>
using namespace std;
#define NULL 0
//void insert_sort(int *s, int length)
//{
// int temp;
// int j;
// for (int i = 2; i <= length; i++)
// {
// int temp = s[i];
// int j = i - 1;
// while (s[j] > temp)
// {
// s[j + 1] = s[j];
// j = j - 1;
// }
// s[j + 1] = temp;
// }
//}
bool isoddnumber(int n)
{
return n % 2 != 0;
}
void oddevensort(int *pdata, unsigned int length)
{
if (pdata == NULL|| length == 0)
{
return;
}
int* pbegin = pdata;
int* pend = pdata + length - 1;
while (pbegin < pend)
{
if (isoddnumber(*pbegin))
{
pbegin++;
}
else
if (!isoddnumber(*pend))
{
pend--;
}
else
{
swap(*pbegin, *pend);
}
}
}
//测试程序:
int main()
{
int s[] = { 1, 5, 6, 8, 3, 7, 14, 51 };
int num = sizeof(s) / sizeof(s[0]);
oddevensort(s, num);
for (int i = 0; i < num; i++)
cout << s[i] << " ";
cout << endl;
return 0;
}