diff --git a/code/sorting/src/main/java/fr/istic/vv/Sorting.java b/code/sorting/src/main/java/fr/istic/vv/Sorting.java index 05a525c..343fad4 100644 --- a/code/sorting/src/main/java/fr/istic/vv/Sorting.java +++ b/code/sorting/src/main/java/fr/istic/vv/Sorting.java @@ -1,12 +1,129 @@ package fr.istic.vv; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; public class Sorting { - public static T[] bubblesort(T[] array, Comparator comparator) { return null; } + public static void swap(T[] array, int i, int j){ + T temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + + // BUBBLESORT /////////////////////////////////////////////////////////////////////// + + public static T[] bubblesort(T[] array, Comparator comparator) { + int n = array.length; + T[] sortedArray=array.clone(); + boolean swapped = true; + while(swapped){ + swapped = false; + for(int i = 0; i0){ + swap(sortedArray, i, i+1); + swapped = true; + } + } + n=n-1; + } + for(int i = 0; i T[] quicksort(T[] array, Comparator comparator) { + T[] sortedArray=array.clone(); + + quicksortBis(sortedArray, comparator, 0, sortedArray.length-1); + + return sortedArray; + } + + private static void quicksortBis(T[] array, Comparator comparator, int first, int last){ + if(first int partition(T[] array, Comparator comparator, int first, int last,int pivot){ + swap(array, first, last); + int j = first; + for(int i = first; i T[] mergesort(T[] array, Comparator comparator) { + List list = new ArrayList(); + + for(int i = 0; i List mergesortBis(List list, Comparator comparator) { + List res = list; + + if(list.size()>1){ + List list1 = new ArrayList(list.subList(0,list.size()/2)); + List list2 = new ArrayList(list.subList(list.size()/2,list.size())); + + res = merge(mergesortBis(list1,comparator),mergesortBis(list2,comparator),comparator); + } + + return res; + } + + public static List merge(List list1, List list2, Comparator comparator){ + List res = new ArrayList(); + int i = 0; + int j = 0; + while(i=list2.size()) { + res.add(list1.get(i)); + i++; + } + else if(i>=list1.size()) { + res.add(list2.get(j)); + j++; + } + else if(comparator.compare(list1.get(i),list2.get(j))<=0){ + res.add(list1.get(i)); + i++; + } + else{ + res.add(list2.get(j)); + j++; + } + } + return res; + } + - public static T[] quicksort(T[] array, Comparator comparator) { return null; } - public static T[] mergesort(T[] array, Comparator comparator) { return null; } } diff --git a/code/sorting/src/test/java/fr/istic/vv/SortingTest.java b/code/sorting/src/test/java/fr/istic/vv/SortingTest.java index c276c96..80d6c96 100644 --- a/code/sorting/src/test/java/fr/istic/vv/SortingTest.java +++ b/code/sorting/src/test/java/fr/istic/vv/SortingTest.java @@ -1,9 +1,154 @@ package fr.istic.vv; import net.jqwik.api.*; +import static fr.istic.vv.Sorting.*; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; public class SortingTest { - @Property - boolean absoluteValueOfAllNumbersIsPositive(@ForAll int anInteger) { - return Math.abs(anInteger) >= 0; + + public static boolean isSorted(T[] array, Comparator comparator){ + boolean isSorted = true; + for(int i = 0; i Map countNbElement(T[] array){ + Map map = new HashMap(); + for(int i = 0; i boolean haveSameElements(T[] array1,T[] array2){ + boolean isSorted = array1.length==array2.length; + if(isSorted){ + Map elements1 = countNbElement(array1); + Map elements2 = countNbElement(array2); + for (T key : elements1.keySet()) { + isSorted = isSorted && elements2.containsKey(key) && elements1.get(key) == elements2.get(key); + if(!isSorted) break; + } + } + return isSorted; + } + + public static void displayerCompare(T[] array1, T[] array2){ + for(int i = 0; i comparatorInt = (a, b) -> a-b; //-1 0 1 2 3 + static Comparator comparatorStr = (a, b) -> a.compareTo(b); //a ab ac b c d + + + + // BUBBLESORT /////////////////////////////////////////////////////////////////////// + + @Property + void arrayIsSortedBubble(@ForAll Integer[] array) { + Integer[] sortedArray = bubblesort(array, comparatorInt); + + boolean res = isSorted(sortedArray,comparatorInt); + + if(!res){ + System.out.println("\nisSorted Bubble Fail :\n"); + displayerCompare(array,sortedArray); + } + + assert res; + } + + @Property + void sameElementsBubble(@ForAll Integer[] array) { + Integer[] sortedArray = bubblesort(array, comparatorInt); + + boolean res = haveSameElements(array,sortedArray); + + if(!res){ + System.out.println("\nisSame Bubble Fail:\n"); + displayerCompare(array,sortedArray); + } + + assert res; + } + + + // QUICKSORT /////////////////////////////////////////////////////////////////////// + + @Property + void arrayIsSortedQuick(@ForAll Integer[] array) { + Integer[] sortedArray = quicksort(array, comparatorInt); + + boolean res = isSorted(sortedArray,comparatorInt); + + if(!res){ + System.out.println("\nisSorted Quick Fail :\n"); + displayerCompare(array,sortedArray); + } + + assert res; + } + + @Property + void sameElementsQuick(@ForAll Integer[] array) { + Integer[] sortedArray = quicksort(array, comparatorInt); + + boolean res = haveSameElements(array,sortedArray); + + if(!res){ + System.out.println("\nisSame Quick Fail:\n"); + displayerCompare(array,sortedArray); + } + + assert res; + } + + // MERGESORT /////////////////////////////////////////////////////////////////////// + + @Property + void arrayIsSortedMerge(@ForAll Integer[] array) { + Integer[] sortedArray = mergesort(array, comparatorInt); + + boolean res = isSorted(sortedArray,comparatorInt); + + if(!res){ + System.out.println("\nisSorted Merge Fail :\n"); + displayerCompare(array,sortedArray); + } + + assert res; + } + + @Property + void sameElementsMerge(@ForAll Integer[] array) { + Integer[] sortedArray = mergesort(array, comparatorInt); + + boolean res = haveSameElements(array,sortedArray); + + if(!res){ + System.out.println("\nisSame Merge Fail:\n"); + displayerCompare(array,sortedArray); + } + + assert res; + } + } \ No newline at end of file diff --git a/code/sorting/target/classes/fr/istic/vv/Sorting.class b/code/sorting/target/classes/fr/istic/vv/Sorting.class index beb6ca4..66ecef9 100644 Binary files a/code/sorting/target/classes/fr/istic/vv/Sorting.class and b/code/sorting/target/classes/fr/istic/vv/Sorting.class differ diff --git a/code/sorting/target/test-classes/fr/istic/vv/SortingTest.class b/code/sorting/target/test-classes/fr/istic/vv/SortingTest.class index aaa1fa5..94bc923 100644 Binary files a/code/sorting/target/test-classes/fr/istic/vv/SortingTest.class and b/code/sorting/target/test-classes/fr/istic/vv/SortingTest.class differ