Advertisement
tiffprag

Bubble Sort

Jan 20th, 2020
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. public class Test{
  2.     static int MAX = 6;
  3.     public void BubbleSort(int A[]){
  4.         int i,j, current;
  5.         for(i=0; i<MAX;i++){  
  6.             for (j=0; j<MAX-1; j++){  
  7.                 if(A[j]>A[j+1]){              
  8.                     current = A[j];              
  9.                     A[j] = A[j+1];              
  10.                     A[j+1] = current;          
  11.                 }        
  12.             }          
  13.         }
  14.     }
  15.     public static void main(String[] args){
  16.         int num[]={39,54,28,69,17,1};
  17.         System.out.println("Original: ");
  18.         for(int i = 0; i<MAX; i++){
  19.             if(i !=MAX-1){
  20.                 System.out.println(num[i] + ",");
  21.             }else{
  22.                 System.out.println(num[i]);
  23.             }
  24.         }
  25.         System.out.println("");
  26.  
  27.         Test b1 = new Test();
  28.  
  29.         b1.BubbleSort(num);
  30.         System.out.println("Sorted: ");
  31.         for(int i = 0; i<MAX; i++){
  32.             if(i !=MAX-1){
  33.                 System.out.println(num[i] + ",");
  34.             }else{
  35.                 System.out.println(num[i]);
  36.             }
  37.         }
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement