diff --git a/src/lab14_strategy/ex2/SortAlgorithm.java b/src/lab14_strategy/ex2/SortAlgorithm.java new file mode 100644 index 0000000..77858fc --- /dev/null +++ b/src/lab14_strategy/ex2/SortAlgorithm.java @@ -0,0 +1,5 @@ +package lab14_strategy.ex2; + +public interface SortAlgorithm { + void sort(int[] vector); +} diff --git a/src/lab14_strategy/ex2/Sorter.java b/src/lab14_strategy/ex2/Sorter.java new file mode 100644 index 0000000..f68f610 --- /dev/null +++ b/src/lab14_strategy/ex2/Sorter.java @@ -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); + } +} diff --git a/src/lab14_strategy/ex2/StrategySortLauncher.java b/src/lab14_strategy/ex2/StrategySortLauncher.java new file mode 100644 index 0000000..16f56b4 --- /dev/null +++ b/src/lab14_strategy/ex2/StrategySortLauncher.java @@ -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); + } +} diff --git a/src/lab14_strategy/ex2/algorithms/BubbleSort.java b/src/lab14_strategy/ex2/algorithms/BubbleSort.java new file mode 100644 index 0000000..0e57df4 --- /dev/null +++ b/src/lab14_strategy/ex2/algorithms/BubbleSort.java @@ -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[i + 1]) { + nbrePermutation++; + temp = vector[i + 1]; + vector[i + 1] = vector[i]; + vector[i] = temp; + } + } + } + } +} diff --git a/src/lab14_strategy/ex2/algorithms/InsertionSort.java b/src/lab14_strategy/ex2/algorithms/InsertionSort.java new file mode 100644 index 0000000..7a885b1 --- /dev/null +++ b/src/lab14_strategy/ex2/algorithms/InsertionSort.java @@ -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= 0; j--) { + if (vector[j] > temp) { + vector[j + 1] = vector[j]; + vector[j] = temp; + } else { + vector[j + 1] = temp; + break; + } + } + } + } +} diff --git a/src/lab14_strategy/ex2/algorithms/SelectionSort.java b/src/lab14_strategy/ex2/algorithms/SelectionSort.java new file mode 100644 index 0000000..6de1a47 --- /dev/null +++ b/src/lab14_strategy/ex2/algorithms/SelectionSort.java @@ -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