62,614
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static int[] psortArray(int[] sourceArray){
int length=sourceArray.length;
int[] temp=new int[length];
List<Integer> indexArray=new ArrayList<Integer>();
for(int i=0;i<length;i++){
if(sourceArray[i]<0){
indexArray.add(i);
}
}
int j=indexArray.size();
for(int i=0;i<j;i++){
temp[i]=sourceArray[indexArray.get(i)];
}
for(int i=0;i<length;i++){
if((j<length)&&sourceArray[i]>0){
temp[j]=sourceArray[i];
j++;
}
}
return temp;
}
public static void main(String[] args) {
int[] test={10,9,7,-5,9,-12,15};
System.out.println(Arrays.toString(psortArray(test)));
}
}
#include <stdio.h>
#include <stdlib.h>
static void arrange ( const int x[], int y[], int size )
{
int first = 0;
int last = size;
int i;
for (i = 0; i < size; i++) {
int a = x[i];
if (a < 0) {
y[first++] = a;
} else {
y[--last] = a;
}
}
first = size;
while (--first > last) {
int a = y[first];
y[first] = y[last];
y[last++] = a;
}
}
static int A[] = {1,7,-5,9,-12,15};
int main ( int argc, char *argv[] )
{
int i;
int n = argc - 1;
int *x, *y;
if (n > 0 ) {
x = malloc(n*sizeof(int));
for (i = 0; i < n; i++) {
x[i] = atoi(argv[i+1]);
}
} else {
x = A;
n = sizeof(A) / sizeof(int);
}
y = malloc(n*sizeof(int));
arrange(x,y,n);
for (i = 0; i < n; i++) {
printf("%d",y[i]);
if (i+1 < n) putchar(',');
}
putchar('\n');
return 0;
}
[BanUBB]
#include <stdio.h>
#include <stdlib.h>
static int arrange(int x[], int size, int setpos, int getpos)
{
while (getpos < size) {
int tmp = x[getpos++];
if (tmp < 0 ) {
if (getpos != setpos) x[setpos] = tmp;
setpos++;
} else {
setpos = arrange(x,size,setpos,getpos) - 1;
x[setpos] = tmp;
return setpos;
}
}
return getpos;
}
static int A[] = {1,7,-5,9,-12,15};
int main ( int argc, char *argv[] )
{
int i, pivot;
int n = argc - 1;
int *x;
if (n > 0 ) {
x = malloc(n*sizeof(int));
for (i = 0; i < n; i++) {
x[i] = atoi(argv[i+1]);
}
} else {
x = A;
n = sizeof(A) / sizeof(int);
}
pivot = arrange(x,n,0,0);
for (i = 0; i < n; i++) {
if (i > 0) {
if (i == pivot)
printf(" || ");
else
printf(",");
}
printf("%d",x[i]);
}
printf("\n");
return 0;
}
[/BanUBB]
[BanUBB]
#include <stdio.h>
#include <stdlib.h>
static void arrange ( const int x[], int y[], int size )
{
int first = 0;
int last = size;
int i;
for (i = 0; i < size; i++) {
int a = x[i];
if (a < 0) {
y[first++] = a;
} else {
y[--last] = a;
}
}
first = size;
while (--first > last) {
int a = y[first];
y[first] = y[last];
y[last++] = a;
}
}
static int A[] = {1,7,-5,9,-12,15};
int main ( int argc, char *argv[] )
{
int i;
int n = argc - 1;
int *x, *y;
if (n > 0 ) {
x = malloc(n*sizeof(int));
for (i = 0; i < n; i++) {
x[i] = atoi(argv[i+1]);
}
} else {
x = A;
n = sizeof(A) / sizeof(int);
}
y = malloc(n*sizeof(int));
arrange(x,y,n);
for (i = 0; i < n; i++) {
printf("%d",y[i]);
if (i+1 < n) putchar(',');
}
putchar('\n');
return 0;
}
[/BanUBB]