diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing0907.java | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing0907.java')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/examples/Listing0907.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing0907.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing0907.java new file mode 100644 index 0000000..1b83b14 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing0907.java @@ -0,0 +1,49 @@ +/* Listing0907.java */
+
+public class Listing0907
+{
+ public static Object getSmallest(Comparable[] objects)
+ {
+ Object smallest = objects[0];
+ for (int i = 1; i < objects.length; ++i) {
+ if (objects[i].compareTo(smallest) < 0) {
+ smallest = objects[i];
+ }
+ }
+ return smallest;
+ }
+
+ public static void bubbleSort(Comparable[] objects)
+ {
+ boolean sorted;
+ do {
+ sorted = true;
+ for (int i = 0; i < objects.length - 1; ++i) {
+ if (objects[i].compareTo(objects[i + 1]) > 0) {
+ Comparable tmp = objects[i];
+ objects[i] = objects[i + 1];
+ objects[i + 1] = tmp;
+ sorted = false;
+ }
+ }
+ } while (!sorted);
+ }
+
+ public static void main(String[] args)
+ {
+ //Erzeugen eines String-Arrays
+ Comparable[] objects = new Comparable[4];
+ objects[0] = "STRINGS";
+ objects[1] = "SIND";
+ objects[2] = "PAARWEISE";
+ objects[3] = "VERGLEICHBAR";
+ //Ausgeben des kleinsten Elements
+ System.out.println((String)getSmallest(objects));
+ System.out.println("--");
+ //Sortieren und Ausgaben
+ bubbleSort(objects);
+ for (int i = 0; i < objects.length; ++i) {
+ System.out.println((String)objects[i]);
+ }
+ }
+}
\ No newline at end of file |
