added lab14 ex2
This commit is contained in:
parent
23829388cb
commit
6eee9df1fe
5
src/lab14_strategy/ex2/SortAlgorithm.java
Normal file
5
src/lab14_strategy/ex2/SortAlgorithm.java
Normal file
@ -0,0 +1,5 @@
|
||||
package lab14_strategy.ex2;
|
||||
|
||||
public interface SortAlgorithm {
|
||||
void sort(int[] vector);
|
||||
}
|
21
src/lab14_strategy/ex2/Sorter.java
Normal file
21
src/lab14_strategy/ex2/Sorter.java
Normal 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);
|
||||
}
|
||||
}
|
35
src/lab14_strategy/ex2/StrategySortLauncher.java
Normal file
35
src/lab14_strategy/ex2/StrategySortLauncher.java
Normal 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);
|
||||
}
|
||||
}
|
23
src/lab14_strategy/ex2/algorithms/BubbleSort.java
Normal file
23
src/lab14_strategy/ex2/algorithms/BubbleSort.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
22
src/lab14_strategy/ex2/algorithms/InsertionSort.java
Normal file
22
src/lab14_strategy/ex2/algorithms/InsertionSort.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
22
src/lab14_strategy/ex2/algorithms/SelectionSort.java
Normal file
22
src/lab14_strategy/ex2/algorithms/SelectionSort.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user