java - How to separate negative numbers and positive numbers from an array? -
i want separate negative numbers , positive numbers in array.
for example, if array has 10 values , {-8,7,3,-1,0,2,-2,4,-6,7}, want new modified array {-6,-2,-1,-8,7,3,0,2,4,7}.
i want in o(n^2) , have written code well. not getting right outputs. code wrong?
import java.util.random; public class apples { public static void main(string[] args) { random randominteger=new random(); int[] a=new int[100]; for(int i=0;i<a.length;i++) { a[i]=randominteger.nextint((int)system.currenttimemillis())%20 - 10; } for(int i=0;i<a.length;i++) { if(a[i]<0) { int temp=a[i]; for(int j=i;j>0;j--) { a[j]=a[j-1]; j--; } a[0]=temp; } } for(int i=0;i<a.length;i++) { system.out.print(a[i]+" "); } } }
you have 2 j--
while need one, remove either 1 of them.
for(int j=i;j>0;j--) { a[j]=a[j-1]; // remove j--; here }
Comments
Post a Comment