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

LS, BS, SS, Is

The document discusses linear search, binary search, selection sort, and insertion sort algorithms. It includes code examples to demonstrate each algorithm.
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)
21 views3 pages

LS, BS, SS, Is

The document discusses linear search, binary search, selection sort, and insertion sort algorithms. It includes code examples to demonstrate each algorithm.
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

//Sequential Seqrch

import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int i, n, search, array[];

Scanner input = new Scanner(System.in);


System.out.println("Enter the number of elements in the Array");
n = input.nextInt();
array = new int[n];

System.out.println("Enter the " + n + " Array Elements");

for (i = 0; i < n; i++) {


array[i] = input.nextInt();
}
System.out.println("Enter the search Element");
search = input.nextInt();

for (i = 0; i < n; i++)


{
if (array[i] == search) /* Searching element is present */
{
System.out.println("Element " +search + " is present at the location " + (i + 1) +
".");
break;
}
}
if (i== n) /* Search element not present */
System.out.println(search + " element isn't present in the array.");
}
}

//BS
class BinarySearch{
public static void BinSearch(int array[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( array[mid] < key ){
first = mid + 1;
}else if ( array[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element not found!");
}
}
public static void main(String args[]){
int array[] = {10,20,30,40,50};
int key = 30;
int last=array.length-1;
BinSearch(array,0,last,key);
}
}
//SS
public class SelectionSort {
public static void SelSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
int index = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[index]) {
index = j; // searching for the lowest index
}
}
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
}
}

public static void main(String[] args) {


int[] array1 = { 44, 88, 55, 11, 33 };
System.out.println("Before Selection Sort");
for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + " ");
}
System.out.println();

SelSort(array1); // sorting array using selection sort

System.out.println("After Selection Sort");


for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + " ");
}
}
}

//IS
public class InsertionSort {
public static void InSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
}
}

public static void main(String[] args) {


int[] array1 = { 8, 6, 1, 5, 3 };
System.out.println("Before Insertion Sort");
for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + " ");
}
System.out.println();
InSort(array1); // sorting array using insertion sort

System.out.println("After Insertion Sort");


for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + " ");
}
}
}

You might also like