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[] numeros = { 1, 5, 3, 25, 12, 6, 7, 2, 87, 44, 31, 0, -1, 4 };
  13. insertionSort(numeros);
  14. printVetor(numeros);
  15. }
  16.  
  17. static void printVetor(int[] vetor) {// essa logica serve apenas para exibir meu vetor
  18. for (int i = 0; i < vetor.length; i++) {
  19. System.out.println(vetor[i]);
  20. }
  21. }
  22.  
  23. static void insertionSort(int[] vetor) {
  24. int chave, valor;
  25. for (int i = 1; i < vetor.length; i++) {
  26. chave = i;
  27. valor = vetor[i];
  28. while (chave > 0 && valor < vetor[chave - 1]) {
  29. vetor[chave] = vetor[chave - 1];
  30. chave--;
  31. }
  32. vetor[chave] = valor;
  33. }
  34. }
  35. }
Success #stdin #stdout 0.08s 32464KB
stdin
Standard input is empty
stdout
-1
0
1
2
3
4
5
6
7
12
25
31
44
87