Advertisement
Zeill

BubbleSort Java

Oct 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. public static void main(String[] args) {
  2.        
  3.         int[] numeros = {5,8,17,1,3,9,12};
  4.        
  5.         bubbleSort(numeros);
  6.        
  7.         for (int numero : numeros) {
  8.             System.out.print(numero + ",");
  9.         }
  10.        
  11.     }
  12.    
  13.     static void bubbleSort(int[] arr) {  
  14.         int n = arr.length;  
  15.         int temp = 0;  
  16.        
  17.         for(int i=0; i < n; i++){
  18.            
  19.             for(int j=1; j < (n-i); j++){  
  20.                
  21.                 if(arr[j-1] > arr[j]){  
  22.                     //Intercambiar Elementos
  23.                     temp = arr[j-1];  
  24.                     arr[j-1] = arr[j];  
  25.                     arr[j] = temp;  
  26.                    
  27.                 }  
  28.             }  
  29.         }  
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement