Skip to content
Snippets Groups Projects
Commit d6111aa0 authored by ADAMOU Salim's avatar ADAMOU Salim
Browse files

correction

parent 67d1269d
No related branches found
No related tags found
No related merge requests found
package fr.univamu.sorting;
public class InsertionSort<T extends Comparable<T>> implements Sorter<T> {
private Sortable<T> sortable;
public InsertionSort(Sortable<T> sortable) {
this.sortable = sortable;
}
@Override
public void sort() {
insertionSort(sortable);
}
private void insertionSort(Sortable<T> sortable){
int n = sortable.size();
for (int i = 1; i < n; ++i) {
T key = sortable.get(i);
int j = i - 1;
while (j >= 0 && sortable.compare(j, key) > 0) {
sortable.Swap(j + 1, j);
j = j - 1;
}
sortable.set(j + 1, key);
}
}
}
\ No newline at end of file
package fr.univamu.sorting; package fr.univamu.sorting;
public class Sort { public class Sort<T extends Comparable<T>> {
private Sortable<T> sortable;
public Sort(Sortable<T> sortable){
this.sortable = sortable;
}
public void sort(){ public void sort(){
bubbleSort(); bubbleSort(sortable);
} }
public void bubbleSort(){ public void bubbleSort(Sortable<T> sortable){
for( int i=0; i< data.size()-1; i++){ for( int i=0; i< sortable.size()-1; i++){
for(int j=0 ; j< data.size()-1-i; j++){ for(int j=0 ; j< sortable.size()-1-i; j++){
if(compare(j,j+1)>0){ if(sortable.compare(j,j+1)>0){
Swap(j,j+1); sortable.Swap(j,j+1);
} }
} }
} }
......
package fr.univamu.sorting;
import java.util.List;
public interface Sortable<T extends Comparable<T>> {
void Swap(int index1, int index2);
int compare(int index1, int index2);
int size();
T get(int index);
void set(int index, T value);
}
package fr.univamu.sorting; package fr.univamu.sorting;
import java.util.List; import java.util.List;
public class SortableIntArray<T extends Comparable> implements Sortable<T> {
public class SortableIntArray { private T[]data;
private int[]data; public SortableIntArray(T [] data){
public SortableIntArray(int [] data){
this.data = data; this.data = data;
} }
public void Swap (int index1, int index2){ public void Swap (int index1, int index2){
Integer x = data[index1]; T x = data[index1];
data[index1] = data[index2]; data[index1] = data[index2];
data[index2]= x; data[index2]= x;
} }
public int compare(int index1, int index2){ public int compare(int index1, int index2){
return data[index1]- data[index2]; return data[index1].compareTo(data[index2]);
} }
public int size() { public int size() {
return data.length; return data.length;
} }
@Override
public T get(int index) {
return data[index];
}
} }
...@@ -2,27 +2,31 @@ package fr.univamu.sorting; ...@@ -2,27 +2,31 @@ package fr.univamu.sorting;
import java.util.List; import java.util.List;
public class SortableIntList { public class SortableIntList<T extends Comparable<T>> implements Sortable<T> {
private List<Integer> data; private List<T> data;
public SortableIntList(List<Integer> data){ public SortableIntList(List<T> data){
this.data = data; this.data = data;
} }
public void Swap (int index1, int index2){ public void Swap (int index1, int index2){
Integer x = data.get(index1); T x = data.get(index1);
data.set(index1, data.get(index2)); data.set(index1, data.get(index2));
data.set(index2, x); data.set(index2, x);
} }
public int compare(int index1, int index2){ public int compare(int index1, int index2){
return data.get(index1)- data.get(index2); return data.get(index1).compareTo(data.get(index2));
} }
public int size(){ public int size(){
return data.size(); return data.size();
} }
public int get(int index){ public T get(int index){
return data.get(index); return (T) data.get(index);
}
public void set(int index, T value){
data.set(index, value);
} }
public void sort(){ public void sort(){
......
package fr.univamu.sorting;
public interface Sorter<T extends Comparable<T>> {
void sort();
}
...@@ -7,12 +7,16 @@ import static fr.univamu.sorting.IntLists.*; ...@@ -7,12 +7,16 @@ import static fr.univamu.sorting.IntLists.*;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
class ListSortTest { class ListSortTest {
List<Integer> values = IntLists.shuffledRange(1,100);
Sortable <Integer> sortableValues = new SortableIntList<>(values);
Sort <Integer> sorter = new Sort<>(sortableValues);
@Test @Test
void testSort() { void testSort() {
sorter.sort();
assertThat(values).isEqualTo(IntLists.range(1,100));
} }
} }
\ 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