[Link].
A SEQUENTIALSEARCH
DATE:
Aim
To write a Java program to solve problems in array by using
sequential search
algorithm.
Algorithm:
1. Start the program
2. Create Sequential Search class with one dimensionalarray.
3. Read the search element from the user.
4. Compare the search element with the first element in the list
5. If both are matched,then display"Given element is found!!!"and
terminate the function
6. If both are not matched, then compare search element with the next
element in the list.
7. Repeat steps3 and 4 until search element is compared with last element
in the list.
8. If last element in the list also doesn't match, then display
"Element is not found!!!"and terminate the function.
9. Stop the program.
Program:
public class Main {
public static void main(String[] args) {
int[] one = {2, 9, 6, 7, 4, 5, 3, 0, 1};
int target = 4;
sequentialSearch(one, target);
}
public static void sequentialSearch(int[] a, int b) {
int index = -1;
for (int i = 0; i < [Link]; i++) {
if (a[i] == b) {
index = i;
break;
}
}
if (index == -1) {
[Link]("Your target does not exist in the array");
} else {
[Link]("Your target is at index " + index + " of the array");
7
}
}
}
Output:
Your target integer is in index 4 of the array
7
[Link].B BINARYSEARCH
DATE:
Aim
To write a Java program to solve problems in array by using binary search algorithm.
Algorithm
1. Start the program
2. Create Binary Search class with one dimensional array.
3. Read the search element from the user.
4. Find the middle element in the sorted list.
5. Compare the search element with the middle element in the sorted list.
6. If both are matched, then display" Given element is found!!!"and terminate the function.
7. If both are not matched, then check whether the search element is smaller or larger than the
middle element.
8. If the search element is smaller than middle element, repeat steps 2,3,4 and5 for the left sub
list of the middle element.
9. If the search element is larger than middle element, repeat steps 2, 3, 4 and5 for the right
sub list of the middle element.
10. Repeat the same process until we find the search element in the list or until sub list contains
only one element.
11. If that element also doesn't match with the search element, then display" Element is not
found in the list!!!" and terminate the function.
12. Stop the program.
Program:
class BinarySearch {
public static int binarySearch(int arr[], int first, int last, int key) {
if (last >= first) {
int mid = first + (last - first) / 2;
if (arr[mid] == key) {
return mid;
}
if (arr[mid] > key) {
// search in left subarray
return binarySearch(arr, first, mid - 1, key);
} else {
// search in right subarray
7
return binarySearch(arr, mid + 1, last, key);
}
}
return -1; // key not found
}
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
int key = 30;
int last = [Link] - 1;
int result = binarySearch(arr, 0, last, key);
if (result == -1)
[Link]("Element is not found!");
else
[Link]("Element is found at index: " + result);
}
}
Output:
Element is found at index: 2
7
[Link].C. SELECTIONSORT
DATE:
Aim:
To write a Java program to solve problems in array by using selection sort
algorithm.
Algorithm:
1. Start the program.
2. Create a SelectionSort class with one dimensional array.
3. Select the first element of the list(i.e.,Element atfirstpositioninthelist).
4. Compare the selectedelement withall the other eleents in the list.
5. In every comparision, if any element is found smaller than the select ed
element (for Ascending order), then both are swapped.
6. Repeat the same procedure with elemnt in then ext position in the list
till the entire list is sorted.
7. Stopthe program
Program:
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};
// Selection Sort
for (int i = 0; i < [Link] - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < [Link]; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
// Print sorted array
[Link]("Sorted array:");
7
for (int num : arr) {
[Link](num + " ");
}
}
}
OUTPUT:
Sorted array:
12358
Result:
Thus the program for solving problems in array using selectionsort
algorithm has been Executed and the output was verified.
7
[Link].D INSERTIONSORT
DATE:
Aim
TowriteaJavaprogramtosolveproblemsinarraybyusinginsertionsortalgorit
hm.
Algorithm
1. Starttheprogram.
2. CreateaInsertionSortclasswithonedimensional array.
3. Assumethatfirstelementinthelistisinsortedportionandalltheremainingel
ementsarein unsorted portion.
4. Takefirstelementfromtheunsortedportionandinsertthatelementintotheso
rtedportionin the order specified.
5. Repeattheaboveprocessuntilalltheelementsfromtheunsortedportionare
movedintothe sorted portion.
6. Stopthe program.
Program:
publicclassInsertionSort{
publicstaticvoidinserti
onSort(intarray[]){
int n=[Link];
f
o
r
(
i
n
t
j
=
1
;
j
<
n
;
j
+
+
7
)
{
i
n
t
k
e
y
=
a
r
r
a
y
[
j
]
;
i
n
t
i
=
j
-
1
;
while((i>-
1)&&(array[i]>key)){
array[i+1]=array[i];
i--;
}
array[i+1]=key;
}
}
publicstaticvoidmain(Stri
nga[]){
int[]arr1={9,14,3,2,43,1
1,58,22};
[Link]("Bef
7
oreInsertionSort");
for(int i:arr1){
[Link](i+"");
}
[Link],println();
insertionSort(arr1);//
sortingarrayusinginsertionsort
[Link]("After Insertion
Sort");
for(inti:arr1){
7
[Link](i+"");
}
}
}
Output:
BeforeInsertionSort 9
14 3 2 43 11 58 22
After InsertionSort
2 3911 14 224358
Result:
Thustheprogramforsolvingproblemsinarrayusinginsertionsortalgorithmhas been
executedandtheoutputwasverified.
8
6
[Link].A STACK
DATE:
Aim
To write a Java program to develop a stack data structures using
classes and objects.
Algorithm:
1. Start the program.
2. Create a stack class and define stack size.
3. Declare all the functions used in stack implementation.
4. Create a one dimensional array with fixed size
5. Define a integer variable 'top'andinitialize with '-1'.
6. In main method, display menu with list of operations and make suitable
function calls to perform operation selected by the user on the stack.
7. Check whether stack is FULL. If it is FULL, then display " Stack is
FULL!" and terminate the function. If it is not full, then increment top value
by oneand set stack[top] to value.
8. Check whether stack is EMPTY. If it is EMPTY, then display "Stack is
EMPTY!!!" and terminate the function. If it is not empty, then define a
variable 'i' and initialize with top. Display stack[i] value and decrement i value
by one (i--).
9. Stop the program
PROGRAM
public class DemoQueue {
private int maxSize;
private int[] queueArray;
private int front;
private int rear;
private int currentSize;
public DemoQueue(int size) {
[Link] = size; 1
[Link] = new int[size];
[Link] = 0;
[Link] = -1;
[Link] = 0;
}
public void insert(int item) {
if (isQueueFull()) {
[Link]("Queue is full!");
return;
}
if (rear == maxSize - 1) {
rear = -1;
}
queueArray[++rear] = item;
currentSize++;
[Link]("Item added to queue: " + item);
}
public int delete() {
if (isQueueEmpty()) {
throw new RuntimeException("Queue is empty");
}
int temp = queueArray[front++];
if (front == maxSize) {
front = 0;
}
currentSize--;
return temp;
}
ALGORITHM :
1
PROGRAM:
public int peek() {
if (isQueueEmpty()) {
throw new RuntimeException("Queue is empty");
}
return queueArray[front];
}
public boolean isQueueFull() {
return (maxSize == currentSize);
}
public boolean isQueueEmpty() {
return (currentSize == 0);
}
public static void main(String[] args) {
DemoQueue queue = new DemoQueue(10);
[Link](2);
[Link](3);
[Link]("Item deleted from queue: " + [Link]());
[Link]("Item deleted from queue: " + [Link]());
[Link](5);
[Link]("Item deleted from queue: " + [Link]());
}
}