Advertisement
Samuel_Berkat_Hulu

Bubble Sort

Apr 6th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Bubble_Sort here.
  4.  *
  5.  * @Samuel Berkat Hulu
  6.  * @version 5.0
  7.  */
  8.  
  9. import java.util.Scanner;
  10. public class Bubble_Sort
  11. {
  12.     static void bubbleSort(int[] arraytest)
  13.     {
  14.         int n = arraytest.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(arraytest[j-1] > arraytest[j])
  22.               {
  23.                   temp = arraytest[j-1];
  24.                   arraytest[j-1] = arraytest[j];
  25.                   arraytest[j] = temp;
  26.               }
  27.           }
  28.         }
  29.     }
  30.      public static void main(String[] args)
  31.      {
  32.          int arraytest[] = {2,10,6,40,70,35,29,9,523,234,900,102,400};
  33.          System.out.println("Array sebelum Bubble Sort");
  34.          for(int i=0; i<arraytest.length; i++)
  35.          {
  36.              System.out.print(arraytest[i] + " ");
  37.          }
  38.          System.out.println();
  39.          bubbleSort(arraytest);
  40.          System.out.println("Array sesudah Bubble Sort");
  41.          for(int i=0; i<arraytest.length; i++){
  42.              System.out.print(arraytest[i] + " ");
  43.         }
  44.          
  45.      }
  46.      
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement