Guest User

Untitled

a guest
Apr 5th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. /**
  2. * Write a description of class InsertionSort here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. public class InsertionSort
  8. {
  9. public static void insertionSort(int array[]){
  10. int n = array.length;
  11. for(int j=1; j<n; j++){
  12. int key = array[j];
  13. int i = j-1;
  14. while((i > -1) && (array[i] > key)){
  15. array[i+1] = array[i];
  16. i--;
  17. }
  18. array[i+1] = key;
  19. }
  20. }
  21. public static void main(String a[]){
  22. int[] arr1 = {9,14,3,2,43,11,58,22};
  23. System.out.println("Before Insertion Sort");
  24. for(int i:arr1){
  25. System.out.print(i+ " ");
  26. }
  27. System.out.println();
  28.  
  29. //sorting array using insertion sort
  30. insertionSort(arr1);
  31. System.out.println("After Insertion Sort");
  32. for(int i:arr1){
  33. System.out.print(i+" ");
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment