100% found this document useful (3 votes)
8K views

CS3381 Oop Lab Manual

The document describes implementations of common data structures - stack and queue - using Java collections. For stacks, it shows how to use a Stack to perform push, pop and peek operations, and check for empty stacks. For queues, it demonstrates using a LinkedList as a queue to add, remove and peek elements, and check the size and empty status.

Uploaded by

SARANYA A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
8K views

CS3381 Oop Lab Manual

The document describes implementations of common data structures - stack and queue - using Java collections. For stacks, it shows how to use a Stack to perform push, pop and peek operations, and check for empty stacks. For queues, it demonstrates using a LinkedList as a queue to add, remove and peek elements, and check the size and empty status.

Uploaded by

SARANYA A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.NO.

1a) Linear Search

AIM

To implement Linear search using Java.

Algorithm:

o Step 1: Traverse the array


o Step 2: Match the key element with array element
o Step 3: If key element is found, return the index position of the array element
o Step 4: If key element is not found, return -1

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

To implement Binary search using Java.

Algorithm:

1. Calculate the mid element of the collection.


2. Compare the key item with the mid element.
3. If key = middle element, then we return the mid index position for the key found.
4. Else If key > mid element, then the key lies in the right half of the collection. Thus
repeat steps 1 to 3 on the lower (right) half of the collection.
5. Else key < mid element, then the key is in the upper half of the collection. Hence you
need to repeat the binary search in the upper half.

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:

Element is found at index: 2


Ex.NO.1c)

Selection sort

AIM

To implement Selection sort using Java.

Algorithm:

1.Set MIN to location 0


2.Search the minimum element in the list
3.Swap with value at location MIN
4.Increment MIN to point to next element
5.Repeat until the list is sorted
Program:

public class SelectionSort {


 public static void main(String args[]){
      int array[] = {10, 20, 25, 63, 96, 57};
      int size = array.length;

      for (int i = 0 ;i< size-1; i++){


         int min = i;

         for (int j = i+1; j<size; j++){


            if (array[j] < array[min]){
            min = j;
      }
         }
         int temp = array[min];
         array[min] = array[i];
         array[i] = temp;
   }

      for (int i = 0 ;i< size; i++){


         System.out.print(" "+array[i]);
   }
   }  
}
Output:

10, 20, 25, 57, 63, 96


Ex.NO.1d)

Insertion sort

AIM

To implement Insertion sort using Java.

Algorithm

Step 1: Repeat Steps 2 to 5 for K = 1 to N-1


Step 2: set temp = A[K]
Step 3: set J = K – 1
Step 4:
Repeat while temp <=A[J]
set A[J + 1] = A[J]
set J = J – 1
[end of inner loop]
Step 5:
set A[J + 1] = temp
[end of loop]
Step 6: exit

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

To implement Stack using Java Collections.

Algorithm

1. Start the program


2. Create object for Stack
3. Perform push operation
4. Display the top element in the stack
5. Perform pop operation and display the count of elements in the stack
6. Stop the Program

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

To implement Queue using Java.

Algorithm

1. Start the program


2. Create object for Queue
3. Insert elements into the queue and display the front element in the queue
4. Remove elements from the queue
5. Display the size of the queue
6. Stop the Program
Program

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

You might also like