added lab14 ex2

This commit is contained in:
Louis Heredero 2024-11-11 10:56:52 +01:00
parent 23829388cb
commit 6eee9df1fe
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package lab14_strategy.ex2;
public interface SortAlgorithm {
void sort(int[] vector);
}

View File

@ -0,0 +1,21 @@
package lab14_strategy.ex2;
public class Sorter {
private SortAlgorithm algorithm;
public void sort(int[] vector) {
algorithm.sort(vector);
}
public void setAlgorithm(SortAlgorithm algorithm) {
this.algorithm = algorithm;
}
public void showVectorData(int[] vector) {
StringBuilder sb = new StringBuilder();
for (int j : vector) {
sb.append(j).append("\t");
}
System.out.println(sb);
}
}

View File

@ -0,0 +1,35 @@
package lab14_strategy.ex2;
import lab14_strategy.ex2.algorithms.BubbleSort;
import lab14_strategy.ex2.algorithms.InsertionSort;
import lab14_strategy.ex2.algorithms.SelectionSort;
public class StrategySortLauncher {
public static void main(String[] args) {
StrategySortLauncher launcher = new StrategySortLauncher();
launcher.test();
}
public void test() {
int[] tab1 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
int[] tab2 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
int[] tab3 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
Sorter sorter = new Sorter();
System.out.println("test bubble sort");
sorter.setAlgorithm(new BubbleSort());
sorter.sort(tab1);
sorter.showVectorData(tab1);
System.out.println("test insert sort");
sorter.setAlgorithm(new InsertionSort());
sorter.sort(tab2);
sorter.showVectorData(tab2);
System.out.println("test selection sort");
sorter.setAlgorithm(new SelectionSort());
sorter.sort(tab3);
sorter.showVectorData(tab3);
}
}

View File

@ -0,0 +1,23 @@
package lab14_strategy.ex2.algorithms;
import lab14_strategy.ex2.SortAlgorithm;
public class BubbleSort implements SortAlgorithm {
@Override
public void sort(int[] vector) {
int temp;
int nbrePermutation = -1;
int nbreIteration = 0;
while (nbrePermutation != 0) {
nbrePermutation = 0;
for (int i=0; i<vector.length - nbreIteration - 1; i++) {
if (vector[i] > vector[i + 1]) {
nbrePermutation++;
temp = vector[i + 1];
vector[i + 1] = vector[i];
vector[i] = temp;
}
}
}
}
}

View File

@ -0,0 +1,22 @@
package lab14_strategy.ex2.algorithms;
import lab14_strategy.ex2.SortAlgorithm;
public class InsertionSort implements SortAlgorithm {
@Override
public void sort(int[] vector) {
int temp;
for (int i=1; i<vector.length; i++) {
temp = vector[i];
for (int j = i - 1; j >= 0; j--) {
if (vector[j] > temp) {
vector[j + 1] = vector[j];
vector[j] = temp;
} else {
vector[j + 1] = temp;
break;
}
}
}
}
}

View File

@ -0,0 +1,22 @@
package lab14_strategy.ex2.algorithms;
import lab14_strategy.ex2.SortAlgorithm;
public class SelectionSort implements SortAlgorithm {
@Override
public void sort(int[] vector) {
int temp, cursor = 0;
for (int i=0; i<vector.length; i++) {
temp = vector[i];
cursor = i;
for (int j = i; j < vector.length; j++) {
if (vector[j] < temp) {
temp = vector[j];
cursor = j;
}
}
vector[cursor] = vector[i];
vector[i] = temp;
}
}
}