Adding in more Algorithms and Data Structures implemented in Java

This commit is contained in:
Frank
2025-08-27 22:06:50 -06:00
parent eeb269ea63
commit 6a9d55e07d
6 changed files with 304 additions and 10 deletions

View File

@@ -4,30 +4,109 @@ import java.util.Random;
public class ArraySorting {
public static void main(String[] args) {
Random rand = new Random();
int[] numbers = new int[100000000];
int[] numbers = new int[10];
int[] array = { 20, 30, 40, 50, 60, 70, 80, 90, 100 };
for(int i = 0; i < numbers.length; i++) {
numbers[i] = rand.nextInt(10000);
numbers[i] = rand.nextInt(10);
}
System.out.println("Before: ");
// System.out.println("Before: " + Arrays.toString(numbers));
int result = binarySearch(array, 0, array.length - 1, 40);
// quickSort(numbers);
// mergeSort(numbers, 0, numbers.length - 1);
// selectionSort(numbers);
// bubbleSort(numbers);
// Arrays.sort(numbers);
// insertionSort(numbers, 4, 8);
System.out.println("After: ");
// insertionSort(numbers, 0, numbers.length);
// System.out.println("After: " + Arrays.toString(numbers));
if (result == -1) {
System.out.println("Element not present");
} else {
System.out.println("Element found at index " + result);
}
}
public static void quickSort(int[] numbers) {
private static int binarySearch(int[] array, int left, int right, int targetValue) {
if (right >= 1) {
int mid = 1 + (right - 1) / 2;
// If the element is present at the middle itself
if (array[mid] == targetValue) {
return mid;
}
// If the element is smaller than mid, hen it can only be present in left subarray
if (array[mid] > targetValue) {
return binarySearch(array, 1, mid - 1, targetValue);
}
// Else the element can only be present in the right subarray
return binarySearch(array, mid + 1, right, targetValue);
}
return -1;
}
/* QUICK SORT ALGORITHM */
private static void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
private static int partition(int[] array, int lowIndex, int highIndex, int pivot) {
int leftPointer = lowIndex;
int rightPointer = highIndex;
while (leftPointer < rightPointer) {
while (array[leftPointer] <= pivot && leftPointer < rightPointer) {
leftPointer++;
}
while (array[rightPointer] >= pivot && leftPointer < rightPointer) {
rightPointer--;
}
swap(array, leftPointer, rightPointer);
}
if (array[leftPointer] > array[highIndex]) {
swap(array, leftPointer, highIndex);
} else {
leftPointer = highIndex;
}
return leftPointer;
}
private static void quickSort(int[] array) {
quickSort(array, 0, array.length - 1);
}
private static void quickSort(int[] array, int lowIndex, int highIndex) {
// Check to see if it's trying to sort a subarray of one item
if (lowIndex >= highIndex) {
return;
}
int pivotIndex = new Random().nextInt(highIndex - lowIndex) + lowIndex;
int pivot = array[pivotIndex];
swap(array, pivotIndex, highIndex);
int leftPointer = partition(array, lowIndex, highIndex, pivot);
quickSort(array, lowIndex, leftPointer - 1);
quickSort(array, leftPointer + 1, highIndex);
}
/* END: QUICK SORT ALGORITHM */
/* MERGE SORT ALGORITHM */
public static void mergeSort(int[] array, int left, int right) {
private static void mergeSort(int[] array, int left, int right) {
if(left < right) {
int mid = (left+right) / 2; // Find the middle element
mergeSort(array, left, mid); // Sort the first half
@@ -35,7 +114,8 @@ public class ArraySorting {
merge(array, left, mid, right); // Merge the sorted halves
}
}
public static void merge(int[] array, int left, int mid, int right) {
private static void merge(int[] array, int left, int mid, int right) {
// Find sizes of two subarrays to be merged
int n1 = mid - left + 1;
int n2 = right - mid;

View File

@@ -0,0 +1,93 @@
public class CircularLinkedList {
public int size = 0;
public Node head = null;
public Node tail = null;
public static void main(String[] args) {
CircularLinkedList myList = new CircularLinkedList();
myList.addNodeToHead(75);
myList.addNodeToHead(50);
myList.addNodeToHead(25);
myList.print();
myList.addNodeToTail(100);
myList.print();
myList.rotateElement();
myList.print();
myList.deleteNodeFromTail();
myList.print();
myList.deleteNodeFromHead();
myList.print();
}
// Add a new Node at the start of the Linked List
public void addNodeToHead(int element) {
Node n = new Node(element);
if (size == 0) {
head = n;
tail = n;
} else {
n.next = head;
head = n;
tail.next = head;
}
size++;
}
// Add a new Node to the Tail of the Linked List
public void addNodeToTail(int element) {
if (size == 0) {
addNodeToHead(element);
} else {
Node n = new Node(element);
tail.next = n;
tail = n;
tail.next = head;
size++;
}
}
public void rotateElement() {
System.out.println("Rotating!");
tail = head;
head = head.next;
}
public void deleteNodeFromTail() {
System.out.println("\nDeleting Node " + tail.element + " from Tail");
if (tail.next == tail) {
tail = null;
}
Node newTail = tail;
while (newTail.next != tail) {
newTail = newTail.next;
}
newTail.next = tail.next;
tail = newTail;
}
public void deleteNodeFromHead() {
head = head.next;
tail.next = head;
size--;
}
public void print() {
System.out.println("The List so far: ");
Node temp = head;
do {
System.out.print(" " + temp.element);
temp = temp.next;
} while (temp != head);
System.out.println();
}
}

20
src/JavaLinkedList.java Normal file
View File

@@ -0,0 +1,20 @@
import java.util.LinkedList;
public class JavaLinkedList {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("I");
myList.add("S");
myList.add("T");
System.out.println("Linked List: " + myList);
myList.addFirst("L");
myList.addLast("9");
System.out.println("Updated Linked List: " + myList);
myList.remove("9");
System.out.println("After Removal: " + myList);
String item = myList.get(2);
myList.set(2, item + " Changed");
System.out.println("After the change: " + myList);
}
}

77
src/LinkedList.java Normal file
View File

@@ -0,0 +1,77 @@
import java.util.NoSuchElementException;
public class LinkedList implements Stack {
public static void main(String[] args) {
int item;
LinkedList items = new LinkedList();
item = 25;
for (int i = item; i < 150; i++) {
items.push(i + 5);
}
// Is the stack empty?
if (items.isEmpty()) {
System.out.println("Empty Stack");
} else {
// Peek at the top
System.out.println("Peeking at the top: " + items.peek());
// Show the size
System.out.println("Size of the Stack: " + items.size());
// Pop off the top item
System.out.println("Popping off the top item: " + items.pop());
}
}
private static class Node {
int item;
Node next;
public Node(int current) {
item = current;
}
}
public LinkedList() {
top = null;
}
private Node top;
public void push(int current) {
Node c = new Node(current);
c.next = top;
top = c;
}
public int pop() {
int returnItem;
returnItem = top.item;
top = top.next;
return returnItem;
}
public boolean isEmpty() {
return top == null;
}
public int size() {
int counter = 0;
for (Node node = top; node != null; node = node.next) {
counter++;
}
return counter;
}
public int peek() {
if (top == null) {
throw new NoSuchElementException();
}
return top.item;
}
}

8
src/Node.java Normal file
View File

@@ -0,0 +1,8 @@
public class Node {
int element;
Node next;
public Node(int element) {
this.element = element;
}
}

16
src/Stack.java Normal file
View File

@@ -0,0 +1,16 @@
public interface Stack {
// Add to Top
void push(int item);
// Remove from the top
int pop();
// Look at the first item
int peek();
// How many elements
int size();
// Is the stack empty
boolean isEmpty();
}