Mid Sem Lab Evaluation
Java concepts, loose and tight coupling, sorting, searching, objects, stack and collection and use of them. spring boot and how to create an GET API calls .
Example of tight coupling :
Example of loose coupling :
package com.hitarth.IIITL2024;
import java.util.Arrays;
// Bubble Sort
public class temp {
/* ------------------------------------------------------ */
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/* ------------------------------------------------------ */
static void bubbleSort(int[] arr) {
int n = arr.length;
int flag = 0;
for (int i = 0; i < arr.length - 1; i++) {
flag = 0;
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j + 1] < arr[j]) {
swap(arr, j, j + 1);
flag = 1;
}
}
if (flag == 0)
break;
}
}
/* ------------------------------------------------------ */
public static void main(String[] args) {
int[] arr = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}
package com.hitarth.IIITL2024;
import java.util.Arrays;
// Insertion Sort
public class temp {
/* ------------------------------------------------------ */
static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < arr.length; i++) {
int j = i-1;
int x = arr[i];
while(j>=0 && x < arr[j])
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = x;
}
}
/* ------------------------------------------------------ */
public static void main(String[] args) {
int[] arr = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3};
insertionSort(arr);
System.out.println(Arrays.toString(arr));
}
}
package com.hitarth.IIITL2024;
import java.util.Arrays;
// Selection Sort
public class temp {
/* ------------------------------------------------------ */
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/* ------------------------------------------------------ */
static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int k = i;
for (int j = i; j < n; j++) {
if(arr[j]<arr[k])
k=j;
}
swap(arr, i, k);
}
}
/* ------------------------------------------------------ */
public static void main(String[] args) {
int[] arr = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3};
selectionSort(arr);
System.out.println(Arrays.toString(arr));
}
}
package com.hitarth.IIITL2024;
import java.util.Arrays;
// Merge Sort
public class temp {
/* ------------------------------------------------------ */
static void merge(int[] arr, int l, int mid, int h) {
int[] b = new int[100];
int i = l, j = mid + 1, k = l;
while (i <= mid && j <= h) {
if (arr[i] < arr[j])
b[k++] = arr[i++];
else
b[k++] = arr[j++];
}
for (; i <= mid; i++)
b[k++] = arr[i];
for (; j <= h; j++)
b[k++] = arr[j];
for (i = l; i <= h; i++)
arr[i] = b[i];
}
/* ------------------------------------------------------ */
static void mergeSort(int[] arr, int l, int h) {
if (l < h) {
int mid = (l + h) / 2;
mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, h);
merge(arr, l, mid, h);
}
}
/* ------------------------------------------------------ */
public static void main(String[] args) {
int[] arr = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3};
mergeSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}
import java.util.Arrays;
public class HeapSort {
public static void heapSort(int[] arr) {
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// Call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
// To heapify a subtree rooted with node i which is an index in arr[]. n is size of heap
static void heapify(int[] arr, int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left = 2*i + 1
int right = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
// If right child is larger than largest so far
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
// If largest is not root
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
heapSort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
package com.hitarth.IIITL2024;
// Binary Search
public class temp {
/* ------------------------------------------------------ */
static int binarySearch(int[] arr, int x) {
int l = 0;
int h = arr.length - 1;
while (l <= h) {
int mid = (l + h) / 2;
if (arr[mid] == x)
return mid;
else if (x < arr[mid])
h = mid;
else
l = mid + 1;
}
return -1;
}
/* ------------------------------------------------------ */
public static void main(String[] args) {
// int[] arr = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3};
int[] arr = {3, 5, 7, 9, 10, 11, 12, 13, 16, 24};
int mid = arr.length / 2;
int index = binarySearch(arr, 7);
System.out.println(index);
}
}