BCA Final Lab Records

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Ex.

No: 1 Implementation of Linear Search


Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

class LinearSearchAlgorithm

public void nSearch(int[] arr,int searchValue)

boolean presence=false;

int pos=0;

for(int n=0;n<arr.length;n++)

if (arr[n]==searchValue)

presence=true;

pos=n+1;

break;

if (presence)

System.out.println("Search value " + searchValue + " exists at " + pos +" position");
else

System.out.println("Search value " + searchValue + " does not exists");

public class LinearSearch

{public static void main(String[] args)

System.out.println("Enter the size of the array:");

Scanner sc=new Scanner(System.in);

int arrSize=sc.nextInt();

int[] arr=new int[arrSize];

System.out.println("Enter elements of the array:");

for(int n=0;n<arrSize;n++)

arr[n]=sc.nextInt();

System.out.println("Enter the element to search:");

int searchValue=sc.nextInt();

LinearSearchAlgorithm Lsa=new LinearSearchAlgorithm();

Lsa.nSearch(arr,searchValue);

}}
Output:

D:\Chezhian Mgr\0.2022-2023-0dd\BCA-AI\Lab Programs>java LinearSearch

Enter the size of the array:

Enter elements of the array:

43

23

65

78

34

23

55

Enter the element to search:

65

Search value 65 exists at 3 position

Result:
Ex. No: 2 Implementation of Binary Search
Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

class BinarySearchAlgorithm

public void bSearch(int[] arr, int searchVal)

int first=0;

int last=arr.length-1;

int midPoint = (first + last)/2;

while( first <= last )

if ( arr[midPoint] < searchVal )

first = midPoint + 1;

else if ( arr[midPoint] == searchVal )

System.out.println("Search Value "+searchVal+" is found at index " + midPoint);

break;

else

last = midPoint - 1;

midPoint = (first + last)/2;

}
if ( first > last )

System.out.println("Search Value is not found!");

public class BinarySearch

public static void main(String[] args)

System.out.println("Enter the size of the array:");

Scanner sc=new Scanner(System.in);

int arrSize=sc.nextInt();

int[] arr=new int[arrSize];

System.out.println("Enter elements of the array:");

for(int n=0;n<arrSize;n++)

arr[n]=sc.nextInt();

System.out.println("Enter the element to search:");

int searchValue=sc.nextInt();

BinarySearchAlgorithm bsa=new BinarySearchAlgorithm();

bsa.bSearch(arr,searchValue);

}
Output:

D:Chezhian Mgr 0.2022-2023-Odd BCA-AI Lab Programs> Java BinarySearch

Enter the size of the array:

10

Enter elements of the array:

12

23

45

67

77

79

82

89

91

93

Enter the element to search:

67

Search Value 67 is found at index 3

Result:
Ex. No: 3 Implementation of Selection sorting method
Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

public class SelectionSort {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of elements to sort:");

int arrSize = sc.nextInt();

int[] arr = new int[arrSize];

System.out.println("Enter the element values:");

for (int n = 0; n < arrSize; n++) arr[n] = sc.nextInt();

for (int i = 0; i < arrSize; i++)

for (int j = i + 1; j < arrSize; j++)

if (arr[i] > arr[j]) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

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

for (int value : arr) System.out.println(value);

}
Output:

D:\Chezhian Mgr 0.2022-2023-Odd BCA-AI Lab Programs > Java SelectionSort

Enter the number of elements to sort:

Enter the element value:

45

23

32

The Sorted Elements as follows

23

32

45

Result:
Ex. No: 4 Implementation of Bubble sorting method
Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

public class BubbleSort {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of elements to sort:");

int arrSize = sc.nextInt();

int[] arr = new int[arrSize];

System.out.println("Enter the element values:");

for (int n = 0; n < arrSize; n++) arr[n] = sc.nextInt();

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

for (int j = 0; j < arrSize - i - 1; j++)

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

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

for (int value : arr) System.out.println(value);

}
Output:

D:\Chezhian Mgr 0.2022-2023-0dd\BCA-AI Lab Programs>Java BubbleSort

Enter the number of elements to sort:

Enter the element value:

65

45

34

88

74

The Sorted Elements as follows

34

45

65

74

88

Result:
Ex. No: 5 Implementation of Insertion sorting method
Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

public class InsertionSort {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of elements to sort:");

int arrSize = sc.nextInt();

int[] arr = new int[arrSize];

System.out.println("Enter the element values:");

for (int n = 0; n < arrSize; n++) arr[n] = sc.nextInt()

for (int j = 1; j < arrSize; j++) {

int key = arr[j], i = j - 1;

while (i >= 0 && arr[i] > key) {

arr[i + 1] = arr[i];

i--;

arr[i + 1] = key;

System.out.println("The Sorted Elements as follows:");

for (int value : arr) System.out.println(value);

}
Output:

D:\Chezhian Mgr/0.2022-2023-0dd\BCA-AI Lab Programs>java InsertionSort

Enter the number of elements to sort:

Enter the element value:

11

33

66

22

44

The Sorted Elements as follows

11

22

33

44

66

Result:
Ex. No: 6 Implementation of PUSH and POP operations of a STACK using ARRAYS

Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

class Stack {

private int[] arr; private int top = -1, size;

Stack(int size) { this.size = size; arr = new int[size]; }

void push(int x) { System.out.println(top == size - 1 ? "Overflow" : "Inserted: " + (arr[++top]


= x)); }

int pop() { return top == -1 ? -1 : arr[top--]; }

void display() { for (int i = top; i >= 0; i--) System.out.print(arr[i] + " "); System.out.println();
}

int getSize() { return top + 1; }

public class StackImplementation {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter stack size: ");

Stack stack = new Stack(sc.nextInt());

while (true) {

System.out.println("1.Display 2.Push 3.Pop 4.Count 5.Exit");

switch (sc.nextInt()) {

case 1 -> stack.display();

case 2 -> stack.push(sc.nextInt());


case 3 -> {

int popped = stack.pop();

System.out.println("Popped: " + (popped == -1 ? "Empty" : popped));

case 4 -> System.out.println("Size: " + stack.getSize());

case 5 -> System.exit(0);

default -> System.out.println("Invalid choice");

}
Output:

D: \Chezhian Mgr/0.2022-2023-0dd\BCA-AI Lab Programs>java StackImplementation

Enter stack size: 2

1. Display 2.Push 3.Pop 4.Count 5.Exit

10

Inserted: 10

1. Display 2.Push 3.Pop 4. Count 5.Exit

20

Inserted: 20

1.Display 2.Push 3.Pop 4.Count 5.Exit

20 10

1.Display 2.Push 3.Pop 4.Count 5.Exit

Popped: 20

1.Display 2.Push 3.Pop 4. Count 5.Exit

Size: 1

1.Display 2.Push 3.Pop 4.Count 5. Exit

Result:
Ex. No: 7 Implementation of INSERT and DELETE operations of a QUEUE
Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

class Queue {

int[] items;

int front = -1, back = -1, size;

public Queue(int size) {

this.size = size;

items = new int[size];

boolean isFull() { return back == size - 1; }

boolean isEmpty() { return front == -1; }

void enQueue(int item) {

if (isFull()) System.out.println("Queue is full");

else {

if (isEmpty()) front = 0;

items[++back] = item;

void deQueue() {
if (isEmpty()) System.out.println("Queue is empty");

else if (front == back) front = back = -1;

else front++;

void display() {

if (isEmpty()) System.out.println("Queue is empty");

else for (int i = front; i <= back; i++) System.out.println(items[i]);

int getSize() { return isEmpty() ? 0 : back - front + 1; }

void peak() {

if (!isEmpty()) System.out.println("Front value is: " + items[front]);

public class QueueImplementation {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the size of the Queue: ");

Queue queue = new Queue(sc.nextInt());

while (true) {

System.out.println("\n1.Display 2.EnQueue 3.DeQueue 4.Size 5.Peak 0.Exit");

switch (sc.nextInt()) {
case 1 -> queue.display();

case 2 -> queue.enQueue(sc.nextInt());

case 3 -> queue.deQueue();

case 4 -> System.out.println("Size: " + queue.getSize());

case 5 -> queue.peak();

case 0 -> System.exit(0);

default -> System.out.println("Invalid choice!");

}
D: \Chezhian Mgr/0.2022-2023-0dd\BCA-AI Lab Programs>java StackImplementation

Enter the size of the Queue: 3

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

10

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

20

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

30

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

10

20

30

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

Size: 3

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

5
Front value is: 10

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

1
20
30
1. Display 2. EnQueue 3. DeQueue 4. Size 5. Peak 0. Exit

Result:
Ex. No: 8 Implementation of Binary Tree Traversals
Date:

Aim:

Algorithm:
Program:

import java.util.*;

class BinaryTree {

class Node { int key; Node left, right; Node(int key) { this.key = key; } }

Node root;

void insert(int key) { root = insertRecursive(root, key); }

Node insertRecursive(Node root, int key) {

if (root == null) return new Node(key);

if (key < root.key) root.left = insertRecursive(root.left, key);

else if (key > root.key) root.right = insertRecursive(root.right, key);

return root;

void traverseInOrder(Node node) { if (node != null) { traverseInOrder(node.left);


System.out.print(node.key + " "); traverseInOrder(node.right); } }

void traversePreOrder(Node node) { if (node != null) { System.out.print(node.key + " ");


traversePreOrder(node.left); traversePreOrder(node.right); } }

void traversePostOrder(Node node) { if (node != null) { traversePostOrder(node.left);


traversePostOrder(node.right); System.out.print(node.key + " "); } }

void traverseLevelOrder() {
if (root == null) return;

LinkedList<Node> queue = new LinkedList<>();

queue.add(root);

while (!queue.isEmpty()) {

Node node = queue.poll();

System.out.print(node.key + " ");

if (node.left != null) queue.add(node.left);

if (node.right != null) queue.add(node.right);

public static void main(String[] args) {

BinaryTree bt = new BinaryTree();

bt.insert(50); bt.insert(30); bt.insert(70); bt.insert(20); bt.insert(40);

System.out.print("InOrder: "); bt.traverseInOrder(bt.root);

System.out.print("\nPreOrder: "); bt.traversePreOrder(bt.root);

System.out.print("\nPostOrder: "); bt.traversePostOrder(bt.root);

System.out.print("\nLevelOrder: "); bt.traverseLevelOrder();

}
Output:

D: \Chezhian Mgr/0.2022-2023-0dd\BCA-AI Lab Programs >java BinaryTree

InOrder: 20 30 40 50 70

PreOrder: 50 30 20 40 70

Postorder: 20 40 30 70 50

LevelOrder: 50 30 70 20 40

Result:
Ex. No: 9 Implementation of Binary Search Tree (BST)
Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

class BinarySearchTree {

class Node { int key; Node left, right; Node(int key) { this.key = key; } }

Node root;

void insert(int key) { root = insertRecursive(root, key); }

Node insertRecursive(Node root, int key) {

if (root == null) return new Node(key);

if (key < root.key) root.left = insertRecursive(root.left, key);

else if (key > root.key) root.right = insertRecursive(root.right, key);

return root;

void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.key + " ");


inorder(root.right); } }

boolean search(Node root, int key) {

if (root == null) return false;

if (root.key == key) return true;

return key < root.key ? search(root.left, key) : search(root.right, key);

}
public static void main(String[] args) {

BinarySearchTree bst = new BinarySearchTree();

Scanner sc = new Scanner(System.in);

while (true) {

System.out.print("1.Insert 2.Display 3.Search 0.Exit: ");

switch (sc.nextInt()) {

case 1 -> bst.insert(sc.nextInt());

case 2 -> { System.out.print("InOrder: "); bst.inorder(bst.root); System.out.println(); }

case 3 -> System.out.println(bst.search(bst.root, sc.nextInt()) ? "Found" : "Not


Found");

case 0 -> System.exit(0);

default -> System.out.println("Invalid Choice");

}
Output:

D: \Chezhian Mgr/0.2022-2023-0dd\BCA-AI Lab Programs >java BinarySearchTree

1. Insert 2. Display 3.Search 0. Exit: 1

10

1. Insert 2.Display 3. Search 0.Exit: 1

20

1. Insert 2. Display 3. Search 0.Exit: 1

30

1. Insert 2. Display 3. Search 0.Exit: 2

InOrder: 10 20 30

1. Insert 2.Display 3. Search 0. Exit: 3

20

Found

1. Insert 2. Display 3. Search 0.Exit: 0

Result:
Ex. No: 10 Implementation of INSERTING and DELETING nodes in Binary Tree.

Date:

Aim:

Algorithm:
Program:

import java.util.Scanner;

class BSTImplementation {

class Node { int key; Node left, right; Node(int k) { key = k; } }

Node root;

void insert(int k) { root = insertRec(root, k); }

Node insertRec(Node r, int k) { if (r == null) return new Node(k); if (k < r.key) r.left =
insertRec(r.left, k); else r.right = insertRec(r.right, k); return r; }

void delete(int k) { root = deleteRec(root, k); }

Node deleteRec(Node r, int k) {

if (r == null) return null;

if (k < r.key) r.left = deleteRec(r.left, k); else if (k > r.key) r.right = deleteRec(r.right, k);

else if (r.left == null) return r.right; else if (r.right == null) return r.left;

r.key = minValue(r.right); r.right = deleteRec(r.right, r.key); return r;

int minValue(Node r) { while (r.left != null) r = r.left; return r.key; }

void inorder(Node r) { if (r != null) { inorder(r.left); System.out.print(r.key + " ");


inorder(r.right); } }
public static void main(String[] args) {

BST bst = new BST(); Scanner sc = new Scanner(System.in);

while (true) {

System.out.println("1.Insert 2.Delete 3.Display 0.Exit:");

switch (sc.nextInt()) {

case 1 -> bst.insert(sc.nextInt());

case 2 -> bst.delete(sc.nextInt());

case 3 -> { bst.inorder(bst.root); System.out.println(); }

case 0 -> System.exit(0);

default -> System.out.println("Invalid choice.");

}
Output:

D: \Chezhian Mgr/0.2022-2023-0dd\BCA-AI Lab Programs >java BSTImplementation

1. Insert 2.Delete 3.Display 0.Exit:

10

1. Insert 2.Delete 3.Display 0.Exit:

20

1. Insert 2.Delete 3.Display 0.Exit:

10 20

1. Insert 2.Delete 3. Display 0.Exit:

10

1. Insert 2.Delete 3.Display 0.Exit:

20

1. Insert 2.Delete 3.Display 0.Exit:

Result:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy