程序高手来,一个数据结构的问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 16:44:50
已知道一个线形表 a1 a2 a3...an,按顺序存于内存中,每个都是整数,设计用最少时间把负数的整数移到正数的前面的算法。方法很多,时间要最少
楼上回答的是C++的解决方法,我需要的是C语言的数据结构的算法解决,但还是非常感谢

快速排序思想中的 partion 算法 选中间值为0 i , j 分别从左 右 开始扫描
#include <iostream>
#include <algorithm>
#include <iterator>
#include <ctime>

using namespace std ;

struct Ope
{
void operator () ( int &a )
{
a = rand() % 100 - 30;
}
};

void adjust( int *a, int len)
{
int i = 0;
int j = len;
while (true)
{
while ( a[i] < 0 )
++i;
while ( a[j] >= 0 )
--j;
if ( i >= j )
return ;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

int main()
{
srand(unsigned int ( time(NULL)));
const int MaxSize = 12;
int a[MaxSize];
for_each(a,a+MaxSize,Ope () );
copy(a,a+MaxSize,ostream_iterator<int> (cout," ") );
cout << endl;
adjust(a,MaxSize);
copy(a,a+MaxSize,ostream_iterator<int> (cout," ") );