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 main (String[] args) throws java.lang.Exception
  11. {
  12. int[] myArray = {5, 7, 2, 8, 1, 0, 3};
  13. int max = 0;
  14. int l = myArray.length;
  15. int index =0;
  16. int temp = 0;
  17. while (l>0){
  18. for (int i=0;i<l;i++){
  19. if(max<=myArray[i]){
  20. max=myArray[i];
  21. index = i;
  22. }
  23. }
  24. temp = myArray[(l - 1)];
  25. myArray[index] = temp;
  26. myArray[(l - 1)] = max;
  27. String s = " Iteration - " + l + " MAX INDEX = " + index + " temp = " + temp + " MAX = " + max;
  28. System.out.println(s);
  29. index = 0;
  30. max = 0;
  31. l = l-1;
  32. }
  33. for (int v : myArray){
  34. System.out.print(v);
  35. }
  36. }
  37. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
 Iteration - 7 MAX INDEX = 3 temp = 3 MAX = 8
 Iteration - 6 MAX INDEX = 1 temp = 0 MAX = 7
 Iteration - 5 MAX INDEX = 0 temp = 1 MAX = 5
 Iteration - 4 MAX INDEX = 3 temp = 3 MAX = 3
 Iteration - 3 MAX INDEX = 2 temp = 2 MAX = 2
 Iteration - 2 MAX INDEX = 0 temp = 0 MAX = 1
 Iteration - 1 MAX INDEX = 0 temp = 0 MAX = 0
0123578