Nothing Special   »   [go: up one dir, main page]

Dat Astru Cure

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

program name: binary search algorithm

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");
}
}
}

2.. bubble sort data structure

public class BubbleSort {


public static void main(String[] args) {
int[] arr={40,10,105,99,1,100,121,17,7,90};
int temp;
for(int i=0;i<arr.length;i++){
for(int j=1;j<arr.length-i-1;j++)
{
if(arr[i]>arr[i+1]){
temp=arr[i+1];
arr[i]=temp;
arr[i+1]=arr[i];
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}

}
}

3.. dejavu code

import java.util.Scanner;

public class DejaVu {


public static void main(String[] args) {
Scanner read=new Scanner(System.in);
System.out.print("Enter the String:");
String str=read.nextLine();
int count=0;
for(int i=0;i<str.length();i++)
{
for(int j=0;j<str.length();j++)
{
if(str.charAt(i)==str.charAt(j)&&i!=j)
{
count++;
}
}
}
if (count>0) {
System.out.println("Deja Vu");
}
else
{
System.out.println("Unique");
}
}
}

4...deletion code in c++

#include<iostream>
using namespace std;

void initarr(int arr[],int N){


cout<<"Enter array "<<N<<" Elements:";
for(int i=0;i<N;i++){
cin>>arr[i];
}
}
void printarr(int arr[],int N){

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);
}

4.. linear seach


#include<iostream>
using namespace std;
class Linear{
int arr[10];
int n=10;
public:
void getnos();
void search();
};
void Linear::getnos(){
cout<<"Enter 10 num in sorted order:";
for(int i=0;i<n;i++){
cin>>arr[i];
}
}
void Linear::search(){
int start=0,end=n-1,med=(start+end)/2,item;
cout<<"Enter number to search:";
cin>>item;
cout<<item<<"item";
cout<<med<<"med";
cout<<start<<"start";
cout<<end<<"end";
cout<<arr[2]<<"arr2";
while(end>start)
{
if(item==arr[med])
{
cout<<"item found at "<<med<<" position";
break;
}
if(item>arr[med])
{
start=med+1;
}
else
{
end=med-1;
}
}
}
int main(){
Linear b;
b.getnos();
b.search();
}

You might also like