diff --git a/src/main/java/algorithms/SearchingAlgorithms.java b/src/main/java/algorithms/SearchingAlgorithms.java new file mode 100644 index 0000000..481d05a --- /dev/null +++ b/src/main/java/algorithms/SearchingAlgorithms.java @@ -0,0 +1,36 @@ +package src.main.java.algorithms; + + + +public class SearchingAlgorithms { + + public static int linearSearch(int[] arr, int target) { + for (int i = 0; i < arr.length; i++) { + if (arr[i] == target) { + return i; // Return index of found element + } + } + return -1; // Element not found + } + + public static int binarySearch(int[] arr, int target) { + int left = 0; + int right = arr.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (arr[mid] == target) { + return mid; // Return index of found element + } + + if (arr[mid] < target) { + left = mid + 1; // Search in the right half + } else { + right = mid - 1; // Search in the left half + } + } + + return -1; // Element not found + } +} diff --git a/src/main/java/algorithms/SortingAlgorithms.java b/src/main/java/algorithms/SortingAlgorithms.java new file mode 100644 index 0000000..b7f22fb --- /dev/null +++ b/src/main/java/algorithms/SortingAlgorithms.java @@ -0,0 +1,35 @@ +package src.main.java.algorithms; + + +public class SortingAlgorithms { + + public static void bubbleSort(int[] arr) { + int n = arr.length; + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + // swap arr[j] and arr[j+1] + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + + public static void selectionSort(int[] arr) { + int n = arr.length; + for (int i = 0; i < n - 1; i++) { + int minIndex = i; + for (int j = i + 1; j < n; j++) { + if (arr[j] < arr[minIndex]) { + minIndex = j; + } + } + // Swap the found minimum element with the first element + int temp = arr[minIndex]; + arr[minIndex] = arr[i]; + arr[i] = temp; + } + } +} diff --git a/src/main/java/data_structures/DS.java b/src/main/java/data_structures/DS.java new file mode 100644 index 0000000..7ac8c94 --- /dev/null +++ b/src/main/java/data_structures/DS.java @@ -0,0 +1,146 @@ +package src.main.java.data_structures; +import java.util.*; + +public class DS { + //Array + void Array_(){ + int[] array = {10, 20, 30, 40, 50}; + System.out.println("Element at index 0: " + array[0]); + System.out.println("Element at index 2: " + array[2]); + System.out.println("Element at index 4: " + array[4]); + + //Sum of elements. + int sum = 0; + for(int i = 0; i < array.length; i++){ + sum =+ array[i]; + } + System.out.println("Sum of elements: " + sum); + + //Updating index 2 + array[2] = 35; + System.out.println("Updated element at index 2: "+array[2]); + + System.out.println("Element in the Array: "); + for(int e : array){ + System.out.println(e); + } + } + + //ArrayList + void ArrayList_(){ + ArrayList arrayList = new ArrayList(List.of(10, 20, 30 ,40, 50)); + + System.out.println("Element at index 0: "+arrayList.get(0)); + System.out.println("Element at index 2: "+arrayList.get(2)); + System.out.println("Element at index 4: "+arrayList.get(4)); + + //Sum of all elements in the arrayList + int sum = arrayList.stream().mapToInt(Integer::intValue).sum(); + System.out.println("Sum of ArrayList elements: "+sum); + + //Updating element at index 2. + int two = arrayList.set(2, 35); + System.out.println("Updated element at index 2: "+two); + + //Printing all ArrayList elements + System.out.println("Element in arrayList: "); + for(int e: arrayList){ + System.out.println(e); + } + } + + void LinkedList_(){ + LinkedList linkedList = new LinkedList<>(); + linkedList.add(10); + linkedList.add(20); + linkedList.add(30); + linkedList.add(40); + linkedList.add(50); + System.out.println("LinkedList:"+linkedList); + + //Removing first element + linkedList.removeFirst(); + System.out.println("LinkedList after removing first element:"+linkedList); + + //Contains + boolean contain = linkedList.contains(30); + System.out.println("LinkedList contains element '30'? " + contain); + + int first = linkedList.getFirst(); + int last = linkedList.getLast(); + System.out.println("First element: "+first); + System.out.println("Last element: "+last); + + //clearing the linked list. + linkedList.clear(); + System.out.println("LinkedList after clearing: "+linkedList); + } + + void Stack_(){ + Stack stack = new Stack<>(); + stack.push(10); + stack.push(20); + stack.push(30); + + //Peek() and pop() + System.out.println("Top element: "+ stack.peek()); + System.out.println("Popped element: "+ stack.pop()); + System.out.println("Is stack empty: "+ stack.isEmpty()); + System.out.println("Stack size: "+ stack.size()); + //printing stack elements. + System.out.println("Stack elements:"); + for(int e:stack){ + System.out.println(e); + } + } + + void Queue_(){ + Queue queue = new LinkedList(); + queue.add(10); + queue.add(20); + queue.add(30); + queue.add(40); + queue.add(50); + + System.out.println("Front element " + queue.peek()); + + //Dequeuing + while(!queue.isEmpty()){ + int e = queue.poll(); + System.out.println("Dequeue element"+e); + } + } + + void HashMap_(){ + HashMap hashMap = new HashMap(); + hashMap.put("John", 25); + hashMap.put("Alice", 30); + hashMap.put("Bob",20); + + System.out.println("Age of John: " + hashMap.get("John")); + System.out.println("Age ofAlice: " + hashMap.get("Alice")); + + System.out.println("Is Bob present? "+ hashMap.containsKey("Bob")); + + for(String key: hashMap.keySet()){ + System.out.println(key+ ":"+ hashMap.get(key)); + } + + System.out.println("Size of the HashMap: " + hashMap.size()); + } + + + //Main Function + public static void main(String[] args) { + DS h = new DS(); + h.Array_(); + System.out.println(); + h.ArrayList_(); + System.out.println(); + h.HashMap_(); + System.out.println(); + h.Queue_(); + System.out.println(); + h.LinkedList_(); + } +} diff --git a/src/main/java/data_structures/DynamicArray.java b/src/main/java/data_structures/DynamicArray.java new file mode 100644 index 0000000..e19cba3 --- /dev/null +++ b/src/main/java/data_structures/DynamicArray.java @@ -0,0 +1,71 @@ +package src.main.java.data_structures; +public class DynamicArray { + int size, capacity = 10; + Object[] array; + + public DynamicArray() { + this.capacity = capacity; + this.array = new Object[capacity]; + } + + public void add(Object data){ + if (size >= capacity){ + grow(); + } + array[size] = data; + size++; + } + public void insert(int index, Object data){ + if(size >= capacity){ + grow(); + } + for(int i = size; i > index; i--){ + array[i] = array[i - 1]; + } + array[index] = data; + size++; + } + public void delete(Object data){ + for(int i = 0; i < size; i++){ + if(array[i] == data){ + for(int j = 0; j < (size - i - 1); j++){ + array[i + j] = array[i + j + 1]; + } + array[size - 1] = null; + size--; + if(size <= (int)(capacity/3)){ + shrink(); + } + break; + } + } +} + public int search(Object data){ + return -1; + } + + private void grow(){ + + } + private void shrink(){ + + } + public boolean isEmpty(){ + return size == 0; + } + + public String toString(){ + String string = ""; + + for (int i =0; i < capacity; i++){ + string += array[i] + ", "; + } + if(string != ""){ + string = "["+string.substring(0, string.length() - 2) + "]"; + } + else{ + string = "[]"; + } + return string; + } +} diff --git a/src/main/java/data_structures/Main_HashMap.java b/src/main/java/data_structures/Main_HashMap.java new file mode 100644 index 0000000..6db2047 --- /dev/null +++ b/src/main/java/data_structures/Main_HashMap.java @@ -0,0 +1,31 @@ +package src.main.java.data_structures; +import java.util.*; + +public class Main_HashMap { + public static void main(String[] args) { + HashMap hashMap = new HashMap<>(); + //Adding keys to a hash map. + hashMap.put("John", 25); + hashMap.put("Alice", 30); + hashMap.put("Bob", 35); + + //Displaying the integer values. + int alice = hashMap.get("Alice"); + int john = hashMap.get("John"); + System.out.println("Age of John: " + john); + System.err.println("Age of Alice: " + alice); + + //Checking if a key is present. + Boolean isPresent = hashMap.containsKey("Bob"); + System.out.println("Is Bob present? " + isPresent); + + //Printing all key-value pairs. + for(String key: hashMap.keySet()){ + System.out.println(key+":"+hashMap.get(key)); + } + + //Size of the hash map + int Size = hashMap.size(); + System.out.println("Size of the hash map: "+Size); + } +} diff --git a/src/main/java/data_structures/Main_LinkedList.java b/src/main/java/data_structures/Main_LinkedList.java new file mode 100644 index 0000000..3977c3d --- /dev/null +++ b/src/main/java/data_structures/Main_LinkedList.java @@ -0,0 +1,36 @@ +package src.main.java.data_structures; +import java.util.*; + +public class Main_LinkedList { + public static void main(String[] args) { + //LinkedList. + LinkedList linkedList = new LinkedList<>(); + //Adding elements to linkedList + linkedList.add(10); + linkedList.add(20); + linkedList.add(30); + linkedList.add(40); + linkedList.add(50); + + System.out.println("LinkedList:"+ linkedList); + + //Removing first element. + linkedList.removeFirst(); + System.out.println("LinkedList after removing first element:"+linkedList); + + //Checking if an element is in the linked list. + Boolean Check = linkedList.contains(30); + System.out.println("LinkedList contains element '30'? " + Check); + + int first = linkedList.getFirst(); + int last = linkedList.getLast(); + + System.out.println("First element: " + first); + System.out.println("Last element: " + last); + + // Clearing the linked list elements + linkedList.clear(); + System.out.println("LinkedList after clearing: " + linkedList); + + } +} diff --git a/src/main/java/data_structures/Main_Queue.java b/src/main/java/data_structures/Main_Queue.java new file mode 100644 index 0000000..88e2080 --- /dev/null +++ b/src/main/java/data_structures/Main_Queue.java @@ -0,0 +1,23 @@ +package src.main.java.data_structures; +import java.util.*; + +public class Main_Queue { + public static void main(String[] args) { + Queue queue = new LinkedList(); + queue.offer(10); + queue.offer(20); + queue.offer(30); + queue.offer(40); + queue.offer(50); + + //Print first element + int first = queue.peek(); + System.out.println("Front element: " + first); + + //Dequeuing the elements. + while(!queue.isEmpty()) { + int e = queue.poll(); + System.out.println("Dequeue element: " + e); + } + } +} diff --git a/src/main/java/data_structures/Main_Stack.java b/src/main/java/data_structures/Main_Stack.java new file mode 100644 index 0000000..7941700 --- /dev/null +++ b/src/main/java/data_structures/Main_Stack.java @@ -0,0 +1,30 @@ +package src.main.java.data_structures; +import java.util.*; + +public class Main_Stack { + public static void main(String[] args) { + Stack stack = new Stack(); + + //Adding stack elements + stack.push(10); + stack.push(20); + stack.push(30); + + //Viewing the top elements of a STack. + int top = stack.peek(); + System.out.println("Top element: " + top); + System.out.println("Popped element: " + stack.pop()); + + //Checking if stack is empty. + boolean isEmpty = stack.empty(); + System.out.println("Is stack empty: " + isEmpty); + + //Checking the size + int Size = stack.size(); + System.out.println("Stack size: " +Size); + //Printing stack elements. + System.out.println("Stack Elements:"); + for(int e : stack) + System.out.println(e); + } +} diff --git a/src/main/java/utils/utils.java b/src/main/java/utils/utils.java new file mode 100644 index 0000000..f42652c --- /dev/null +++ b/src/main/java/utils/utils.java @@ -0,0 +1,10 @@ +package src.main.java.utils; + +public class utils { + public static void printArray(int[] array) { + for (int value : array) { + System.out.print(value + " "); + } + System.out.println(); + } +} diff --git a/src/test/java/algorithms/SearchingAlgorithmsTest.java b/src/test/java/algorithms/SearchingAlgorithmsTest.java new file mode 100644 index 0000000..bb354a9 --- /dev/null +++ b/src/test/java/algorithms/SearchingAlgorithmsTest.java @@ -0,0 +1,25 @@ +package algorithms; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class SearchingAlgorithmsTest { + + @Test + void testLinearSearch() { + int[] array = {5, 3, 8, 6}; + + assertEquals(2, SearchingAlgorithms.linearSearch(array, 8)); + + assertEquals(-1, SearchingAlgorithms.linearSearch(array, 10)); + } + + @Test + void testBinarySearch() { + int[] sortedArray = {1, 2, 3, 4, 5}; + + assertEquals(2, SearchingAlgorithms.binarySearch(sortedArray, 3)); + + assertEquals(-1, SearchingAlgorithms.binarySearch(sortedArray, 10)); + } +} diff --git a/src/test/java/algorithms/SortingAlgorithmsTest.java b/src/test/java/algorithms/SortingAlgorithmsTest.java new file mode 100644 index 0000000..3f585ac --- /dev/null +++ b/src/test/java/algorithms/SortingAlgorithmsTest.java @@ -0,0 +1,25 @@ +package algorithms; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class SortingAlgorithmsTest { + + @Test + void testBubbleSort() { + int[] array = {64, 34, 25, 12, 22}; + + SortingAlgorithms.bubbleSort(array); + + assertArrayEquals(new int[]{12, 22, 25, 34, 64}, array); + } + + @Test + void testSelectionSort() { + int[] array = {64, 34, 25, 12, 22}; + + SortingAlgorithms.selectionSort(array); + + assertArrayEquals(new int[]{12, 22, 25, 34, 64}, array); + } +} diff --git a/src/test/java/data_structures/LinkedListTest.java b/src/test/java/data_structures/LinkedListTest.java new file mode 100644 index 0000000..8edb38a --- /dev/null +++ b/src/test/java/data_structures/LinkedListTest.java @@ -0,0 +1,21 @@ +package src.test.java.data_structures; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + + @Test + void testAddAndPrint() { + LinkedList list = new LinkedList(); + list.add(10); + list.add(20); + + // Capture output from printList method here if needed. + + // Assertions can go here based on expected output. + + // For demonstration purposes: + assertEquals(10, list.head.data); + } +} \ No newline at end of file diff --git a/src/test/java/data_structures/StackTest.java b/src/test/java/data_structures/StackTest.java new file mode 100644 index 0000000..ed08f95 --- /dev/null +++ b/src/test/java/data_structures/StackTest.java @@ -0,0 +1,25 @@ +package data_structures; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class StackTest { + + @Test + void testPushPop() { + Stack stack = new Stack(5); + + stack.push(10); + stack.push(20); + + assertEquals(20, stack.pop()); + + assertFalse(stack.isEmpty()); + + assertEquals(10, stack.pop()); + + assertTrue(stack.isEmpty()); + + assertThrows(EmptyStackException.class, () -> stack.pop()); + } +}