OOPS Record 02
OOPS Record 02
class LinearSearch
int n = array.length;
if (array[i] == x)
return i;
return -1;
int array[] = { 2, 4, 0, 1, 9 };
int x = 1;
if (result == -1)
else
}
Output:
RESULT:
Thus, the Java application to perform sequential search was implemented and
executed.
// Binary Search in Java
class BinarySearch
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid – 1;
return -1;
int array[] = { 3, 4, 5, 6, 7, 8, 9 };
int n = array.length;
int x = 4;
if (result == -1)
System.out.println(“Not found”);
else
Output :
RESULT:
Thus, the Java application to perform binary search was implemented and
executed successfully.
// Insertion sort in Java
class InsertionSort
int j = step – 1;
Array[j + 1] = array[j];
--j;
Array[j + 1] = key;
{
int[] data = { 9, 5, 1, 4, 3 };
is.insertionSort(data);
System.out.println(Arrays.toString(data));
Output :
//Insertion Sort
RESULT:
Thus, the Java application to sort an array of N elements using insertion sort
import java.util.Arrays;
class SelectionSort {
min_index = i;
array[step] = array[min_index];
array[min_index] = temp;
sort.selectionSort(data);
System.out.println(Arrays.toString(data));
}
Output:
RESULT:
Thus, the Java application to sort an array of N elements using selection sort
Program:
class Stack {
// Creating a stack
Stack(intsize)
capacity = size;
top = -1;
if (isFull()) {
System.exit(1);
}
arr[++top] = x;
if (isEmpty()) {
System.out.println("STACK EMPTY");
System.exit(1);
return arr[top--];
return top + 1;
System.out.println(arr[i]);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.pop();
stack.printStack();
}
OUTPUT:
Inserting 1
Inserting 2
Inserting 3
Inserting 4
3
1
2
3
4
5
6