From 6eee9df1fe1a1b01c18358b1c232e52050c77d18 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 11 Nov 2024 10:56:52 +0100 Subject: [PATCH] added lab14 ex2 --- src/lab14_strategy/ex2/SortAlgorithm.java | 5 +++ src/lab14_strategy/ex2/Sorter.java | 21 +++++++++++ .../ex2/StrategySortLauncher.java | 35 +++++++++++++++++++ .../ex2/algorithms/BubbleSort.java | 23 ++++++++++++ .../ex2/algorithms/InsertionSort.java | 22 ++++++++++++ .../ex2/algorithms/SelectionSort.java | 22 ++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 src/lab14_strategy/ex2/SortAlgorithm.java create mode 100644 src/lab14_strategy/ex2/Sorter.java create mode 100644 src/lab14_strategy/ex2/StrategySortLauncher.java create mode 100644 src/lab14_strategy/ex2/algorithms/BubbleSort.java create mode 100644 src/lab14_strategy/ex2/algorithms/InsertionSort.java create mode 100644 src/lab14_strategy/ex2/algorithms/SelectionSort.java 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