Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pertemuan3;
- public class nomor4{
- public static void nomor4 (
- int arr[], int awal, int akhir, int key){
- int tengah = (awal + akhir)/2;
- while( awal <= akhir ){
- if ( arr[tengah]< key ){
- awal = tengah + 1;
- }else if ( arr[tengah] == key ){
- System.out.printf("\nBerada di Index ke-" + tengah);
- break;
- }else{
- akhir = tengah - 1;
- }
- tengah = (awal + akhir)/2;
- }
- if ( awal > akhir ){
- System.out.println("\nElement is not found!");
- }
- }
- public static void nomor4(int[] arr, int temp){
- for(int i = 0; i<arr.length-1; i++) {
- for(int j=0; j<arr.length-i-1; j++){
- if(arr[j]>arr[j+1]){
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- }
- public static void main (String[]args){
- int arr1[] = {19, 22, 89, 26, 77, 20, 24, 56, 8, 4, 92, 93, 10};
- int key = 24;
- int akhir = arr1.length-1;
- System.out.println("Data Setelah Diurutkan");
- for(int i:arr1){
- System.out.print(i+" ");
- }
- System.out.printf("\nElement yang Dicari: %d",key);
- nomor4(arr1,0,akhir,key);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement