笔试题目:假设一整型数组存在若干正数和负数,现在通过某种算法使得该数组的所有负数在正数的左边,
且保证负数件和正数间元素相对位置不变。时空复杂度要求分别为:o(n),o(1)。
且保证负数件和正数间元素相对位置不变。时空复杂度要求分别为:o(n),o(1)。
例如
-3 4 2 -1 7 3 -5
排序后
-3 -1 -5 4 2 7 3
/*1.0版本思想*/
考虑复杂度,我的实现方法是找到第一个postive,再找到第一个negative after the postive,然后进行类似一趟冒泡的排序,重复做下去,直到cann't find negative after the first postive.
2.0版本是对1.0版的优化
#include<stdio.h> void swap(int *a,int *b) { int temp=*a; *a=*b; *b=temp; } int main() { int