Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TestWork {
  4. public static void main(String[] args) {
  5. int[] x = {2, 64, 12, 123, 92, 3, 14};
  6.  
  7. for(int a:insertionSort(x)){
  8. System.out.println(a);
  9. }
  10. }
  11.  
  12. public static int[] insertionSort(int[] x){
  13. int valueToInsert;
  14. int holePosition;
  15.  
  16. for (int i=1; i<x.length;i++){
  17.  
  18. valueToInsert = x[i];
  19. holePosition = i;
  20. while( (holePosition > 0) && (x[holePosition-1] > valueToInsert)){
  21. x[holePosition] = x[holePosition-1];
  22. holePosition = holePosition -1;
  23.  
  24. }
  25. x[holePosition] = valueToInsert;
  26.  
  27. }
  28. return x;
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement