diff --git a/src/main/java/fr/univamu/sorting/Sort.java b/src/main/java/fr/univamu/sorting/Sort.java index 8d4e21cba6c24c8f42a41ecf4f0763c1d5b6c3e6..31d5f27641974a29791c6f316b86387f23a9f42f 100644 --- a/src/main/java/fr/univamu/sorting/Sort.java +++ b/src/main/java/fr/univamu/sorting/Sort.java @@ -3,19 +3,23 @@ package fr.univamu.sorting; import java.util.List; public class Sort { - public Sortable sortable; - public List<Integer> IntList; - public int[] IntTab; + public Sortable values; + + public Sort (Sortable values){ + this.values=values; + } public void sort() { - for (int i = 0; i < this.size() - 1; i++) { - for (int j = i + 1; j < this.size(); j++) { - if (compare(j,i) < 0) { - swap(i,j); + for (int i = 0; i < values.size() - 1; i++) { + int mini = i; + for (int j = i + 1; j < values.size(); j++) { + if (values.compare(j,mini) < 0) { + mini = j; } } + values.swap(mini,i); } } } diff --git a/src/main/java/fr/univamu/sorting/Sortable.java b/src/main/java/fr/univamu/sorting/Sortable.java index 8f0b574b46ce27a8239c011ae81454a06b23741e..9f132951bda0cf2ddf5f33c718a3c0be85b3ede1 100644 --- a/src/main/java/fr/univamu/sorting/Sortable.java +++ b/src/main/java/fr/univamu/sorting/Sortable.java @@ -1,37 +1,7 @@ package fr.univamu.sorting; public interface Sortable { - public void swap(int index1, int index2) { - int value1 = IntTab[index1]; - int value2 = IntTab[index2]; - IntTab[index1] = value2; - IntTab[index2] = value1; - - } - - public int compare(int index1, int index2) { - return IntTab[index1] - (IntTab[index2]; - } - - public int size() { - return IntTab.length; - } - 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) { - return IntList.get(index1).compareTo(IntList.get(index2)); - } - - public int size() { - return IntList.size(); - } - - public int get(int index) { - return IntList.get(index); - } + public int size(); + public void swap(int index1, int index2); + public int compare(int index1, int index2); } diff --git a/src/main/java/fr/univamu/sorting/SortableIntArray.java b/src/main/java/fr/univamu/sorting/SortableIntArray.java index aaa80ba094ba32aad61c15a7346f4c4a49ed7254..289169b156539d79da82bb9bc2812aa75d144890 100644 --- a/src/main/java/fr/univamu/sorting/SortableIntArray.java +++ b/src/main/java/fr/univamu/sorting/SortableIntArray.java @@ -1,19 +1,23 @@ package fr.univamu.sorting; -public class SortableIntArray { - public int[] IntTab; +public class SortableIntArray implements Sortable{ + private final int[] values; + + public SortableIntArray (int[] values){ + this.values= values; + } public void swap(int index1, int index2) { - int value1 = IntTab[index1]; - int value2 = IntTab[index2]; - IntTab[index1] = value2; - IntTab[index2] = value1; + int value1 = values[index1]; + int value2 = values[index2]; + values[index1] = value2; + values[index2] = value1; } public int compare(int index1, int index2) { - return IntTab[index1] - (IntTab[index2]; + return values[index1] - (values[index2]); } public int size() { - return IntTab.length; + return values.length; } } diff --git a/src/main/java/fr/univamu/sorting/SortableIntList.java b/src/main/java/fr/univamu/sorting/SortableIntList.java index 042c4d598e8ef5fdf344762d3a8c27720dfc2a3a..13d8058d19aa738c3839a79cc3996bb726ff0bc6 100644 --- a/src/main/java/fr/univamu/sorting/SortableIntList.java +++ b/src/main/java/fr/univamu/sorting/SortableIntList.java @@ -1,34 +1,30 @@ package fr.univamu.sorting; import java.util.*; -public class SortableIntList { - public List<Integer> IntList; +public class SortableIntList implements Sortable{ + private final List<Integer> values; + + public SortableIntList(List<Integer> values) { + this.values = values; + } 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); + int value1 = values.get(index1); + int value2 = values.get(index2); + values.set(index1, value2); + values.set(index2, value1); } public int compare(int index1, int index2) { - return IntList.get(index1).compareTo(IntList.get(index2)); + return values.get(index1).compareTo(values.get(index2)); } public int size() { - return IntList.size(); + return values.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(j,i) < 0) { - swap(i,j); - } - } - } + return values.get(index); } + } \ 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 76a1d00fca8f82d96ec0bb28b7c0fa07c82afcfb..927327bb97c7aaf710c90e1d91b75b7e564bab77 100644 --- a/src/test/java/fr/univamu/sorting/ListSortTest.java +++ b/src/test/java/fr/univamu/sorting/ListSortTest.java @@ -12,7 +12,14 @@ class ListSortTest { @Test void testSort() { - assertThat(shuffledRange(1,8).sort()).isEqualTo(range(1,8)); + + List<Integer> values = IntLists.shuffledRange(1,9); + Sortable sortableValues = new SortableIntList(values); + Sort sorter = new Sort(sortableValues); + sorter.sort(); + assertThat(values).isEqualTo(range(1,9)); + } + } \ No newline at end of file