64,643
社区成员
发帖
与我相关
我的任务
分享
private static void changeorder(){
int o[]={-5,7,1,9,-12,15 };
int size=o.length;
int []temp=new int[size];
int lastIndex=0;//负数下标
int lastIndex_1=o.length;////正数下标
for (int i = 0; i < o.length; i++) {
if(o[i]<0){
temp[lastIndex] = o[i];
lastIndex++;
}
if(o[size-1-i] > 0){
temp[lastIndex_1-1]=o[size-1-i];
lastIndex_1--;
}
}
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
int *pSourElem; // 原始链表.
int *pPositivElem = 0; // 正数位置.
int *pNegativElem = 0; // 负数位置.
for(pSourElem = head; pSourElem; pSourElem移动到下一个.)
{
if(pSourElem < 0) // 负数.
{
if(!pNegativElem) // 遇到第一个负数.
pNegativElem = pSourElem;
else
// 将pSourElem取出这个放到pNegativElem的下一个.
// 然后更新pNegativElem
}
else // 正数
{
//同上.
}
}
#include<iostream>
using namespace std;
int main()
{
int a[6]={-5,7,1,9,-12,15};
int i,j,k,t;
j=0;
for(i=0;i<6;i++)
{
if(a[i]<0)
{
t=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[j]=t;
j++;
}
}
for(i=0;i<6;i++)
cout<<a[i]<<" ";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a[6]={-5,7,1,9,-12,15};
int i,j,k,t;
j=0;
for(i=0;i<6;i++)
{
if(a[i]<0)
{
t=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[j]=t;
j++;
}
}
for(i=0;i<6;i++)
cout<<a[i]<<" ";
return 0;
}
package com.infosys.arithmetic;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class SpecifiedSort {
public static void main(String[] args) {
Integer[] data = { -5, 7, 1, 9, -12, 15 };
System.out.println(Arrays.toString(data));
specifiedSort(data);
System.out.println(Arrays.toString(data));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void specifiedSort(Comparable data[]) {
int negative = 0;
Queue<Comparable> queue = new LinkedList<Comparable>();
for (int index = data.length - 1; index >= 0; index--) {
if (data[index].compareTo(0) > 0 && index != data.length - 1
&& negative != 0) {
data[index + negative] = data[index];
}
if (data[index].compareTo(0) < 0) {
negative++;
queue.offer(data[index]);
}
}
for (int i = negative - 1; i >= 0; i--) {
data[i] = queue.poll();
}
}
}
最后打印出来的结果是:
[-5, 7, 1, 9, -12, 15]
[-5, -12, 7, 1, 9, 15]