gunawantw

bubble sort

Apr 15th, 2022
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. S = [52,29,15,19,3]
  2. console.log('Nilai s adalah ' + S);
  3. console.log('banyak angka s adalah ' + S.length);
  4.  
  5. bubbleSort(S)
  6.  
  7. function bubbleSort(array) {
  8.     console.log("Nilai array sebelum disortir adalah " + array)
  9.  
  10.   for(var i = 0; i < array.length; i++) {
  11.      
  12.    for(var j = 0; j < ( array.length - i -1 ); j++) {
  13.        
  14.      if(array[j] > array[j+1]) {
  15.          
  16.        var temp = array[j]
  17.        array[j] = array[j + 1]
  18.        array[j+1] = temp
  19.        
  20.       }
  21.    }
  22.   }
  23.     console.log("Nilai array setelah disortir adalah " + array)
  24.  
  25. }
Add Comment
Please, Sign In to add comment