diff --git a/src/main/java/fr/univamu/sorting/SortableIntList.java b/src/main/java/fr/univamu/sorting/SortableIntList.java index c709b4aa8c732acd7c5f5a8dd60ca3e6d95bc9cd..65cc74cea2c0d8517ae3be9f1c8db94003643362 100644 --- a/src/main/java/fr/univamu/sorting/SortableIntList.java +++ b/src/main/java/fr/univamu/sorting/SortableIntList.java @@ -2,43 +2,35 @@ package fr.univamu.sorting; import java.util.*; public class SortableIntList { - public ArrayList<Integer> IntList; + public ArrayList<Integer> IntList; - public void swap(int index1, int index2){ - int value1 = IntList.get(index1); - int value2 = IntList.get(index2); - IntList.set(index1, value1); - IntList.set(index2, value2); - } + public void swap(int index1, int index2) { + int value1 = IntList.get(index1); + int value2 = IntList.get(index2); + IntList.set(index1, value1); + IntList.set(index2, value2); + } - public int compare(int index1, int index2){ - if (IntList.get(index1) > IntList.get(index2)){ - return 1; - } - else if (IntList.get(index1) < IntList.get(index2)) { - return -1; - } - else { - return 0; - } + public int compare(int index1, int index2) { + return IntList.get(index1).compareTo(IntList.get(index2)); - } + } - public int size(){ - return IntList.size(); - } + public int size() { + return IntList.size(); + } - public int get(int index){ - return IntList.get(index); - } - public void sort(){ - for (int i = 0; i < IntList.size() - 1; i++) - { - for (int j = i + 1; j < IntList.size(); j++) { - if (compare(IntList.get(j),IntList.get(i)) < 0){ - swap(IntList.get(i),IntList.get(j)); - } + public int get(int index) { + return IntList.get(index); + } - } - } -} + public void sort() { + for (int i = 0; i < IntList.size() - 1; i++) { + for (int j = i + 1; j < IntList.size(); j++) { + if (compare(IntList.get(j), IntList.get(i)) < 0) { + swap(IntList.get(i), IntList.get(j)); + } + } + } + } +} \ No newline at end of file diff --git a/src/test/java/fr/univamu/sorting/ListSortTest.java b/src/test/java/fr/univamu/sorting/ListSortTest.java index 921553e59bc69f1b54a25df3977e3c22f8927563..d25a4eff80a1a3842b639fc90b3978afe050da9a 100644 --- a/src/test/java/fr/univamu/sorting/ListSortTest.java +++ b/src/test/java/fr/univamu/sorting/ListSortTest.java @@ -2,7 +2,7 @@ package fr.univamu.sorting; import org.junit.jupiter.api.Test; -import java.util.List; +import java.util.*; import static fr.univamu.sorting.IntLists.*; import static org.assertj.core.api.Assertions.assertThat; @@ -12,6 +12,25 @@ class ListSortTest { @Test void testSort() { + ArrayList<Integer> TestList = new ArrayList<Integer>(8); + TestList.add(1); + TestList.add(8); + TestList.add(9); + TestList.add(5); + TestList.add(4); + TestList.add(3); + TestList.add(2); + TestList.add(4); + ArrayList<Integer> TestList2 = new ArrayList<Integer>(8); + TestList.add(1); + TestList.add(2); + TestList.add(3); + TestList.add(4); + TestList.add(4); + TestList.add(5); + TestList.add(8); + TestList.add(9); + assertThat(TestList.sort()).isEqualTo(TestList2); }