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

task5 + questions

parent 70c22aa6
No related branches found
No related tags found
No related merge requests found
package fr.univamu.sorting;
import java.util.List;
public class Sort {
public Sortable sortable;
public List<Integer> IntList;
public int[] IntTab;
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);
}
}
}
}
}
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);
}
}
package fr.univamu.sorting;
public class SortableIntArray {
public int[] IntTab;
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;
}
}
...@@ -2,7 +2,7 @@ package fr.univamu.sorting; ...@@ -2,7 +2,7 @@ package fr.univamu.sorting;
import java.util.*; import java.util.*;
public class SortableIntList { public class SortableIntList {
public ArrayList<Integer> IntList; public List<Integer> IntList;
public void swap(int index1, int index2) { public void swap(int index1, int index2) {
int value1 = IntList.get(index1); int value1 = IntList.get(index1);
...@@ -13,7 +13,6 @@ public class SortableIntList { ...@@ -13,7 +13,6 @@ public class SortableIntList {
public int compare(int index1, int index2) { public int compare(int index1, int index2) {
return IntList.get(index1).compareTo(IntList.get(index2)); return IntList.get(index1).compareTo(IntList.get(index2));
} }
public int size() { public int size() {
...@@ -23,12 +22,11 @@ public class SortableIntList { ...@@ -23,12 +22,11 @@ public class SortableIntList {
public int get(int index) { public int get(int index) {
return IntList.get(index); return IntList.get(index);
} }
public void sort() { public void sort() {
for (int i = 0; i < IntList.size() - 1; i++) { for (int i = 0; i < IntList.size() - 1; i++) {
for (int j = i + 1; j < IntList.size(); j++) { for (int j = i + 1; j < IntList.size(); j++) {
if (compare(IntList.get(j), IntList.get(i)) < 0) { if (compare(j,i) < 0) {
swap(IntList.get(i), IntList.get(j)); swap(i,j);
} }
} }
} }
......
...@@ -12,26 +12,7 @@ class ListSortTest { ...@@ -12,26 +12,7 @@ class ListSortTest {
@Test @Test
void testSort() { void testSort() {
ArrayList<Integer> TestList = new ArrayList<Integer>(8); assertThat(shuffledRange(1,8).sort()).isEqualTo(range(1,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);
} }
} }
\ 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