Dat Astru Cure
Dat Astru Cure
Dat Astru Cure
import java.util.*;
public class BinarySearch {
public static void main(String[] args) {
int[] arr=new int[10];
int count =0;
Scanner read=new Scanner(System.in);
System.out.println("enter 10 integers:");
//read values
for(int i=0;i<arr.length;i++){
arr[i]=read.nextInt();
}
//cheacking array is sorted or not
for(int i=0;i<arr.length-1;i++){
if(arr[i]>arr[i+1])
{
count++;
}
}
// aftr validation
if(count==0){
int n;
System.out.print("Enter A number that you want to search:");
n=read.nextInt();
int start=0,end=arr.length,mid;
while(start<=end){
mid=(start+end)/2;
if(arr[mid]==n)
{
System.out.println("your number found at position "+(mid+1));
break;
}
if(arr[mid]<n){
start=mid+1;
}
else{
end=mid-1;
}
if (start>end){
System.out.println("Number is not found in list");
}
}
}else{
System.out.println("Enter sorted list please because,\n binary search works on
only sorted list");
}
}
}
}
}
import java.util.Scanner;
#include<iostream>
using namespace std;
for(int i=0;i<N-1;i++){
cout<<arr[i]<<" ";
}
}
int main(){
int N,del;
cout<<"Enter size of Array:";
cin>>N;
int arr[N];
initarr(arr,N);
cout<<"Enter index that you want to delete:";
cin>>del;
if(del>N-1)
{
cout<<"Invalid Index";
}
else{
for(int i=del;i<N-1;i++){
arr[i]=arr[i+1];
}
}
printarr(arr,N);
}