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/Product.java b/src/main/java/br/com/zevolution/algorithms/sorting/Product.java new file mode 100644 index 0000000..37791ef --- /dev/null +++ b/src/main/java/br/com/zevolution/algorithms/sorting/Product.java @@ -0,0 +1,40 @@ +package br.com.zevolution.algorithms.sorting; + +public class Product { + + private String description; + private double price; + + public Product(String description, double price) { + this.description = description; + this.price = price; + } + + public double getPrice() { + return price; + } + + @Override + public String toString() { + return this.description; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + if (this.getClass() != obj.getClass()) + return false; + + Product other = Product.class.cast(obj); + return other.toString().equals(this.toString()) && + other.getPrice() == this.getPrice(); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/zevolution/algorithms/sorting/Sort.java b/src/main/java/br/com/zevolution/algorithms/sorting/Sort.java new file mode 100644 index 0000000..ff805ad --- /dev/null +++ b/src/main/java/br/com/zevolution/algorithms/sorting/Sort.java @@ -0,0 +1,7 @@ +package br.com.zevolution.algorithms.sorting; + +public interface Sort { + + Product[] sort(Product[] products, int length); + +} diff --git a/src/main/java/br/com/zevolution/algorithms/sorting/insertionsort/InsertionSort.java b/src/main/java/br/com/zevolution/algorithms/sorting/insertionsort/InsertionSort.java new file mode 100644 index 0000000..86b26d6 --- /dev/null +++ b/src/main/java/br/com/zevolution/algorithms/sorting/insertionsort/InsertionSort.java @@ -0,0 +1,30 @@ +package br.com.zevolution.algorithms.sorting.insertionsort; + +import br.com.zevolution.algorithms.sorting.Product; +import br.com.zevolution.algorithms.sorting.Sort; + +public class InsertionSort implements Sort { + + public Product[] sort(Product[] products, int length) { + Product[] array = products.clone(); + for (int current = 1; current < length; current++) { + int beingAnalyzed = current; + + while (beingAnalyzed > 0) { + int previousBeingAnalyzed = beingAnalyzed - 1; + + Product currentProductBeingAnalyzed = array[beingAnalyzed]; + Product previousProductBeingAnalyzed = array[previousBeingAnalyzed]; + if (currentProductBeingAnalyzed.getPrice() < previousProductBeingAnalyzed.getPrice()) { + array[beingAnalyzed] = previousProductBeingAnalyzed; + array[previousBeingAnalyzed] = currentProductBeingAnalyzed; + + beingAnalyzed--; + } else { + break; + } + } + } + return array; + } +} \ No newline at end of file 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/algorithms/sorting/selectionsort/Product.java b/src/main/java/br/com/zevolution/algorithms/sorting/selectionsort/Product.java deleted file mode 100644 index 11aa4d6..0000000 --- a/src/main/java/br/com/zevolution/algorithms/sorting/selectionsort/Product.java +++ /dev/null @@ -1,22 +0,0 @@ -package br.com.zevolution.algorithms.sorting.selectionsort; - -public class Product { - - private String description; - private double price; - - public Product(String description, double price) { - this.description = description; - this.price = price; - } - - public double getPrice() { - return price; - } - - @Override - public String toString() { - return this.description; - } - -} diff --git a/src/main/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSort.java b/src/main/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSort.java index 1d01422..a6f8678 100644 --- a/src/main/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSort.java +++ b/src/main/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSort.java @@ -1,8 +1,11 @@ package br.com.zevolution.algorithms.sorting.selectionsort; -public class SelectionSort { +import br.com.zevolution.algorithms.sorting.Product; +import br.com.zevolution.algorithms.sorting.Sort; + +public class SelectionSort implements Sort { - public static Product[] sortingByCheapest(Product[] products, int length) { + public Product[] sort(Product[] products, int length) { Product[] array = products.clone(); for (int current = 0; current < length - 1; current++) { int cheapest = getCheapest(array, current, length - 1); @@ -16,7 +19,7 @@ public static Product[] sortingByCheapest(Product[] products, int length) { return array; } - private static int getCheapest(Product[] products, int beginIndex, int endIndex) { + private int getCheapest(Product[] products, int beginIndex, int endIndex) { int cheapest = beginIndex; for (int current = beginIndex; current <= endIndex; current++) { if (products[current].getPrice() < products[cheapest].getPrice()) { 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 new file mode 100644 index 0000000..ff120e5 --- /dev/null +++ b/src/test/java/br/com/zevolution/algorithms/sorting/SortTester.java @@ -0,0 +1,21 @@ +package br.com.zevolution.algorithms.sorting; + +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) { + assertArrayEquals(SORTED_BY_LOWEST_PRICE, sorter.sort(SORTED_BY_HIGHEST_PRICE, SORTED_BY_HIGHEST_PRICE.length)); + 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/SortingTestData.java b/src/test/java/br/com/zevolution/algorithms/sorting/SortingTestData.java new file mode 100644 index 0000000..53a15c5 --- /dev/null +++ b/src/test/java/br/com/zevolution/algorithms/sorting/SortingTestData.java @@ -0,0 +1,37 @@ +package br.com.zevolution.algorithms.sorting; + +public class SortingTestData { + + public static final Product[] SORTED_BY_LOWEST_PRICE = { + new Product("Mouse", 50), + new Product("Keyboard", 100), + new Product("Notebook", 3500), + new Product("iPhone 12 Pro Max Ultra Uou", 10000), + new Product("iMac", 30000) + }; + + public static final Product[] SORTED_BY_HIGHEST_PRICE = { + new Product("iMac", 30000), + new Product("iPhone 12 Pro Max Ultra Uou", 10000), + new Product("Notebook", 3500), + new Product("Keyboard", 100), + new Product("Mouse", 50) + }; + + public static final Product[] NOT_SORTED = { + new Product("iPhone 12 Pro Max Ultra Uou", 10000), + new Product("Keyboard", 100), + new Product("Notebook", 3500), + new Product("Mouse", 50), + new Product("iMac", 30000) + }; + + public static final Product[] SAME_PRODUCT = { + new Product("Mouse", 50), + new Product("Mouse", 50), + new Product("Mouse", 50), + new Product("Mouse", 50), + new Product("Mouse", 50) + }; + +} diff --git a/src/test/java/br/com/zevolution/algorithms/sorting/insertionsort/InsertionSortTest.java b/src/test/java/br/com/zevolution/algorithms/sorting/insertionsort/InsertionSortTest.java new file mode 100644 index 0000000..33a50a4 --- /dev/null +++ b/src/test/java/br/com/zevolution/algorithms/sorting/insertionsort/InsertionSortTest.java @@ -0,0 +1,14 @@ +package br.com.zevolution.algorithms.sorting.insertionsort; + +import static br.com.zevolution.algorithms.sorting.SortTester.testSort; + +import org.junit.Test; + +public class InsertionSortTest { + + @Test + public void should_Sort_Array() { + testSort(new InsertionSort()); + } + +} 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/algorithms/sorting/selectionsort/SelectionSortTest.java b/src/test/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSortTest.java index ceceadd..754be75 100644 --- a/src/test/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSortTest.java +++ b/src/test/java/br/com/zevolution/algorithms/sorting/selectionsort/SelectionSortTest.java @@ -1,39 +1,14 @@ package br.com.zevolution.algorithms.sorting.selectionsort; -import org.junit.Test; - -import static org.junit.Assert.*; +import static br.com.zevolution.algorithms.sorting.SortTester.testSort; -import org.junit.Before; +import org.junit.Test; public class SelectionSortTest { - private static final double CHEAPEST_PRODUCT = 5; - private static final double MOST_EXPENSIVE_PRODUCT = 50; - private Product[] products; - - @Before - public void init() { - Product[] array = { - new Product("iMac", MOST_EXPENSIVE_PRODUCT), - new Product("iPhone", 8), - new Product("Notebook", 7), - new Product("Keyboard", 9), - new Product("Mouse", CHEAPEST_PRODUCT) - }; - this.products = array; - } - @Test - public void should_Get_CheapestProduct() { - Product[] ordened = SelectionSort.sortingByCheapest(this.products, this.products.length); - assertEquals(CHEAPEST_PRODUCT, ordened[0].getPrice(), 0); + public void should_Sort_Array() { + testSort(new SelectionSort()); } - - @Test - public void should_Get_MostExpensiveProduct() { - Product[] ordened = SelectionSort.sortingByCheapest(this.products, this.products.length); - assertEquals(MOST_EXPENSIVE_PRODUCT, ordened[ordened.length - 1].getPrice(), 0); - } - + } 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)); + } + }