LS, BS, SS, Is
LS, BS, SS, Is
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int i, n, search, 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;
}
}
//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;
}
}