import java.util.
Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of array ");
int l = sc.nextInt() ;
int [] arr = new int[l] ;
System.out.println("Enter the sorted array");
for (int i = 0; i < l; i++) {
arr[i] = sc.nextInt() ;
}
int ub = l-1;
int lb = 0;
int mid = ub/2 ;
System.out.println("Enter the element to be searched");
int toFind = sc.nextInt();
while (ub>=lb && arr[mid]!=toFind) {
int midValue = arr[mid];
if (midValue>toFind)
ub = mid-1;
if (midValue<toFind)
lb = mid+1;
mid = (ub+lb)/2 ;
}
if (arr[mid]==toFind)
System.out.println("Found");
else
System.out.println("Not found");
}
}
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of array ");
int l = sc.nextInt() ;
int [] arr = new int[l] ;
System.out.println("Fill the array");
for (int i = 0; i < l; i++) {
arr[i] = sc.nextInt() ;
}
for (int i = 0; i < l - 1; i++) {
for (int j = 0; j < l - i - 1; j++) {
if (arr[j]>arr[j+1]){
int t = arr[j];
arr [j] = arr[j+1] ;
arr[j+1] = t;
}
}
}
System.out.print("Sorted array: ");
for (int i = 0; i < l; i++) {
System.out.print(arr[i] + ", ");
}
}
}
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of array ");
int l = sc.nextInt() ;
int [] arr = new int[l] ;
System.out.println("Fill the array");
for (int i = 0; i < l; i++) {
arr[i] = sc.nextInt() ;
}
System.out.println("Enter the element to be searched");
int toFind = sc.nextInt();
int found = -1;
for (int i = 0; i < l; i++) {
if (arr[i]==toFind)
found = i ;
}
if (found!=-1)
System.out.println("Found at " + found);
else
System.out.println("Not found");
}
}
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of array ");
int l = sc.nextInt() ;
int [] arr = new int[l] ;
System.out.println("Fill the array");
for (int i = 0; i < l; i++) {
arr[i] = sc.nextInt() ;
}
for (int i = 0; i < l; i++) {
int index = i ;
for (int j = i; j < l; j++) {
if (arr[j]<arr[index])
index = j;
}
int t = arr[i];
arr[i] = arr[index] ;
arr[index] = t;
}
System.out.print("Sorted array: ");
for (int i = 0; i < l; i++) {
System.out.print(arr[i] + ", ");
}
}
}