From 3a1535ed55c9182ee63afb6d4ef789392b8ef796 Mon Sep 17 00:00:00 2001 From: f20021643 <alexandre.fournel@etu.univ-amu.fr> Date: Tue, 6 Dec 2022 17:16:00 +0100 Subject: [PATCH] question 2 --- .../fr/univamu/sorting/SortableIntList.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/fr/univamu/sorting/SortableIntList.java diff --git a/src/main/java/fr/univamu/sorting/SortableIntList.java b/src/main/java/fr/univamu/sorting/SortableIntList.java new file mode 100644 index 0000000..c709b4a --- /dev/null +++ b/src/main/java/fr/univamu/sorting/SortableIntList.java @@ -0,0 +1,44 @@ +package fr.univamu.sorting; +import java.util.*; + +public class SortableIntList { + public ArrayList<Integer> IntList; + + public void swap(int index1, int index2){ + int value1 = IntList.get(index1); + int value2 = IntList.get(index2); + IntList.set(index1, value1); + IntList.set(index2, value2); + } + + public int compare(int index1, int index2){ + if (IntList.get(index1) > IntList.get(index2)){ + return 1; + } + else if (IntList.get(index1) < IntList.get(index2)) { + return -1; + } + else { + return 0; + } + + } + + public int size(){ + return IntList.size(); + } + + public int get(int index){ + return IntList.get(index); + } + public void sort(){ + for (int i = 0; i < IntList.size() - 1; i++) + { + for (int j = i + 1; j < IntList.size(); j++) { + if (compare(IntList.get(j),IntList.get(i)) < 0){ + swap(IntList.get(i),IntList.get(j)); + } + + } + } +} -- GitLab