Program Bubble Sort dengan Java

Program Bubble Sort dengan Java

Hosting Unlimited Indonesia

Berikut listing program Bubble dengan Java silahkan dicoba yaaa…..
 

import java.util.Scanner; 
import static java.lang.System.out;  

public class BubleSort{     
	public static void main(String[] args){            
		Scanner in=new Scanner(System.in);            
		system.out.print("Banyak bilangan: ");
		int N=in.nextInt();            
		int bil[]=new int [N];
		
		for(int i=1;i<=N;i++){               
			system.out.print("tBilangan ke-"+i+": ");                
			bil[i-1]=in.nextInt();            
		}          
		
		int pil=0;            
		while(pil!=3){                 
			system.out.println("n[1] Urut bilangan secara Ascending");                  
			system.out.println("[2] Urut bilangan secara Descending");                  
			system.out.println("[3] Exit");                  
			system.out.print("Masukkan pilihan: ");
			pil=in.nextInt();                  
			if(pil==1){                     
				for(int i=0;i<N;i++){                         
					for(int j=0;jbil[j+1]){                                
						int tampung=bil[j];                                
						bil[j]=bil[j+1];                                
						bil[j+1]=tampung;                            
					}                         
				}
				system.out.println("nBilangan di urut secara Ascending");                     
				for(int i=0;i<N;i++){                        
					system.out.println("tBilangan ke-"+i+" : "+bil[i]);                    				
				}				
			}                     
			                 
		}else if(pil==2){                      
			for(int i=0;i<N;i++){                          
				for(int j=0;j<N;j++){                             
					if(bil[j]                                  
					int tampung=bil[j];                                                 
					bil[j]=bil[j+1];                                                 
					bil[j+1]=tampung;                                              
				}                                         
			}
			system.out.println("nBilangan di urut secara Descending");                                 
			for(int i=0;i<N;i++){                                    
				system.out.println("tBilangan ke-"+i+" : "+bil[i]);                                 
			} 
		}else if(pil==3){                                    
			System.exit(0);                          
		}else{                               
			system.out.println("Pilihan tidak ada");                          
		}                 
	}
 } 

 
Hosting Unlimited Indonesia
Share