#include <iostream>
using namespace std;
inline bool IsOdd(int *a)
{
return *a % 2 == 0;
}
void Sort(int a[], int len)
{
if (a == NULL || len == 0)
return;
else
{
int *p1 = a, *p2 = a + len - 1;
while (p2 > p1)
{
while (!IsOdd(p1))++p1;
while (IsOdd(p2))--p2;
if (p2 > p1)swap(*p1, *p2);
}
}
for (int i = 0; i != len; ++i)
cout << a[i] << endl;
}
int main()
{
int a[] = { 0,1,2,3,4,5,6,7 };
Sort(a, 8);
system("pause");
return 0;
}
這是不穩定的排序,時間複雜度O(n),空間複雜度O(1)。穩定的排序今天一時沒寫出來,先挖空,改天更。
講道理,穩定排序的時間複雜度爲O(n2).