0% found this document useful (0 votes)
64 views12 pages

ADS - Practical Material 2024

The document contains 10 programming problems related to algorithms and data structures. Each problem provides a code snippet for a Java program to implement a different concept such as arrays, vectors, linked lists, stacks, queues, linear search, binary search, bubble sort, selection sort, and insertion sort. The programs take input from the user, perform the relevant operations, and display output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views12 pages

ADS - Practical Material 2024

The document contains 10 programming problems related to algorithms and data structures. Each problem provides a code snippet for a Java program to implement a different concept such as arrays, vectors, linked lists, stacks, queues, linear search, binary search, bubble sort, selection sort, and insertion sort. The programs take input from the user, perform the relevant operations, and display output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Dr.

Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Lab Manual

1332 CIS - 3
Algorithm and Data Structures
[PRACTICAL ACTIVITY BOOK]

Prepared by

Dr. Vasanthi Muniasamy

Department of Computer Science,


Program: Web and Mobile Application
Academic year-2023-2024
1
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Index Page

S. No Marks Given Date Submission Date Signature

1 3

2 3

3 3

4 3

5 3

6 3

7 3

8 3

9 3

10 3

2
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

1. Write a java program that reads five integers and displays them

import javax.swing.*;
class array
{
public static void main(String args[])
{
int i;
int[] arr;
arr = new int[5];
for(i=0;i<5;i++)
arr[i]=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Array Elements: "));

System.out.println("The Array Elements are: " );


for(i=0;i<5;i++)
System.out.print("\t "+ arr[i]);

}
}

Output:

3
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

2. Write a java program to implement the vector concepts.

import java.util.*;
public class vectors
{
public static void main(String args[]) {

Vector vec = new Vector(2);


vec.addElement("A");
vec.addElement("B");
vec.addElement("C");
vec.addElement("D");

System.out.println("Size is: "+vec.size());


System.out.println("Capacity: "+vec.capacity());
vec.addElement("E");
vec.addElement("F");
vec.addElement("G");

System.out.println("Size: "+vec.size());
System.out.println("Capacity: "+vec.capacity());

System.out.println("First Element: "+vec.firstElement());


System.out.println("Capacity after increment is: "+vec.lastElement());

Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}

Output:

4
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

3. Write a java program to implement the linked list using built-in java classes.

import java.util.*;
class list
{
public static void main(String[] args){
LinkedList link=new LinkedList();

link.add("a");
link.add("b");
link.add(10);

System.out.println("The contents of array is" + link);


System.out.println("size of an linkedlist is" + link.size());

link.addFirst(20);
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());

link.addLast("c");
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());

link.add(2,"j");
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());

link.add(1,"t");
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());

link.remove(3);
System.out.println("The contents of array is" + link);
System.out.println("size of an linkedlist is" + link.size());
}
}

Output:

5
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

4. Write a java program to implement of Stack using java.util.Stack;

import java.util.*;
class stacks
{
public static void main(String args[])
{
Stack st=new Stack();
st.push("A");
st.push("B");
st.push("C");
st.push("D");
st.push("E");
st.push("F");
st.push("G");
st.push("H");
st.push("I");
st.push("J");
System.out.println(st);
System.out.println("st.size()=" +st.size());
System.out.println("st.peek()=" +st.peek());
System.out.println("st.search(B)="+st.search("B"));
System.out.println("st.pop()=" +st.pop());
System.out.println("st.pop()=" +st.pop());
System.out.println(st);
System.out.println("st.size()=" +st.size());
System.out.println("st.peek()=" +st.peek());
System.out.println("st.search(B)="+st.search("B"));
}
}

Output:

6
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

5. Write a java program to implement of Queue.

;*.import java.util

public class QueueExample

{public static void main(String args[])

;)(><Queue<Integer> qu = new LinkedList

;System.out.println("Is Queue empty ?: "+qu.isEmpty())

;System.out.println("Adding elements to the Queue")

;qu.add(5)

;qu.add(6)

;qu.add(8)

;System.out.println("The Queue size is : "+qu.size())

;System.out.println("The Queue elements are : "+qu)

;System.out.println("The first element is : "+qu.peek())

;System.out.println("poll elements from the Queue")

;)(qu.poll

;)(qu.poll

;)(qu.poll

;System.out.println("The Queue size is : "+qu.size())

;System.out.println("The Queue elements are : "+qu)

Output:

7
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

6. Write a java program to implement the concept of Linear Search.

import java.util.*;
public class LinearSearch {
public int find(final int[] data, final int key) {
for (int i = 0; i < data.length; ++i) {
if (data[i] > key)
return -1;
else if (data[i] == key)
return i;
}
return -1;
}
public static void main(String[] args)
{
final int arr[] = new int[10];
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr.length; i++)
{
arr[i] = input.nextInt();
}
LinearSearch search = new LinearSearch();
System.out.print("Enter the element to search: ");
int num=input.nextInt();
int n = search.find(arr, num);
if ((n >= 0) && (n < arr.length))
{
System.out.println("Found at index: " + n);
}
else
{
System.out.println("Not Found");
}}}
Output:

8
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

7. Write a java program to implement the concept of Binary Search.

public class BinarySearch


{
static int binarySearch(int[] arr, int key, int first, int last)
{
if(first<=last)
{
;int mid = (first+last)/2
if(key == arr[mid])
{
;return mid
}

if(key < arr[mid])


{
;return binarySearch(arr, key,first, mid-1)
}
if(key > arr[mid])
{
;return binarySearch(arr, key, mid+1, last)
}
}
;return -1
}

public static void main(String[] args)


{
;int[] arr = {1,2,3,4,5,6,7,8,9,10}
;int key = 9
;System.out.println("the key index is = "+binarySearch(arr, key, 0, arr.length-1))
}
}

Output:

9
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

8. Write a java program to implement the concept of Bubble Sort.

public class bubbleSort


{
public static void main(String a[])
{
int i;
int array[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
bubble_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
System.out.println("PAUSE");
}

public static void bubble_srt( int a[], int n )


{
int i, j,t=0;
for(i = 0; i < n; i++)
{
for(j = 1; j < (n-i); j++)
{
if(a[j-1] > a[j])
{
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}}}}}

Output:

10
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

9. Write a java program to implement the concept of Selection Sort.

public class selectionSort{


public static void main(String a[])
{
int i;
int array[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
selection_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
}
public static void selection_srt(int array[], int n){
for(int x=0; x<n; x++){
int index_of_min = x;
for(int y=x; y<n; y++){
if(array[index_of_min]>array[y]){
index_of_min = y;
}
}
int temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
}
}

Output:

11
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

10. Write a java program to implement the concept of Insertion Sort.

public class Insertion{


public static void main(String a[])
{
int i;
int array[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
InsertionSort(array);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
}
public static void InsertionSort( int [ ] num){
int j;
int key;
int i;

for (j = 1; j < num.length; j++)


{
key = num[ j ];
for(i=j-1;(i>=0)&&(num[ i ] < key); i--)
{
num[ i+1 ] = num[ i ];
}
num[ i+1 ] = key;
}
}
}
Output:

12

You might also like