[go: up one dir, main page]

0% found this document useful (0 votes)
13 views6 pages

Experiment 2

The document discusses 5 different algorithms for searching and sorting: sequential search, binary search, bubble sort, selection sort, and insertion sort. Code examples are provided to demonstrate how each algorithm works on sample input data. The output for each example shows the steps and results of applying the search and sorting algorithm to an array.

Uploaded by

Nesamanikandan S
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)
13 views6 pages

Experiment 2

The document discusses 5 different algorithms for searching and sorting: sequential search, binary search, bubble sort, selection sort, and insertion sort. Code examples are provided to demonstrate how each algorithm works on sample input data. The output for each example shows the steps and results of applying the search and sorting algorithm to an array.

Uploaded by

Nesamanikandan S
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/ 6

Experiment-2

Solve Problems using Searching and sorting

1. Sequential Search
Code:
import java.util.Scanner;
class Sequential
{
public static void main(String[]args)
{
int a,ser,x=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the Number of elements in
array:");
a=s.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++)
{
System.out.println("Value at index"+i+":");
arr[i]=s.nextInt();
}
System.out.println("Entre the search element:");
ser=s.nextInt();
for(int i=0;i<a;i++)
{
if(arr[i]==ser)
{
System.out.println("Element is found at
index"+i);
x=1;
}
}
if(x==0)
{
System.out.println("Element not Found");
}
}

}
Output:
Enter the Number of elements in array:
5
Value at index0:
1
Value at index1:
2
Value at index2:
3
Value at index3:
4
Value at index4:
5
Entre the search element:
3
Element is found at index 2

2. Binary Search
Code:
import java.util.*;
class Binary
{
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
int[] list = {2,5,6,7,8,};
System.out.println("Enter a Element to search : ");
int num =s.nextInt();
boolean found = false;
for (int i=0;i<list.length;i++)
{
if(list[i] == num);
{
found = true;
System.out.println("Element is found");
break;
}
}
if(!found)
{
System.out.println("Element is not found");
}
}
}

Output:
Enter a Element to search :
7
Element is found

3. Bubble Sorting
Code:
import java.util.Scanner;
class Bubble {
public static void main(String[] args){
int a,min;
Scanner s=new Scanner(System.in);
System.out.print("Enter the array limit:");
a=s.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++){
System.out.print("Value at index "+i+":");
arr[i]=s.nextInt();
}
for(int i=1;i<a;i++){
min=arr[i];
int j=i-1;
while(j>=0 && min<arr[j]){
arr[j+1]=arr[j];
j-=1;
}
arr[j+1]=min;

}
System.out.print("Array after sorting:");
for(int i=0;i<a;i++){
System.out.print(arr[i]+" ");
}
}
}
Output:
Enter the array limit:5
Value at index 0:7
Value at index 1:4
Value at index 2:1
Value at index 3:8
Value at index 4:5
Array after sorting:1 4 5 7 8

4. Selection Sorting
Code:
import java.util.Scanner;
class Selection {
public static void main(String[] args){
int a,temp,least;
Scanner s=new Scanner(System.in);
System.out.print("Enter the array limit:");
a=s.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++){
System.out.print("Value at index "+i+":");
arr[i]=s.nextInt();
}
for(int i=0;i<(a-1);i++){
least=i;
for(int j=(i+1);j<a;j++){
if(arr[least]>arr[j]){
least=j;
}
}
temp=arr[least];
arr[least]=arr[i];
arr[i]=temp;
}
System.out.print("Array after sorting:");
for(int i=0;i<a;i++){
System.out.print(arr[i]+" ");
}
}
}
Output:
Enter the array limit:7
Value at index 0:9
Value at index 1:1
Value at index 2:4
Value at index 3:0
Value at index 4:2
Value at index 5:5
Value at index 6:8
Array after sorting:0 1 2 4 5 8 9

5. Insertion Sorting
Code:
import java.util.Scanner;
class Insertion
{
public static void main(String[]args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of Elements:");
int num = s.nextInt();
int[]arr = new int[num];
for(int i=0;i<num;i++)
{
arr[i] = s.nextInt();
}
for(int i=1;i<num;i++)
{
int key = arr[i];
int j = i-1;
while(j>=0 && arr[j]>key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
System.out.println("Sorted Array :");
for(int i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
}

Output:
Enter the number of Elements:
5
7
3
9
1
2
Sorted Array :
1
2
3
7
9

You might also like