Advertisement
Samuel_Berkat_Hulu

Selection Sort

Apr 6th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Selection_Sort here.
  4.  *
  5.  * @Samuel Berkat Hulu
  6.  * @version 5.0
  7.  */
  8. import java.util.*;
  9. public class Selection_Sort{
  10. public static void selsort(int[] arr)
  11. {
  12.     int n=arr.length;
  13.     for(int i=0;i<n-1;i++)
  14.     {
  15.         int MIN=i;
  16.         System.out.println("Sorting berdasarkan nomor "+(i+1));
  17.  
  18.         for(int j=i+1;j<n;j++)
  19.             {
  20.                 System.out.println("Perbandingan "+ arr[MIN] + " dan " + arr[j]);
  21.                 if(arr[j]<arr[MIN])
  22.                     {
  23.                     System.out.println(arr[MIN] + " Lebih besar dari " + arr[j] );
  24.                     MIN=j;
  25.             }
  26.         }
  27.             int temp=arr[i];
  28.             arr[i]=arr[MIN];
  29.             arr[MIN]=temp;
  30.     }
  31. }
  32. public static void main(String[] args) {
  33.     int[] arr= {15,21,6,3,19,20};
  34.         System.out.println("Elemen array sebelum di Sorting: "+ Arrays.toString(arr));
  35.             selsort(arr);
  36.                 System.out.println("Eleme array setelah di Sorting: "+Arrays.toString(arr));
  37. }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement