Advertisement
xxrvnge

Remedial

Nov 13th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package insertion;
  7.  
  8. /**
  9.  *
  10.  * @author xxrvnge
  11.  */
  12. public class Insertion {
  13.   public static void Insertion(int array[]) {  
  14.         int n = array.length;  
  15.         for (int j = 1; j < n; j++) {  
  16.             int key = array[j];  
  17.             int i = j-1;  
  18.             while ( (i > -1) && ( array [i] > key ) ) {  
  19.                 array [i+1] = array [i];  
  20.                 i--;  
  21.             }  
  22.             array[i+1] = key;  
  23.         }  
  24.     }  
  25.     /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) {
  29.         // TODO code application logic here
  30.        
  31.        int[] arr1 = {17,10,13,15,18,12,14,16};    
  32.         System.out.println("Sebelum Insertion Sort");    
  33.         for(int i:arr1){    
  34.             System.out.print(i+" ");    
  35.         }    
  36.         System.out.println();    
  37.            
  38.         Insertion(arr1);//sorting array using insertion sort    
  39.            
  40.         System.out.println("Sesudah Insertion Sort");    
  41.         for(int i:arr1){    
  42.             System.out.print(i+" ");    
  43.         }    
  44.     }    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement