first part lab 12

This commit is contained in:
Rémi Heredero 2022-03-14 15:07:48 +01:00
parent 0cb43a08c5
commit 3a28323d25
12 changed files with 286 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
bin/tools/Chrono.class Normal file

Binary file not shown.

View File

@ -0,0 +1,34 @@
package lab12_sort;
import java.util.Random;
public abstract class ArrayFactory {
static int[] createRandomArray(int size, int maxValue){
int[] array = new int[size];
Random r = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = r.nextInt(maxValue);
}
return array;
}
static int[] createInvertedSortedArray(int size){
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = size-i-1;
}
return array;
}
static int[] createShuffleArray(int size){
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
if(i%2 == 0){
array[i] = i/2;
} else {
array[i] = (size-1)-(i/2);
}
}
return array;
}
}

View File

@ -0,0 +1,19 @@
package lab12_sort;
public abstract class BubbleSort {
static public int[] sort(int[] array){
boolean shorted = false;
while (shorted == false) {
shorted = true;
for (int i = 0; i < array.length-1; i++) {
if (array[i] > array[i+1]) {
int tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
shorted = false;
}
}
}
return array;
}
}

View File

@ -0,0 +1,68 @@
package lab12_sort;
public abstract class FusionSort {
private static void fusion(int tab[], int debut, int milieu, int fin) {
int n1 = milieu - debut + 1;
int n2 = fin - milieu;
int G[] = new int[n1];
int D[] = new int[n2];
for (int i = 0; i < n1; i++)
G[i] = tab[debut + i];
for (int j = 0; j < n2; j++)
D[j] = tab[milieu + 1 + j];
// maintient trois pointeurs, un pour chacun des deux tableaux et un pour
// maintenir l'index actuel du tableau trié final
int i, j, k;
i = 0;
j = 0;
k = debut;
while (i < n1 && j < n2) {
if (G[i] <= D[j]) {
tab[k] = G[i];
i++;
} else {
tab[k] = D[j];
j++;
}
k++;
}
// Copiez tous les éléments restants du tableau non vide
while (i < n1) {
tab[k] = G[i];
i++;
k++;
}
while (j < n2) {
tab[k] = D[j];
j++;
k++;
}
}
// Tri par fusion
private static void triFusion(int tab[], int debut, int fin) {
if (debut < fin) {
// Trouvez le point milieu pour diviser le tableau en deux moitiés
int m = (debut + fin) / 2;
triFusion(tab, debut, m);
triFusion(tab, m + 1, fin);
// Fusionnez les deux moitiés triées
fusion(tab, debut, m, fin);
}
}
static public void sort(int[] array){
triFusion(array, 0, array.length - 1);
}
}

View File

@ -0,0 +1,20 @@
package lab12_sort;
public abstract class SelectionSort {
static public int[] sort(int[] array){
for (int i = 0; i < array.length-1; i++) {
int min = array[i];
int iMin = i;
for (int j = i; j < array.length; j++) {
if(array[j] < min){
min = array[j];
iMin = j;
}
}
int tmp = array[i];
array[i] = array[iMin];
array[iMin] = tmp;
}
return array;
}
}

View File

@ -0,0 +1,107 @@
package lab12_sort;
import tools.Chrono;
public class SortApplication {
public static void main(String[] args) {
long[][][] selection = new long[3][451][10];
long[][][] bubble = new long[3][451][10];
long[][][] yShort = new long[3][451][10];
long[][][] fusion = new long[3][451][10];
for (int iValue = 450; iValue >= 0; iValue--) {
int value = iValue*200 + 10000;
//int value = iValue*10 + 100;
Chrono valueChrono = new Chrono();
long sortTime = 0;
for (int iAttempt = 0; iAttempt < 10; iAttempt++) {
for (int iTable = 0; iTable < 3; iTable++){
int[] arrayIN;
switch (iTable) {
case 0:
arrayIN = ArrayFactory.createRandomArray(value, value);
break;
case 1:
arrayIN = ArrayFactory.createInvertedSortedArray(value);
break;
case 2:
arrayIN = ArrayFactory.createShuffleArray(value);
break;
default:
arrayIN = null;
break;
}
Chrono sort = new Chrono();
Chrono selectionChrono = new Chrono();
SelectionSort.sort(arrayIN);
selection[iTable][iValue][iAttempt] = selectionChrono.stop();
Chrono bubbleChrono = new Chrono();
BubbleSort.sort(arrayIN);
bubble[iTable][iValue][iAttempt] = bubbleChrono.stop();
Chrono yShortChrono = new Chrono();
YSort.sort(arrayIN);
yShort[iTable][iValue][iAttempt] = yShortChrono.stop();
Chrono fusionChrono = new Chrono();
FusionSort.sort(arrayIN);
fusion[iTable][iValue][iAttempt] = fusionChrono.stop();
sortTime = sort.stop();
}
}
long[][] selectionAverage = new long[3][451];
long[][] bubbleAverage = new long[3][451];
long[][] yShortAverage = new long[3][451];
long[][] fusionAverage = new long[3][451];
for(int iTable = 0; iTable < 3; iTable++){
for(int j = 0; j < 10; j++){
selectionAverage[iTable][iValue] += selection[iTable][iValue][j];
bubbleAverage[iTable][iValue] += bubble[iTable][iValue][j];
yShortAverage[iTable][iValue] += yShort[iTable][iValue][j];
fusionAverage[iTable][iValue] += fusion[iTable][iValue][j];
}
selectionAverage[iTable][iValue] /= 10.0;
bubbleAverage[iTable][iValue] /= 10.0;
yShortAverage[iTable][iValue] /= 10.0;
fusionAverage[iTable][iValue] /= 10.0;
print(value, iTable, "Selection", selectionAverage[iTable][iValue]);
print(value, iTable, "Bubble ", bubbleAverage[iTable][iValue]);
print(value, iTable, "YShort ", yShortAverage[iTable][iValue]);
print(value, iTable, "Fusion ", fusionAverage[iTable][iValue]);
System.out.println("");
}
System.out.println("Total time for sort " + value + ": " + sortTime/1000000.0 + " s");
System.out.println("Total time for " + value + ": " + valueChrono.stop()/1000000.0 + " s");
System.out.println("---------- ---------- ----------\n");
}
}
static void print(int value, int iTable, String name, long average){
System.out.print(name + "");
System.out.print("\tTable: " + iTable + " | Value: " + value);
System.out.print(" -> [us] " + average);
System.out.print("\n");
}
static void displayArray(int[] array){
int size = array.length;
String s = "Size ";
s += size;
s += ": {";
for (int i = 0; i < array.length-1; i++) {
s += array[i];
s += ", ";
}
s += array[size-1];
s += "}";
System.out.println(s);
}
}

38
src/tools/Chrono.java Normal file
View File

@ -0,0 +1,38 @@
package tools;
public class Chrono {
private long start, end, total;
private String name = null;
public Chrono(String name){
this.name = name;
start = System.nanoTime();
}
public Chrono(){
start = System.nanoTime();
}
/**
* Stop the chrono and return the total time in us
* @return the total length in us
*/
public long stop(){
end = System.nanoTime();
total = (long) ((end - start)/1000.0);
return total;
}
public String toString(){
String s = "";
if (name == null){
s += total;
s += " uS";
} else {
s += name;
s += " : \t";
s += total;
s += " uS";
}
return s;
}
}