Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/algorithms/SearchingAlgorithms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package src.main.java.algorithms;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a searching algorithm class




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
}
}
35 changes: 35 additions & 0 deletions src/main/java/algorithms/SortingAlgorithms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package src.main.java.algorithms;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a searching algorithm class



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;
}
}
}
146 changes: 146 additions & 0 deletions src/main/java/data_structures/DS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package src.main.java.data_structures;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a main data-structure class

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 <Integer> arrayList = new ArrayList<Integer>(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 <Integer> 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 <Integer> 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 <Integer> queue = new LinkedList<Integer>();
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 <String, Integer> hashMap = new HashMap<String, Integer>();
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_();
}
}
71 changes: 71 additions & 0 deletions src/main/java/data_structures/DynamicArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package src.main.java.data_structures;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented Dynamic Array class

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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/data_structures/Main_HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package src.main.java.data_structures;
import java.util.*;

public class Main_HashMap {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap Implementation

public static void main(String[] args) {
HashMap <String, Integer> 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);
}
}
36 changes: 36 additions & 0 deletions src/main/java/data_structures/Main_LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package src.main.java.data_structures;
import java.util.*;

public class Main_LinkedList {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedList Implementation

public static void main(String[] args) {
//LinkedList.
LinkedList <Integer> 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);

}
}
23 changes: 23 additions & 0 deletions src/main/java/data_structures/Main_Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package src.main.java.data_structures;
import java.util.*;

public class Main_Queue {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Queue Implementation

public static void main(String[] args) {
Queue <Integer> queue = new LinkedList<Integer>();
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);
}
}
}
Loading