Skip to content
Snippets Groups Projects
Commit f8282198 authored by t19015527's avatar t19015527
Browse files

mon TP après la scéance

parent bc6aaf72
No related branches found
No related tags found
No related merge requests found
package fr.univamu.sorting;
import java.lang.reflect.Array;
import java.util.List;
public interface Sortable {
int get(int index);
public interface Sortable<T extends Comparable<T>> {
T get(int index);
int size();
void swap(int index1, int index2);
int compare(int index1, int index2);
static <T extends Comparable<T>> Sortable of(List<T> A) {
return new SortableIntList(A);
}
static <T extends Comparable<T>> Sortable of(T[] A) {
return new SortableIntArray(A);
}
}
package fr.univamu.sorting;
public class SortableIntArray implements Sortable {
private int[] Array;
public class SortableIntArray<T extends Comparable<T>> implements Sortable {
private T[] Array;
public SortableIntArray(int [] Array){
public SortableIntArray(T[] Array){
this.Array=Array;
}
public int[] GetArray(){
public T[] GetArray(){
return this.Array;
}
public int size(){
return this.Array.length;
}
public int get(int index){
public T get(int index){
return this.Array[index];
}
public void swap(int index1, int index2){
int a = this.Array[index1];
int b = this.Array[index2];
T a = this.Array[index1];
T b = this.Array[index2];
this.Array[index1] = b;
this.Array[index2] = a;
}
public int compare(int index1, int index2){
return this.Array[index1]-this.Array[index2];
return this.Array[index1].compareTo(this.Array[index1]);
}
}
package fr.univamu.sorting;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class SortableIntList implements Sortable {
private List<Integer> list;
public class SortableIntList<T extends Comparable<T>> implements Sortable {
private List<T> list;
public SortableIntList(List<Integer> list){
public SortableIntList(List<T> list){
this.list = list;
}
public List<Integer> GetList(){
public List<T> GetList(){
return this.list;
}
public int get(int index){
public T get(int index){
return this.list.get(index);
}
......@@ -25,13 +22,13 @@ public class SortableIntList implements Sortable {
}
public void swap(int index1, int index2){
int a = this.list.get(index1);
int b = this.list.get(index2);
T a = this.list.get(index1);
T b = this.list.get(index2);
this.list.add(index2,a);
this.list.add(index1,b);
}
public int compare(int index1, int index2){
return this.list.get(index1)-this.list.get(index2);
return this.list.get(index1).compareTo(this.list.get(index2));
}
}
package fr.univamu.sorting;
public interface Sorter<T extends Comparable<T>> {
void sort();
}
package fr.univamu.sorting;
public class insertionSort {
public class insertionSort<T extends Comparable<T>> implements Sorter {
private Sortable Mustsort;
public insertionSort(Sortable Mustsort){
......@@ -12,9 +12,16 @@ public class insertionSort {
}
public void sort() {
int i = 0;
while (i<Mustsort.size()-1){
int x = Mustsort.get(i);
while (i<this.Mustsort.size()-1){
T x = (T) this.Mustsort.get(i);
int j = i;
while (j>0){
while (x.compareTo((T)this.Mustsort.get(j-this.Mustsort.size()))<0){
this.Mustsort.swap(j,j-this.Mustsort.size());
j=j-this.Mustsort.size();
}
}
this.Mustsort.swap(j,i);
}
}
}
......@@ -2,7 +2,7 @@ package fr.univamu.sorting;
import java.util.List;
public class quicksortSort {
public class quicksortSort<T extends Comparable<T>> implements Sorter {
private Sortable Mustsort;
public quicksortSort(Sortable Mustsort){
......@@ -13,28 +13,28 @@ public class quicksortSort {
return this.Mustsort;
}
private List<List<Integer>> split(List<Integer> Tosplit){
int a = Tosplit.get(Tosplit.size()-1);
List<Integer> A = List.of();
List<Integer> B = List.of();
List<Integer> C = List.of();
for(int b : Tosplit){
if (a>b){
private List<List<T>> split(List<T> Tosplit){
T a = Tosplit.get(Tosplit.size()-1);
List<T> A = List.of();
List<T> B = List.of();
List<T> C = List.of();
for(T b : Tosplit){
if (a.compareTo(b)>0){
A.add(b);
}
if (a==b){
if (a.compareTo(b)==0){
C.add(b);
}
if (a<b){
if (a.compareTo(b)<0){
B.add(b);
}
}
return List.of(A,C,B);
}
private boolean HasRightSize(List<List<Integer>> Tocheck){
private boolean HasRightSize(List<List<T>> Tocheck){
boolean Truth = true;
for (List<Integer> L : Tocheck){
for (List<T> L : Tocheck){
if (L.size()>1){
Truth = false;
}
......@@ -42,17 +42,17 @@ public class quicksortSort {
return Truth;
}
private List<Integer> Tolist(Sortable S){
private List<T> Tolist(Sortable S){
int A = 0;
List<Integer> list = List.of();
List<T> list = List.of();
while (A<S.size()){
list.add(S.get(A));
list.add((T) S.get(A));
A=A+1;
}
return list;
}
private void Change(List<Integer> list){
private void Change(List<T> list){
int A = 0;
while (A<list.size()){
int B = 0;
......@@ -66,19 +66,19 @@ public class quicksortSort {
}
}
public void sort() {
List<Integer> Tosort = Tolist(this.Mustsort);
List<List<Integer>> Part = List.of(Tosort);
List<T> Tosort = Tolist(this.Mustsort);
List<List<T>> Part = List.of(Tosort);
boolean Issort = false;
while (Issort == false){
List<List<Integer>> Provpart = List.of();
for (List<Integer> L : Part){
List<List<T>> Provpart = List.of();
for (List<T> L : Part){
Provpart.addAll(split(L));
}
Part=Provpart;
Issort=HasRightSize(Part);
}
List<Integer> sort=List.of();
for (List<Integer> L : Part){
List<T> sort=List.of();
for (List<T> L : Part){
sort.addAll(L);
}
Change(sort);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment