0% found this document useful (0 votes)
6 views4 pages

Experiment 1 Java

The document describes four algorithms for searching and sorting in Java: Linear Search, Binary Search, Selection Sort, and Insertion Sort. Each section includes sample code that prompts the user for input, performs the respective algorithm, and outputs the results. The examples demonstrate how to search for elements in an array and how to sort an array of integers.

Uploaded by

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

Experiment 1 Java

The document describes four algorithms for searching and sorting in Java: Linear Search, Binary Search, Selection Sort, and Insertion Sort. Each section includes sample code that prompts the user for input, performs the respective algorithm, and outputs the results. The examples demonstrate how to search for elements in an array and how to sort an array of integers.

Uploaded by

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

EXPERIMENT : 2

SEARCHING AND SORTING

1.LINEAR SEARCH:
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the element to search for: ");
int x = scanner.nextInt();
int position = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
position = i;
break;
}
}
if (position != -1) {
System.out.println("Element found at position " + (position + 1));
} else {
System.out.println("Element not found in the array");
}
scanner.close();
}
}
Output:
Enter the number of elements in the array: 3
Enter the array elements:
50
12
13
Enter the element to search for: 12
Element found at position 1
2.BINARY SEARCH
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the no of elements in an array: ");
int n = scanner.nextInt();
int[] arr1 = new int[n];
System.out.println("Enter the array elements in sorted order:");
for (int i = 0; i < n; i++) {
arr1[i] = scanner.nextInt();
}
System.out.print("Enter the search element: ");
int x = scanner.nextInt();
int result = binarySearch(arr1, x);
if (result == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index " + result);
}
scanner.close();
}
public static int binarySearch(int[] list1, int x) {
int first = 0;
int last = list1.length - 1;
while (first <= last) {
int midpoint = (first + last) / 2;
if (list1[midpoint] == x) {
return midpoint;
} else if (x < list1[midpoint]) {
last = midpoint - 1;
} else {
first = midpoint + 1;
}
}
return -1;
}
}

Output:
Enter the no of elements in an array: 5
Enter the array elements in sorted order:1
2
3
4
5
Enter the search element: 5
Element found at index 4

3.SELECTION SORT
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in an array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
System.out.println("Sorted Array elements:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
scanner.close();
}
}

Output:
Enter the number of elements in an array: 3
Enter the array elements:
12
15
30
Sorted Array elements:
12 15 30
4.INSERTION SORT
import java.util.Scanner;
public class InsertionSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in an array: ");
int n = scanner.nextInt();

int[] arr = new int[n];


System.out.println("Enter the array elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 1; i < n; i++) {
int currentElement = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > currentElement) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = currentElement;
}
System.out.println("Sorted Array elements:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
scanner.close();
}
}

Output:
Enter the number of elements in an array: 4
Enter the array elements:
12
15
16
18
Sorted Array elements:
12 15 16 18

You might also like