[go: up one dir, main page]

0% found this document useful (0 votes)
4 views44 pages

OOPS Record 02

The document contains Java implementations for various algorithms including Linear Search, Binary Search, Insertion Sort, Selection Sort, and Stack operations. Each section provides code examples, execution results, and confirms successful implementation. The document demonstrates fundamental data structures and algorithms in Java programming.

Uploaded by

velaabd007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views44 pages

OOPS Record 02

The document contains Java implementations for various algorithms including Linear Search, Binary Search, Insertion Sort, Selection Sort, and Stack operations. Each section provides code examples, execution results, and confirms successful implementation. The document demonstrates fundamental data structures and algorithms in Java programming.

Uploaded by

velaabd007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

//LinearSearch in java

class LinearSearch

public static int linearSearch(int array[], int x) {

int n = array.length;

// Going through array sequencially

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

if (array[i] == x)

return i;

return -1;

public static void main(String args[]) {

int array[] = { 2, 4, 0, 1, 9 };

int x = 1;

int result = linearSearch(array, x);

if (result == -1)

System.out.print("Element not found");

else

System.out.print("Element found at index: " + result);

}
Output:

Element found at index: 3

RESULT:

Thus, the Java application to perform sequential search was implemented and
executed.
// Binary Search in Java

class BinarySearch

int binarySearch(int array[], int x, int low, int high)

while (low <= high) {

int mid = low + (high – low) / 2;

if (array[mid] == x)

return mid;

if (array[mid] < x)

low = mid + 1;

else

high = mid – 1;

return -1;

public static void main(String args[])

BinarySearch ob = new BinarySearch();

int array[] = { 3, 4, 5, 6, 7, 8, 9 };

int n = array.length;
int x = 4;

int result = ob.binarySearch(array, x, 0, n – 1);

if (result == -1)

System.out.println(“Not found”);

else

System.out.println(“Element found at index “ + result);

Output :

Element Found at Index : 1

RESULT:

Thus, the Java application to perform binary search was implemented and
executed successfully.
// Insertion sort in Java

class InsertionSort

void insertionSort(int array[])

int size = array.length;

for (int step = 1; step < size;step++)

int key = array[step];

int j = step – 1;

while (j >= 0 && key < array[j])

Array[j + 1] = array[j];

--j;

Array[j + 1] = key;

public static void main(String args[])

{
int[] data = { 9, 5, 1, 4, 3 };

InsertionSort is = new InsertionSort();

is.insertionSort(data);

System.out.println(“Sorted Array in Ascending Order: “);

System.out.println(Arrays.toString(data));

Output :

//Insertion Sort

Sorted Array in Ascending Order : [1, 3, 4, 5, 9]

RESULT:

Thus, the Java application to sort an array of N elements using insertion sort

was implemented and executed successfully.


// Selection sort in Java

import java.util.Arrays;

class SelectionSort {

void selectionSort(int array[]) {

int size = array.length;

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

int min_index = step;

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

if (array[i] < array[min_index]) {

min_index = i;

int temp = array[step];

array[step] = array[min_index];
array[min_index] = temp;

public static void main(String args[]) {

int[] data = { 20, 12, 10, 15, 2 };

SelectionSort sort = new SelectionSort();

sort.selectionSort(data);

System.out.println(“Sorted Array in Ascending Order:");

System.out.println(Arrays.toString(data));

}
Output:

Sorted Array in Ascending Order:

[2, 10, 12, 15, 20]

RESULT:

Thus, the Java application to sort an array of N elements using selection sort

was implemented and executed successfully.


STACK AND QUEUE OPERATIONS

Program:

// Stack implementation in Java

class Stack {

private int arr[];

private int top;

private int capacity;

// Creating a stack

Stack(intsize)

arr = new int[size];

capacity = size;

top = -1;

// Add elements into stack

public void push(int x) {

if (isFull()) {

System.out.println("OverFlow \n Program Terminated\n");

System.exit(1);
}

System.out.println("Inserting " + x);

arr[++top] = x;

// Remove element from stack

public int pop() {

if (isEmpty()) {

System.out.println("STACK EMPTY");

System.exit(1);

return arr[top--];

// to return the size of the stack

public int size() {

return top + 1;

// Check if the stack is empty

public Boolean isEmpty(){

return top == -1;

// Check if the stack is full

public Boolean isFull() {


return top == capacity - 1;

public void printStack() {

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

System.out.println(arr[i]);

public static void main(String[] args)

Stack stack = new Stack(5);

stack.push(1);

stack.push(2);

stack.push(3);

stack.push(4);

stack.pop();

System.out.println("\nAfter popping out");

stack.printStack();

}
OUTPUT:

Inserting 1

Inserting 2

Inserting 3

Inserting 4

After popping out

3
1
2
3
4
5
6

You might also like