fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void segregateInPlace(int[] arr) {
  11. int n = arr.length;
  12.  
  13. for (int i = 1; i < n; i++) {
  14. if (arr[i] >= 0) {
  15. int key = arr[i];
  16. int j = i - 1;
  17.  
  18. // Shift negative elements to the right
  19. // until we find correct position for key
  20. while (j >= 0 && arr[j] < 0) {
  21. arr[j + 1] = arr[j];
  22. j--;
  23. }
  24. arr[j + 1] = key;
  25. }
  26. }
  27. }
  28. public static void main (String[] args) throws java.lang.Exception
  29. {
  30. // your code goes here
  31. Scanner sc = new Scanner(System.in);
  32. int n = sc.nextInt();
  33. int[] arr = new int[n];
  34. for (int i = 0; i < n; i++) {
  35. arr[i] = sc.nextInt();
  36. }
  37.  
  38. System.out.println(Arrays.toString(arr));
  39. }
  40. }
Success #stdin #stdout 0.12s 56420KB
stdin
10
6 4 -2 5 -4 2 -8 -5 7 10
stdout
[6, 4, -2, 5, -4, 2, -8, -5, 7, 10]