diff --git a/.gitignore b/.gitignore index 1933432..149dcaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .attach_pid* /src/target/ /src/.attach_pid* +/target !.mvn/wrapper/maven-wrapper.jar ### sts ### diff --git a/README.md b/README.md new file mode 100644 index 0000000..468117b --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +

+AlgoAndDataStruct-Logo +

Java Algorithms and Data Structures

+ +## Table of contents +- [About the project](#about-the-project) +- [Description](#description) +- [Built with](#built-with) +- [Installation](#installation) +- [Run](#run) +- [License](#license) + +## About the project +Basically, it's about algorithms and data structure and was created for the purpose of learning, researching, and recording all the content so that I can return when necessary. + +## Description +This project contains basic implementations using Java so that anyone can learn from examples. + +## Built with +* [Java 8](https://java.com/en/download/help/java8.html) +* [JUnit 4](https://junit.org/junit4/) +* [Maven](https://maven.apache.org/) + +## Installation + +To clone and run this application, you'll need Git installed on your computer(or no, if you want to download **.zip**). From your command line: +```bash +# Git CLI +git clone https://github.com/zevolution/java-algorithms-and-datastructure.git + +# Github CLI +gh repo clone zevolution/java-algorithms-and-datastructure +``` + +## Run +```bash +mvn test +``` + +## License +[MIT](https://choosealicense.com/licenses/mit/) \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..79c812b --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + br.com.zevolution + java-algorithms-and-datastructure + 0.0.1-SNAPSHOT + java-algorithms-and-datastructure + Algorithms and data structures implemented in Java + + + 1.8 + + + + + junit + junit + 4.12 + + + diff --git a/src/main/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSort.java b/src/main/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSort.java new file mode 100644 index 0000000..e6998ed --- /dev/null +++ b/src/main/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSort.java @@ -0,0 +1,62 @@ +package br.com.zevolution.algorithms.sorting.mergesort; + +import br.com.zevolution.algorithms.sorting.Product; +import br.com.zevolution.algorithms.sorting.Sort; + +public class MergeSort implements Sort { + + public Product[] sort(Product[] products, int length) { + Product[] array = products.clone(); + mergeSort(array, 0, length); + return array; + } + + private void mergeSort(Product[] products, int start, int end) { + int length = end - start; + + if (length > 1) { + int middle = (start + end) >> 1; + + mergeSort(products, start, middle); + mergeSort(products, middle, end); + mergeSort(products, start, middle, end); + } + } + + private void mergeSort(Product[] products, int low, int middle, int high) { + Product[] array = new Product[high-low]; + + int current = 0; + int left = low; + int right = middle; + + while (left < middle && right < high) { + if (products[left].getPrice() < products[right].getPrice()) { + array[current] = products[left]; + left++; + } else { + array[current] = products[right]; + right++; + } + current++; + } + + while (left < middle) { + array[current] = products[left]; + left++; + current++; + } + + while (right < high) { + array[current] = products[right]; + right++; + current++; + } + + for (int i = 0; i < current; i++ ) { + products[low + i] = array[i]; + } + + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/zevolution/algorithms/sorting/quicksort/QuickSort.java b/src/main/java/br/com/zevolution/algorithms/sorting/quicksort/QuickSort.java new file mode 100644 index 0000000..2dbff8f --- /dev/null +++ b/src/main/java/br/com/zevolution/algorithms/sorting/quicksort/QuickSort.java @@ -0,0 +1,49 @@ +package br.com.zevolution.algorithms.sorting.quicksort; + +import br.com.zevolution.algorithms.sorting.Product; +import br.com.zevolution.algorithms.sorting.Sort; + +public class QuickSort implements Sort { + + @Override + public Product[] sort(Product[] products, int length) { + Product[] array = products.clone(); + this.quickSort(array, 0, length); + return array; + + } + + private void quickSort(Product[] products, int start, int end) { + int length = end - start; + if (length > 1) { + int pivot = this.partitionProducts(products, start, end); + this.quickSort(products, start, pivot); + this.quickSort(products, pivot + 1, end); + } + } + + private int partitionProducts(Product[] products, int start, int end) { + int cheapest = start; + + Product pivotProduct = products[end - 1]; + for (int current = start; current < end - 1; current++) { + Product currentProduct = products[current]; + if (currentProduct.getPrice() <= pivotProduct.getPrice()) { + this.swap(products, current, cheapest); + cheapest++; + } + } + + this.swap(products, end - 1, cheapest); + + return cheapest; + } + + private void swap(Product[] products, int from, int to) { + Product productFrom = products[from]; + Product productTo = products[to]; + products[from] = productTo; + products[to] = productFrom; + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedList.java b/src/main/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedList.java index 3954268..997996c 100644 --- a/src/main/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedList.java +++ b/src/main/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedList.java @@ -58,6 +58,33 @@ public void removeFirst() { } } + public void remove(Object element) { + Node current = this.first; + Node previous = null; + + if (current.getElement() == element) { + this.first = current.getNext(); + current = null; + this.totalElements--; + return; + } + + while (current != null) { + if (current.getElement() == element) { + break; + } + + previous = current; + current = current.getNext(); + } + + if (current == null) return; + + previous.setNext(current.getNext()); + current = null; + this.totalElements--; + } + public Object get(int position) { return this.getNode(position).getElement(); } diff --git a/src/test/java/br/com/zevolution/algorithms/sorting/SortTester.java b/src/test/java/br/com/zevolution/algorithms/sorting/SortTester.java index c6b8afd..ff120e5 100644 --- a/src/test/java/br/com/zevolution/algorithms/sorting/SortTester.java +++ b/src/test/java/br/com/zevolution/algorithms/sorting/SortTester.java @@ -3,6 +3,8 @@ import static br.com.zevolution.algorithms.sorting.SortingTestData.*; import static org.junit.Assert.*; +import java.util.Arrays; + public class SortTester { public static void testSort(Sort sorter) { @@ -10,6 +12,10 @@ public static void testSort(Sort sorter) { assertArrayEquals(SORTED_BY_LOWEST_PRICE, sorter.sort(SORTED_BY_LOWEST_PRICE, SORTED_BY_LOWEST_PRICE.length)); assertArrayEquals(SORTED_BY_LOWEST_PRICE, sorter.sort(NOT_SORTED, NOT_SORTED.length)); assertArrayEquals(SAME_PRODUCT, sorter.sort(SAME_PRODUCT, SAME_PRODUCT.length)); + + // Make sure which original array it's not modified + sorter.sort(NOT_SORTED, NOT_SORTED.length); + assertFalse(Arrays.equals(SORTED_BY_LOWEST_PRICE, NOT_SORTED)); } } \ No newline at end of file diff --git a/src/test/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSortTest.java b/src/test/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSortTest.java new file mode 100644 index 0000000..22cc747 --- /dev/null +++ b/src/test/java/br/com/zevolution/algorithms/sorting/mergesort/MergeSortTest.java @@ -0,0 +1,14 @@ +package br.com.zevolution.algorithms.sorting.mergesort; + +import static br.com.zevolution.algorithms.sorting.SortTester.testSort; + +import org.junit.Test; + +public class MergeSortTest { + + @Test + public void should_Sort_Array() { + testSort(new MergeSort()); + } + +} diff --git a/src/test/java/br/com/zevolution/algorithms/sorting/quicksort/QuickSortTest.java b/src/test/java/br/com/zevolution/algorithms/sorting/quicksort/QuickSortTest.java new file mode 100644 index 0000000..15b096e --- /dev/null +++ b/src/test/java/br/com/zevolution/algorithms/sorting/quicksort/QuickSortTest.java @@ -0,0 +1,15 @@ +package br.com.zevolution.algorithms.sorting.quicksort; + +import static br.com.zevolution.algorithms.sorting.SortTester.testSort; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class QuickSortTest { + + @Test + public void should_Sort_Array() { + testSort(new QuickSort()); + } + +} diff --git a/src/test/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedListTest.java b/src/test/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedListTest.java index 1166a82..e3ee958 100644 --- a/src/test/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedListTest.java +++ b/src/test/java/br/com/zevolution/datastructure/linkedlist/CustomLinkedListTest.java @@ -93,4 +93,18 @@ public void should_ThrowException_When_NoSuchElement() { linkedList.removeFirst(); } + @Test + public void should_RemoveMiddleElement() { + CustomLinkedList linkedList = new CustomLinkedList(); + linkedList.add("Bia"); + linkedList.add("Lucas"); + linkedList.add("Laura"); + + linkedList.remove("Lucas"); + + assertEquals(2, linkedList.size()); + assertEquals("Bia", linkedList.get(0)); + assertEquals("Laura", linkedList.get(1)); + } + }