master
Caleb Fontenot 2024-01-26 15:30:51 +07:00
parent aef4bcc29c
commit 7962e14215
5 changed files with 262 additions and 0 deletions

2
.gitignore vendored

@ -198,3 +198,5 @@
/Semester 4/Assignments/DAO/target/
/Semester 4/Assignments/ProjectTrees_CalebFontenot/target/
/Semester 4/Assignments/DataStructures/target/
/MergeSort/target/
/Semester 3/Exams/ProgrammingExam2_CalebFontenot/target/

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.calebfontenot</groupId>
<artifactId>MergeSort</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mergesort.MergeSort</exec.mainClass>
</properties>
</project>

@ -0,0 +1,88 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.mergesort;
/**
*
* @author caleb
*/
public class MergeSort {
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String args[]) {
// merge sort = recursively divide array in 2, sort, re-combine
// run-time complexity = O(n Log n)
// space complexity = O(n)
int[] array = {8, 2, 5, 3, 4, 7, 6, 1, 0};
printArray(array);
mergeSort(array);
printArray(array);
}
private static void mergeSort(int[] array) {
int length = array.length;
if (length <= 1) {
return; //base case
}
int middle = length / 2;
int[] leftArray = new int[middle];
int[] rightArray = new int[length - middle];
int i = 0; //left array
int j = 0; //right array
for (; i < length; i++) {
if (i < middle) {
leftArray[i] = array[i];
} else {
rightArray[j] = array[i];
j++;
}
}
mergeSort(leftArray);
mergeSort(rightArray);
merge(leftArray, rightArray, array);
}
private static void merge(int[] leftArray, int[] rightArray, int[] array) {
int leftSize = array.length / 2;
int rightSize = array.length - leftSize;
int i = 0, l = 0, r = 0; //indices
//check the conditions for merging
while (l < leftSize && r < rightSize) {
if (leftArray[l] < rightArray[r]) {
array[i] = leftArray[l];
i++;
l++;
} else {
array[i] = rightArray[r];
i++;
r++;
}
}
while (l < leftSize) {
array[i] = leftArray[l];
i++;
l++;
}
while (r < rightSize) {
array[i] = rightArray[r];
i++;
r++;
}
}
}

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.calebfontenot</groupId>
<artifactId>ProgrammingExam2_CalebFontenot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>com.calebfontenot.programmingexam2_calebfontenot.ProgrammingExam2_CalebFontenot</exec.mainClass>
</properties>
</project>

@ -0,0 +1,144 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.programmingexam2_calebfontenot;
/**
*
* @author caleb
*/
/**
*
* @author ar114
* @param <T>
*/
public class ProgrammingExam2_CalebFontenot<T> {
Node<T> head;
Node<T> tail;
class Node<T> {
T t;
Node<T> l;
Node<T> r;
}
public T removeAt(int pos) {
if (pos < 0 || pos > size() - 1) {
throw new IndexOutOfBoundsException();
}
//list is empty
if (size() == 0) {
throw new RuntimeException("The list empty.");
}
T returnT = null;
if (pos == 0)//remove at 0
{
Node<T> pointer = head.r;
returnT = (T) head.t;
pointer.l = null;
head = pointer;
} else if (pos == (size() - 1))//remove at end
{
Node<T> pointer = tail.l.l;
returnT = (T) tail.t;
pointer.r = null;
tail = pointer;
} else//remove in the middle
{
// Iterate to the element position
Node<T> pointer = head;
for (int i = 0; i < pos - 1; ++i) {
pointer = pointer.r;
}
returnT = (T) pointer.r.t;
pointer.r = pointer.r.r;
pointer.l = pointer.r;
}
return returnT;
}
public void clear() {
head = tail = null;
}
public void addAt(T t, int pos) {
if (pos < 0 || pos > size()) {
throw new IndexOutOfBoundsException();
}
Node<T> newNode = new Node<T>();
newNode.t = t;
if (head == null)//list is empty
{
head = tail = newNode;
} else if (pos == 0)//add at the front
{
newNode.r = head;
head.l = newNode;
head = newNode;
} else if (pos == size())//add at the end
{
newNode.l = tail;
tail.r = newNode;
tail = newNode;
} else//middle
{
Node<T> p = head;
for (int i = 0; i < pos - 1; ++i) {
p = p.r;
}
newNode.l = p;
newNode.r = p.r;
p.r.l = newNode;
p.r = newNode;
}
}
public int size() {
Node<T> p = head;
int count = 0;
while (p != null) {
count++;
p = p.r;
}
return count;
}
@Override
public String toString() {
String s = "";
Node<T> p = head;
while (p != null) {
s += p.t.toString() + " ";
p = p.r;
}
return s;
}
public static void main(String[] args) {
ProgrammingExam2_CalebFontenot<Integer> list = new ProgrammingExam2_CalebFontenot<Integer>();
list.addAt(20, 0);
list.addAt(10, 0);
list.addAt(40, 2);
list.addAt(30, 2);
list.addAt(50, 4);
System.out.println(list);
System.out.println(list.removeAt(0));
System.out.println(list);
System.out.println(list.removeAt(2));
System.out.println(list);
System.out.println(list.removeAt(2));
System.out.println(list.toString());
}
}