ihsan1

java

May 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package array;
  2. import java.util.*;
  3. public class Array {
  4.     public static void main(String[] args) {
  5.         int [] fredy = new int [10];
  6.         Integer [] yohanes = {9,3,8,7,2};
  7.        
  8.         fredy[1]=5;
  9.         fredy[3]=7;
  10.         fredy[8]=3;
  11.        
  12.        
  13.         int i = 0 ;
  14.         for ( i = 0 ; i < fredy.length ; i++)
  15.             System.out.print(fredy[i] + " ");
  16.         System.out.println();
  17.        
  18.         for ( i = 0 ; i < yohanes.length ; i++)
  19.             System.out.print(yohanes[i] + " ");
  20.        
  21.         System.out.println();
  22.        
  23.         System.out.println(Arrays.toString(fredy));
  24.         System.out.println();
  25.         System.out.println(Arrays.toString(yohanes));
  26.         System.out.println();
  27.         Arrays.sort(yohanes);
  28.         System.out.println(Arrays.toString(yohanes));
  29.         System.out.println();
  30.         Arrays.binarySearch(fredy, 5);
  31.        
  32.         Arrays.sort(yohanes, Collections.reverseOrder());
  33.         System.out.println("Besar ke kecil");
  34.         for (Integer yohane : yohanes) {
  35.             System.out.println(yohane); //data1 = fredy , data2 = yohanes
  36.         }
  37.        
  38.  
  39.            
  40.            
  41.         }
  42.        
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. package array;
  53. import java.util.*;
  54. public class LatSearch {
  55.     public static void main (String [] args){
  56.         sequential();
  57.         System.out.println(" ");
  58.         binarysearch();
  59.     }                                                  
  60.     public static void sequential (){
  61.        
  62.        int [] wildan = {1, 2, 4, 8, 12};
  63.        
  64.        
  65.         System.out.println(Arrays.toString(wildan));
  66.        
  67.         //metode Sequential Search
  68.        
  69.         boolean ketemu = false ; int x = 8;
  70.         for (int i = 0 ; i < wildan.length ; i++){
  71.             if (wildan[i] == x)
  72.                 ketemu = true;
  73.         }
  74.         if  (ketemu)
  75.             System.out.println("Data ditemukan");
  76.        
  77.         else
  78.             System.out.println("Data tak ditemukan");
  79.     }
  80.    
  81.     public static void binarysearch(){
  82.           int arr[] = {10,20,30,40,50};  
  83.         int key = 10;  
  84.         int result = Arrays.binarySearch(arr,key);  
  85.         if (result < 0)  
  86.             System.out.println("Data tidak ketemu");  
  87.         else  
  88.             System.out.println("Data ketemu di: "+result);  
  89.     }  
  90.  
  91.     }
Add Comment
Please, Sign In to add comment