diff --git a/algorithms-miscellaneous-3/pom.xml b/algorithms-miscellaneous-3/pom.xml index c4017144c8..3cebdd09ac 100644 --- a/algorithms-miscellaneous-3/pom.xml +++ b/algorithms-miscellaneous-3/pom.xml @@ -18,6 +18,18 @@ ${org.assertj.core.version} test + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + com.google.guava + guava + ${guava.version} + @@ -34,6 +46,7 @@ 3.9.0 + 4.3 + 28.0-jre - \ No newline at end of file diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/checksortedlist/Employee.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/checksortedlist/Employee.java new file mode 100644 index 0000000000..796932728b --- /dev/null +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/checksortedlist/Employee.java @@ -0,0 +1,34 @@ +package com.baeldung.algorithms.checksortedlist; + +public class Employee { + + long id; + + String name; + + public Employee() { + } + + public Employee(long id, String name) { + super(); + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/checksortedlist/SortedListChecker.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/checksortedlist/SortedListChecker.java new file mode 100644 index 0000000000..d2aa159e6d --- /dev/null +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/checksortedlist/SortedListChecker.java @@ -0,0 +1,85 @@ +package com.baeldung.algorithms.checksortedlist; + +import static org.apache.commons.collections4.CollectionUtils.isEmpty; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +import com.google.common.collect.Comparators; +import com.google.common.collect.Ordering;; + +public class SortedListChecker { + + private SortedListChecker() { + throw new AssertionError(); + } + + public static boolean checkIfSortedUsingIterativeApproach(List listOfStrings) { + if (isEmpty(listOfStrings) || listOfStrings.size() == 1) { + return true; + } + + Iterator iter = listOfStrings.iterator(); + String current, previous = iter.next(); + while (iter.hasNext()) { + current = iter.next(); + if (previous.compareTo(current) > 0) { + return false; + } + } + return true; + } + + public static boolean checkIfSortedUsingIterativeApproach(List employees, Comparator employeeComparator) { + if (isEmpty(employees) || employees.size() == 1) { + return true; + } + + Iterator iter = employees.iterator(); + Employee current, previous = iter.next(); + while (iter.hasNext()) { + current = iter.next(); + if (employeeComparator.compare(previous, current) > 0) { + return false; + } + } + return true; + } + + public static boolean checkIfSortedUsingRecursion(List listOfStrings) { + return isSortedRecursive(listOfStrings, listOfStrings.size()); + } + + public static boolean isSortedRecursive(List listOfStrings, int index) { + if (index < 2) { + return true; + } else if (listOfStrings.get(index - 2) + .compareTo(listOfStrings.get(index - 1)) > 0) { + return false; + } else { + return isSortedRecursive(listOfStrings, index - 1); + } + } + + public static boolean checkIfSortedUsingOrderingClass(List listOfStrings) { + return Ordering. natural() + .isOrdered(listOfStrings); + } + + public static boolean checkIfSortedUsingOrderingClass(List employees, Comparator employeeComparator) { + return Ordering.from(employeeComparator) + .isOrdered(employees); + } + + public static boolean checkIfSortedUsingOrderingClassHandlingNull(List listOfStrings) { + return Ordering. natural() + .nullsLast() + .isOrdered(listOfStrings); + } + + public static boolean checkIfSortedUsingComparators(List listOfStrings) { + return Comparators.isInOrder(listOfStrings, Comparator. naturalOrder()); + } + +} diff --git a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/checksortedlist/SortedListCheckerUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/checksortedlist/SortedListCheckerUnitTest.java new file mode 100644 index 0000000000..44c4388e6c --- /dev/null +++ b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/checksortedlist/SortedListCheckerUnitTest.java @@ -0,0 +1,106 @@ +package com.baeldung.algorithms.checksortedlist; + +import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingComparators; +import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingIterativeApproach; +import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingOrderingClass; +import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingRecursion; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class SortedListCheckerUnitTest { + + private List sortedListOfString; + private List unsortedListOfString; + private List singletonList; + + private List employeesSortedByName; + private List employeesNotSortedByName; + + @Before + public void setUp() { + sortedListOfString = asList("Canada", "HK", "LA", "NJ", "NY"); + unsortedListOfString = asList("LA", "HK", "NJ", "NY", "Canada"); + singletonList = Collections.singletonList("NY"); + + employeesSortedByName = asList(new Employee(1L, "John"), new Employee(2L, "Kevin"), new Employee(3L, "Mike")); + employeesNotSortedByName = asList(new Employee(1L, "Kevin"), new Employee(2L, "John"), new Employee(3L, "Mike")); + } + + @Test + public void givenSortedList_whenUsingIterativeApproach_thenReturnTrue() { + assertThat(checkIfSortedUsingIterativeApproach(sortedListOfString)).isTrue(); + } + + @Test + public void givenSingleElementList_whenUsingIterativeApproach_thenReturnTrue() { + assertThat(checkIfSortedUsingIterativeApproach(singletonList)).isTrue(); + } + + @Test + public void givenUnsortedList_whenUsingIterativeApproach_thenReturnFalse() { + assertThat(checkIfSortedUsingIterativeApproach(unsortedListOfString)).isFalse(); + } + + @Test + public void givenSortedListOfEmployees_whenUsingIterativeApproach_thenReturnTrue() { + assertThat(checkIfSortedUsingIterativeApproach(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue(); + } + + @Test + public void givenUnsortedListOfEmployees_whenUsingIterativeApproach_thenReturnFalse() { + assertThat(checkIfSortedUsingIterativeApproach(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse(); + } + + @Test + public void givenSortedList_whenUsingRecursion_thenReturnTrue() { + assertThat(checkIfSortedUsingRecursion(sortedListOfString)).isTrue(); + } + + @Test + public void givenSingleElementList_whenUsingRecursion_thenReturnTrue() { + assertThat(checkIfSortedUsingRecursion(singletonList)).isTrue(); + } + + @Test + public void givenUnsortedList_whenUsingRecursion_thenReturnFalse() { + assertThat(checkIfSortedUsingRecursion(unsortedListOfString)).isFalse(); + } + + @Test + public void givenSortedList_whenUsingGuavaOrdering_thenReturnTrue() { + assertThat(checkIfSortedUsingOrderingClass(sortedListOfString)).isTrue(); + } + + @Test + public void givenUnsortedList_whenUsingGuavaOrdering_thenReturnFalse() { + assertThat(checkIfSortedUsingOrderingClass(unsortedListOfString)).isFalse(); + } + + @Test + public void givenSortedListOfEmployees_whenUsingGuavaOrdering_thenReturnTrue() { + assertThat(checkIfSortedUsingOrderingClass(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue(); + } + + @Test + public void givenUnsortedListOfEmployees_whenUsingGuavaOrdering_thenReturnFalse() { + assertThat(checkIfSortedUsingOrderingClass(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse(); + } + + @Test + public void givenSortedList_whenUsingGuavaComparators_thenReturnTrue() { + assertThat(checkIfSortedUsingComparators(sortedListOfString)).isTrue(); + } + + @Test + public void givenUnsortedList_whenUsingGuavaComparators_thenReturnFalse() { + assertThat(checkIfSortedUsingComparators(unsortedListOfString)).isFalse(); + } + +}