CS3381 Oop Lab Manual
CS3381 Oop Lab Manual
AIM
Algorithm:
Program:
public class LinearSearchExample{
public static int linearSearch(int[] arr, int key){
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}
public static void main(String a[]){
int[] a1= {10,20,30,50,70,90};
int key = 50;
System.out.println(key+" is found at index: "+linearSearch(a1, key));
}
}
Output:
50 is found at index: 3
Ex.NO.1b)
Binary Search
AIM
Algorithm:
Program:
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[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 is not found!");
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key); } }
Output:
Selection sort
AIM
Algorithm:
Insertion sort
AIM
Algorithm
Program:
import java.util.*;
public class Main {
public static void main(String[] args) {
//declare an array and print the original contents
int[] numArray = {10,6,15,4,1,45};
System.out.println("Original Array:" + Arrays.toString(numArray));
//apply insertion sort algorithm on the array
for(int k=1; k<numArray.length-1; k++) {
int temp = numArray[k];
int j= k-1;
while(j>=0 && temp <= numArray[j]) {
numArray[j+1] = numArray[j];
j = j-1;
}
numArray[j+1] = temp;
}
//print the sorted array
System.out.println("Sorted Array:" + Arrays.toString(numArray));
}
}
Output:
Original Array:[10, 6, 15, 4, 1, 45]
Sorted Array:[1, 4, 6, 10, 15, 45]
Ex.No : 2a) Implementation of Stack
AIM
Algorithm
Program
import java.util.Stack;
class Main
{
public static void main(String[] args)
{
Stack<String> stack = new Stack<String>();
stack.push("A"); // Insert `A` into the stack
stack.push("B"); // Insert `B` into the stack
stack.push("C"); // Insert `C` into the stack
stack.push("D"); // Insert `D` into the stack
System.out.println("The top element is " + stack.peek());
stack.pop(); // removing the top element (`D`)
stack.pop(); // removing the next top (`C)
// returns the total number of elements present in the stack
System.out.println("The stack size is " + stack.size());
// check if the stack is empty
if (stack.empty()) {
System.out.println("The stack is empty");
}
else {
System.out.println("The stack is not empty");
}
}
}
Output:
The top element is D
The stack size is 2
The stack is not empty
Ex.No : 2b) Implementation of Queue
AIM
Algorithm
import java.util.LinkedList;
import java.util.Queue;
class Main
{
public static void main(String[] args)
{
Queue<String> queue = new LinkedList<String>();
queue.add("A"); // Insert `A` into the queue
queue.add("B"); // Insert `B` into the queue
queue.add("C"); // Insert `C` into the queue
queue.add("D"); // Insert `D` into the queue
// Prints the front of the queue (`A`)
System.out.println("The front element is " + queue.peek());
queue.remove(); // removing the front element (`A`)
queue.remove(); // removing the front element (`B`)
// Prints the front of the queue (`C`)
System.out.println("The front element is " + queue.peek());
// Returns the total number of elements present in the queue
System.out.println("The queue size is " + queue.size());
// check if the queue is empty
if (queue.isEmpty()) {
System.out.println("The queue is empty");
}
else {
System.out.println("The queue is not empty");
}
}
}
Output:
The front element is A
The front element is C
The queue size is 2
The queue is not empty