[go: up one dir, main page]

0% found this document useful (0 votes)
8 views3 pages

Arrays Programs

The document contains Java implementations of four algorithms: Binary Search, Bubble Sort, Linear Search, and Selection Sort. Each algorithm prompts the user to input the length of an array and its elements, followed by performing the respective search or sort operation. The results of the operations are printed to the console.

Uploaded by

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

Arrays Programs

The document contains Java implementations of four algorithms: Binary Search, Bubble Sort, Linear Search, and Selection Sort. Each algorithm prompts the user to input the length of an array and its elements, followed by performing the respective search or sort operation. The results of the operations are printed to the console.

Uploaded by

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

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

You might also like