Skip to content
Snippets Groups Projects
Commit 96099570 authored by FOURNEL Alexandre's avatar FOURNEL Alexandre
Browse files

question 7

parent 7e31b684
Branches
No related tags found
No related merge requests found
...@@ -3,19 +3,23 @@ package fr.univamu.sorting; ...@@ -3,19 +3,23 @@ package fr.univamu.sorting;
import java.util.List; import java.util.List;
public class Sort { public class Sort {
public Sortable sortable;
public List<Integer> IntList; public Sortable values;
public int[] IntTab;
public Sort (Sortable values){
this.values=values;
}
public void sort() { public void sort() {
for (int i = 0; i < this.size() - 1; i++) { for (int i = 0; i < values.size() - 1; i++) {
for (int j = i + 1; j < this.size(); j++) { int mini = i;
if (compare(j,i) < 0) { for (int j = i + 1; j < values.size(); j++) {
swap(i,j); if (values.compare(j,mini) < 0) {
mini = j;
} }
} }
values.swap(mini,i);
} }
} }
} }
......
package fr.univamu.sorting; package fr.univamu.sorting;
public interface Sortable { public interface Sortable {
public void swap(int index1, int index2) { public int size();
int value1 = IntTab[index1]; public void swap(int index1, int index2);
int value2 = IntTab[index2]; public int compare(int index1, int 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);
}
} }
package fr.univamu.sorting; package fr.univamu.sorting;
public class SortableIntArray { public class SortableIntArray implements Sortable{
public int[] IntTab; private final int[] values;
public SortableIntArray (int[] values){
this.values= values;
}
public void swap(int index1, int index2) { public void swap(int index1, int index2) {
int value1 = IntTab[index1]; int value1 = values[index1];
int value2 = IntTab[index2]; int value2 = values[index2];
IntTab[index1] = value2; values[index1] = value2;
IntTab[index2] = value1; values[index2] = value1;
} }
public int compare(int index1, int index2) { public int compare(int index1, int index2) {
return IntTab[index1] - (IntTab[index2]; return values[index1] - (values[index2]);
} }
public int size() { public int size() {
return IntTab.length; return values.length;
} }
} }
package fr.univamu.sorting; package fr.univamu.sorting;
import java.util.*; import java.util.*;
public class SortableIntList { public class SortableIntList implements Sortable{
public List<Integer> IntList; private final List<Integer> values;
public SortableIntList(List<Integer> values) {
this.values = values;
}
public void swap(int index1, int index2) { public void swap(int index1, int index2) {
int value1 = IntList.get(index1); int value1 = values.get(index1);
int value2 = IntList.get(index2); int value2 = values.get(index2);
IntList.set(index1, value1); values.set(index1, value2);
IntList.set(index2, value2); values.set(index2, value1);
} }
public int compare(int index1, int index2) { 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() { public int size() {
return IntList.size(); return values.size();
} }
public int get(int index) { public int get(int index) {
return IntList.get(index); return values.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);
}
}
}
} }
} }
\ No newline at end of file
...@@ -12,7 +12,14 @@ class ListSortTest { ...@@ -12,7 +12,14 @@ class ListSortTest {
@Test @Test
void testSort() { 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment