saban123

PROGRAM PENGURUTAN INSERTION SORT

Dec 10th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. class node{
  2.         int data;
  3.         node prev = null;
  4.         node next = null;
  5.         node(int data){
  6.             this.data = data;
  7.         }
  8.     }
  9.  
  10.  
  11. class sayembara{
  12.     node head=null;
  13.     node tail=null;
  14.  
  15.     void addback(int b){
  16.         node baru = new node(b);
  17.         if(head==null){
  18.             head=baru;
  19.             tail=baru;
  20.         }else{
  21.             baru.prev=tail;
  22.             tail.next=baru;
  23.             tail=baru;
  24.         }
  25.     }
  26.  
  27.  
  28. public void insertionsort(){
  29.   node temp=head;
  30.   int n=0;
  31.   for(node i=temp.next;i!=null;i=i.next){
  32.    for(node j=i;j.prev!=null;j=j.prev){
  33.  
  34.     n=n+1;
  35.     System.out.println("itarasi ke "+n);
  36.     tampil();    
  37.     if(j.data<j.prev.data){
  38.         int tmp;
  39.         tmp=j.data;
  40.         j.data=j.prev.data;
  41.         j.prev.data=tmp;
  42.    
  43.     }
  44.      }
  45.    }
  46.   }
  47.  
  48. void tampil(){
  49.         node tmp=head;
  50.         if(head==null){
  51.             System.out.println("List Kosong");
  52.         }else{
  53.             while(tmp !=null){
  54.                 System.out.print(tmp.data +" ");
  55.                 tmp=tmp.next;
  56.             }
  57.         }System.out.println();
  58.     }
  59. public static void main (String [] args){
  60.         sayembara x= new sayembara();
  61.  
  62.             x.addback(2);
  63.         x.addback(6);
  64.         x.addback(13);
  65.         x.addback(19);
  66.         x.addback(46);
  67.         x.addback(35);
  68.         x.tampil();
  69.  
  70. System.out.println("pengurutan dengan buble sort :");
  71.         x.insertionsort();
  72.  
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment