0% found this document useful (0 votes)
15 views5 pages

Oops Assaigment in Java

The document discusses binary search, linear search, insertion in arrays, and finding duplicate elements in an array in Java. It includes code examples for each topic.

Uploaded by

Ikrash official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Oops Assaigment in Java

The document discusses binary search, linear search, insertion in arrays, and finding duplicate elements in an array in Java. It includes code examples for each topic.

Uploaded by

Ikrash official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Islamia College Peshawar

Assignment of java (oop-2)


Dept : Software Engineering
Name : Muhammad Irfan
Roll: 221108
Section: A
Topic :
(i) Binary search
(ii) Linear search
(iii) Insertion in array
(iv) Duplicate of array elements
Binary search :
import java.util.Scanner;

public class binary_search {


public static void main(String[] args) {
System.out.println("Binary Search in array");
int arr[]={2,3,4,5,6,7,8,9,10,11,13,14,16};
for(int i:arr){
System.out.print("\t"+i);
}
System.out.println();
Scanner in = new Scanner(System.in);
int lb =0;
int ub = arr.length-1;
int mid =(lb+ub)/2;
System.out.println("enter the item that you search ");
int item = in.nextInt();
while(lb<ub&&arr[mid]!=item){
if(arr[mid]<item){
lb =mid+1;
mid = (lb+ub)/2;
}else {
ub = mid-1;
mid = (lb+ub)/2;
}
}
int loc =-1;
if(arr[mid]==item){
loc = mid;
}else {
loc =-1;
}
if(loc ==-1){
System.out.println("number is not found : ");
}else {
System.out.println("number is found at index :
"+(loc+1));
}
}
}

Linear search in array:


import java.util.Scanner;

public class linear_search_in_array {


static int search(int ar[],int lenght,int item){
int i=0;

while(i<ar.length){
if(ar[i]==item) {
return (i);

}
i++;

}
return -1;
}
public static void main(String[] args) {
// linear search in array
System.out.println("linear search in array");
int arr[]={9,8,7,6,5,4,3,2,1};
for(int i=0;i<arr.length;i++){
System.out.print("\t"+arr[i]);
}
// linear seaching in array
System.out.println();
System.out.println("enter the item that you search");
Scanner in = new Scanner(System.in);
int item = in.nextInt();
int result= search(arr,arr.length,item);
if(result==-1){
System.out.println("number not found");
}else {
System.out.println("number found at location :
"+(result+1));
}

}
}
Insertion in array:
import java.util.Scanner;

// inserrtion in array
public class Main{
public static void main(String[] args) {
System.out.println("insertion in linear array ");
int arr[]={2,3,4,5,6,7,8,9};
// displying elements
for(int i:arr){
System.out.print("\t"+i);
}
System.out.println();
System.out.println("enter the item that you insert ");
Scanner in = new Scanner(System .in);
int item = in.nextInt();
System.out.println("enter the location that you insert");
int loc = in.nextInt();
int lenght = arr.length-1;
int i = lenght;
int kk = loc;
while(i>=kk){
arr[i+1]=arr[i];
}
arr[loc]=item;
lenght++;

System.out.println("array after insertion ");

for(int k=0;k<arr.length+1;k++){
System.out.println("\t"+arr[k]);
}
}
}
Duplication of array elements:

public class Main{


public static void main(String[] args) {
// duplication of arrray elements
System.out.println(" linera array ");
int [] arr = new int [] {1, 2, 3,3, 2, 7, 8, 7};
for(int i=0;i<arr.length;i++){
System.out.print("\t"+arr[i]);
}
System.out.println();
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j])
System.out.println("duplicate elements : "+arr[j]);

}
}
}
}

You might also like