You are on page 1of 10

Practical 1:

Aim: Program to implement Linear search.


Linear search is a very simple search algorithm. In this type of search, a sequential search is
made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.

Program:
public class ani {
public static int aniruddha(int number[],int key){
for(int i=0;i<number.length;i++){
if(number[i]==key){
return i;
}
}
return-1;
}
public static void main(String[] args) {
int number[]={2,8,78,36,48,98};
int key= 48;
System.out.println("The number is at given index position:"+aniruddha(number, key));
}
}

Output:
Practical 2:
Aim: Program to implement Binary search.
Binary search is the search technique that works efficiently on sorted lists.Binary search
follows the divide and conquer approach in which the list is divided into two halves, and the
item is compared with the middle element of the list. If the match is found then, the location of
the middle element is returned. Otherwise, we search into either of the halves depending upon
the result produced through the match.

Program:
public class ani {
public static int aniruddha(int numbers[],int key){
int start=0,end= numbers.length-1;
while(start<=end){
int mid= (start+end)/2;
if(numbers[mid]==key){
return mid;
}
if(numbers[mid]<key){
start=mid+1;
}
else{
end= mid-1;
}
}
return -1;
}
public static void main(String args[]){
int numbers[]={0,4,7,9,45,10};
int key=45;
System.out.println("The index of the searched element is:"+aniruddha(numbers, key));
}
}

Output:
Practical 3:
Aim: Program to implement Insertion sort.
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards
in your hands. The array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted part.
To sort an array of size N in ascending order iterate over the array and compare the current
element (key) to its predecessor, if the key element is smaller than its predecessor, compare it
to the elements before. Move the greater elements one position up to make space for the
swapped element.

Program:
public class ani {
public static void aniruddha(int arr[]){
for(int i=1;i<arr.length;i++){
int curr= arr[i];
int prev= i-1;
while(prev>=0 && arr[prev]>curr){
arr[prev+1]= arr[prev];
prev--;
}
arr[prev+1]= arr[curr];
}
}
public static void joshi(int arr[]){
for(int i=1;i<arr.length+1;i++){
System.out.print(i+"");
}
System.out.println();
}
public static void main(String args[]){
int arr[]={5,4,1,3,2};
aniruddha(arr);
joshi(arr);
}
}

Output:
Practical 4:
Aim: Program to implement Bubble sort.
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm
in which each pair of adjacent elements is compared and the elements are swapped if they are
not in order. This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2) where n is the number of items.

Algorithm:
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort

Program:
public class ani {
public static void aniruddha(int number[]){
for(int i=0;i<number.length-1;i++){
for(int j=0;j<number.length-1-i;j++){
if(number[j]>number[j+1]){
int temp= number[j];
number[j]= number[j+1];
number[j+1]= temp;
}
}
}
}
public static void joshi(int number[]){
for(int i=0;i<number.length;i++){
System.out.print(number[i]);
}
}
public static void main(String[] args) {
int number[]={5,1,3,2,4};
aniruddha(number);
joshi(number);
}
}

Output:

Time complexity:
Practical 5:
Aim: Program to implement Selection sort.
The selection sort algorithm is a simple, yet effective sorting algorithm. A selection-based
sorting algorithm is described as an in-place comparison-based algorithm that divides the list
into two parts, the sorted part on the left and the unsorted part on the right. Initially, the sorted
section is empty, and the unsorted section contains the entire list. When sorting a small list,
selection sort can be used.

Algorithm:
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort

Program:
public class ani {
public static void aniruddha(int number[]){
for(int i=0;i<number.length-1;i++){
int min=i;
for(int j=i+1;j<number.length;j++){
if(number[min]>number[j]){
min=j;
}
}
int temp= number[min];
number[min]= number[i];
number[i]=temp;
}
}
public static void joshi(int number[]){
for(int i=0;i<number.length;i++){
System.out.print(number[i]);
}
}
public static void main(String[] args) {
int number[]={1,3,4,5,2};
aniruddha(number);
joshi(number);
}
}

Output:

Time complexity:

You might also like