Advertisement
Telor_Goreng0

nomor 4 pertemuan 3

Apr 10th, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. package pertemuan3;
  2. public class nomor4{
  3. public static void nomor4 (
  4. int arr[], int awal, int akhir, int key){
  5. int tengah = (awal + akhir)/2;
  6. while( awal <= akhir ){
  7. if ( arr[tengah]< key ){
  8. awal = tengah + 1;
  9. }else if ( arr[tengah] == key ){
  10. System.out.printf("\nBerada di Index ke-" + tengah);
  11. break;
  12. }else{
  13. akhir = tengah - 1;
  14. }
  15. tengah = (awal + akhir)/2;
  16. }
  17. if ( awal > akhir ){
  18. System.out.println("\nElement is not found!");
  19. }
  20. }
  21. public static void nomor4(int[] arr, int temp){
  22. for(int i = 0; i<arr.length-1; i++) {
  23. for(int j=0; j<arr.length-i-1; j++){
  24. if(arr[j]>arr[j+1]){
  25. temp = arr[j];
  26. arr[j] = arr[j+1];
  27. arr[j+1] = temp;
  28. }
  29. }
  30. }
  31. }
  32. public static void main (String[]args){
  33. int arr1[] = {19, 22, 89, 26, 77, 20, 24, 56, 8, 4, 92, 93, 10};
  34. int key = 24;
  35. int akhir = arr1.length-1;
  36. System.out.println("Data Setelah Diurutkan");
  37. for(int i:arr1){
  38. System.out.print(i+" ");
  39. }
  40. System.out.printf("\nElement yang Dicari: %d",key);
  41. nomor4(arr1,0,akhir,key);
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement