Sorting Test + Impl

This commit is contained in:
Rochas
2025-12-19 20:33:44 +01:00
parent b74c4e04c1
commit cd79f547c7
4 changed files with 268 additions and 6 deletions

View File

@@ -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> T[] bubblesort(T[] array, Comparator<T> comparator) { return null; }
public static <T> void swap(T[] array, int i, int j){
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
// BUBBLESORT ///////////////////////////////////////////////////////////////////////
public static <T> T[] bubblesort(T[] array, Comparator<T> comparator) {
int n = array.length;
T[] sortedArray=array.clone();
boolean swapped = true;
while(swapped){
swapped = false;
for(int i = 0; i<n-1; i++){
if(comparator.compare(sortedArray[i], sortedArray[i+1])>0){
swap(sortedArray, i, i+1);
swapped = true;
}
}
n=n-1;
}
for(int i = 0; i<sortedArray.length; i++){
//System.out.print( sortedArray[i] + " ");
}
//System.out.println("");
return sortedArray;
}
// QUICKSORT ///////////////////////////////////////////////////////////////////////
public static <T> T[] quicksort(T[] array, Comparator<T> comparator) {
T[] sortedArray=array.clone();
quicksortBis(sortedArray, comparator, 0, sortedArray.length-1);
return sortedArray;
}
private static <T> void quicksortBis(T[] array, Comparator<T> comparator, int first, int last){
if(first<last){
int pivot = (last-first)/2;
pivot = partition(array,comparator,first,last,pivot);
quicksortBis(array, comparator,first, pivot-1);
quicksortBis(array, comparator,pivot+1, last);
}
}
private static <T> int partition(T[] array, Comparator<T> comparator, int first, int last,int pivot){
swap(array, first, last);
int j = first;
for(int i = first; i<last; i++){
if(comparator.compare(array[i], array[last])<=0){
swap(array, i, j);
j++;
}
}
swap(array, last, j);
return j;
}
// MERGESORT ///////////////////////////////////////////////////////////////////////
public static <T> T[] mergesort(T[] array, Comparator<T> comparator) {
List<T> list = new ArrayList<T>();
for(int i = 0; i<array.length; i++){
list.add(array[i]);
}
list = mergesortBis(list,comparator);
T[] sortedArray=array.clone();
for(int i = 0; i<array.length; i++){
sortedArray[i] = list.get(i);
}
return sortedArray;
}
public static <T> List<T> mergesortBis(List<T> list, Comparator<T> comparator) {
List<T> res = list;
if(list.size()>1){
List<T> list1 = new ArrayList<T>(list.subList(0,list.size()/2));
List<T> list2 = new ArrayList<T>(list.subList(list.size()/2,list.size()));
res = merge(mergesortBis(list1,comparator),mergesortBis(list2,comparator),comparator);
}
return res;
}
public static <T> List<T> merge(List<T> list1, List<T> list2, Comparator<T> comparator){
List<T> res = new ArrayList<T>();
int i = 0;
int j = 0;
while(i<list1.size() || j<list2.size()){
if(j>=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> T[] quicksort(T[] array, Comparator<T> comparator) { return null; }
public static <T> T[] mergesort(T[] array, Comparator<T> comparator) { return null; }
}

View File

@@ -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 <T> boolean isSorted(T[] array, Comparator<T> comparator){
boolean isSorted = true;
for(int i = 0; i<array.length-1 && isSorted; i++){
isSorted = isSorted && comparator.compare(array[i],array[i+1])<=0;
}
return isSorted;
}
public static <T> Map<T,Integer> countNbElement(T[] array){
Map<T,Integer> map = new HashMap<T,Integer>();
for(int i = 0; i<array.length; i++){
if(map.containsKey(array[i])){
int newVal = map.get(array[i])+1;
map.put(array[i],newVal);
}
else{
map.put(array[i],1);
}
}
return map;
}
public static <T> boolean haveSameElements(T[] array1,T[] array2){
boolean isSorted = array1.length==array2.length;
if(isSorted){
Map<T,Integer> elements1 = countNbElement(array1);
Map<T,Integer> 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 <T> void displayerCompare(T[] array1, T[] array2){
for(int i = 0; i<array1.length; i++){
System.out.print(array1[i] + " ");
}
System.out.println("");
for(int i = 0; i<array2.length; i++){
System.out.print(array2[i] + " ");
}
System.out.println("\n");
}
static Comparator<Integer> comparatorInt = (a, b) -> a-b; //-1 0 1 2 3
static Comparator<String> 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;
}
}