From eb9efb185f8e860a684fda8fcde606f210d3c653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A5=D0=BB=D0=B5=D0=B1=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Mon, 18 Nov 2024 00:33:30 +0300 Subject: [PATCH] Sorting --- pom.xml | 1 + .../practice/vol2/yasymb/stage2/Stage.java | 10 ++-- .../practice/vol2/yasymb/stage2/Stage2.java | 16 +++--- vol3/pom.xml | 15 ++++++ .../java/ru/mifi/practice/vol3/CountSort.java | 29 +++++++++++ .../main/java/ru/mifi/practice/vol3/Main.java | 52 +++++++++++++++++++ .../java/ru/mifi/practice/vol3/MergeSort.java | 50 ++++++++++++++++++ .../java/ru/mifi/practice/vol3/QuickSort.java | 47 +++++++++++++++++ .../main/java/ru/mifi/practice/vol3/Sort.java | 27 ++++++++++ 9 files changed, 234 insertions(+), 13 deletions(-) create mode 100644 vol3/pom.xml create mode 100644 vol3/src/main/java/ru/mifi/practice/vol3/CountSort.java create mode 100644 vol3/src/main/java/ru/mifi/practice/vol3/Main.java create mode 100644 vol3/src/main/java/ru/mifi/practice/vol3/MergeSort.java create mode 100644 vol3/src/main/java/ru/mifi/practice/vol3/QuickSort.java create mode 100644 vol3/src/main/java/ru/mifi/practice/vol3/Sort.java diff --git a/pom.xml b/pom.xml index a4d05e8..f17671c 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ vol1 vol2 + vol3 diff --git a/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage.java b/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage.java index c7095a9..84ba824 100644 --- a/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage.java +++ b/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage.java @@ -56,12 +56,12 @@ private static void start(String x, String y, String z) { } static void main(String[] args) { -// start("x", "y", "z"); -// start("win", "lose", "game"); + start("x", "y", "z"); + start("win", "lose", "game"); start("love", "hate", "feel"); -// start("four", "seven", "eight"); -// start("a", "b", "a"); -// start("odin", "odin", "mnogo"); + start("four", "seven", "eight"); + start("a", "b", "a"); + start("odin", "odin", "mnogo"); // Generator generator = new Generator(4, 4); // for (int i = 0; i < 1; i++) { // Generator.Result generated = generator.generate(); diff --git a/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage2.java b/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage2.java index 99ba50a..2042064 100644 --- a/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage2.java +++ b/vol2/src/main/java/ru/mifi/practice/vol2/yasymb/stage2/Stage2.java @@ -20,15 +20,15 @@ static boolean start(String x, String y, String z, boolean debug) { } static void simple(boolean debug) { - start("x", "y", "z", debug); - start("win", "lose", "game", debug); - start("love", "hate", "feel", debug); +// start("x", "y", "z", debug); +// start("win", "lose", "game", debug); +// start("love", "hate", "feel", debug); start("four", "seven", "eight", debug); - start("a", "b", "a", debug); - start("odin", "odin", "mnogo", debug); - start("acdf", "adbg", "baeg", debug); - start("accb", "adeg", "bcfe", debug); - start("acef", "abfg", "bdcf", debug); +// start("a", "b", "a", debug); +// start("odin", "odin", "mnogo", debug); +// start("acdf", "adbg", "baeg", debug); +// start("accb", "adeg", "bcfe", debug); +// start("acef", "abfg", "bdcf", debug); } static void generated(boolean debug) { diff --git a/vol3/pom.xml b/vol3/pom.xml new file mode 100644 index 0000000..39f3207 --- /dev/null +++ b/vol3/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + ru.mifi.practice + AaDS + 2024.2 + ../pom.xml + + vol3 + vol3 + Модуль №3. Сортировка + + diff --git a/vol3/src/main/java/ru/mifi/practice/vol3/CountSort.java b/vol3/src/main/java/ru/mifi/practice/vol3/CountSort.java new file mode 100644 index 0000000..a0fc69b --- /dev/null +++ b/vol3/src/main/java/ru/mifi/practice/vol3/CountSort.java @@ -0,0 +1,29 @@ +package ru.mifi.practice.vol3; + +import java.util.ArrayList; +import java.util.List; + +public final class CountSort implements Sort { + private final int maxElement; + + public CountSort(int maxElement) { + this.maxElement = maxElement; + } + + @Override + public List sort(List array, Counter counter, boolean debug) { + List result = new ArrayList<>(array); + int[] filter = new int[maxElement + 1]; + for (Integer it : array) { + ++filter[it]; + counter.increment(); + } + for (int val = 0; val <= maxElement; val++) { + for (int i = 0; i < filter[val]; i++) { + result.add(val); + counter.increment(); + } + } + return result; + } +} diff --git a/vol3/src/main/java/ru/mifi/practice/vol3/Main.java b/vol3/src/main/java/ru/mifi/practice/vol3/Main.java new file mode 100644 index 0000000..3f54902 --- /dev/null +++ b/vol3/src/main/java/ru/mifi/practice/vol3/Main.java @@ -0,0 +1,52 @@ +package ru.mifi.practice.vol3; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Random; + +public abstract class Main { + private static final int MAX_GENERATED_ELEMENT_VALUE = 100; + + private static List generateSlice(int length) { + Random r = new Random(new Date().getTime()); + List slice = new ArrayList<>(length); + for (int i = 0; i < length; i++) { + slice.add(r.nextInt(MAX_GENERATED_ELEMENT_VALUE + 1)); + } + return slice; + } + + /** + * ДЗ: почему реальная сложность больше в два раза сложности расчетной? + */ + public static void main(String[] args) { + boolean debug = false; + List slice = generateSlice(10000); +// List slice = List.of(7, 8, 2, 0, 5, 2, 7, 0); + System.out.println("BAD : " + (slice.size() * slice.size())); + System.out.println("MUST : " + Math.round(slice.size() * (Math.log(slice.size()) / Math.log(2)))); + for (Algorithms algorithm : Algorithms.values()) { + Sort.Counter counter = new Sort.Counter.Default(); + var result = algorithm.sort(slice, counter, debug); + System.out.printf("%7s: %s%n", algorithm, counter); + } + } + + enum Algorithms implements Sort { + MERGE(new MergeSort<>()), + QUICK(new QuickSort<>()), + COUNT(new CountSort(MAX_GENERATED_ELEMENT_VALUE)); + + private final Sort sort; + + Algorithms(Sort sort) { + this.sort = sort; + } + + @Override + public List sort(List array, Counter counter, boolean debug) { + return sort.sort(array, counter, debug); + } + } +} diff --git a/vol3/src/main/java/ru/mifi/practice/vol3/MergeSort.java b/vol3/src/main/java/ru/mifi/practice/vol3/MergeSort.java new file mode 100644 index 0000000..66f483f --- /dev/null +++ b/vol3/src/main/java/ru/mifi/practice/vol3/MergeSort.java @@ -0,0 +1,50 @@ +package ru.mifi.practice.vol3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public final class MergeSort> implements Sort { + public static > List merge(List left, List right, Counter counter, boolean debug) { + List result = new ArrayList<>(right.size() + left.size()); + int l = 0; + int r = 0; + while (l < left.size() || r < right.size()) { + if (l < left.size() && (r == right.size() || left.get(l).compareTo(right.get(r)) < 0)) { + result.add(left.get(l)); + ++l; + } else { + result.add(right.get(r)); + ++r; + } + counter.increment(); + } + if (debug) { + System.out.println("Result : " + Arrays.toString(result.toArray())); + } + return result; + } + + @Override + public List sort(List array, Counter counter, boolean debug) { + if (array.size() <= 1) { + return array; + } + List left = new ArrayList<>(array.size() / 2); + List right = new ArrayList<>(array.size() / 2); + for (int i = 0; i < array.size(); i++) { + E element = array.get(i); + if (i < array.size() / 2) { + left.add(element); + } else { + right.add(element); + } + counter.increment(); + } + if (debug) { + System.out.println("Left : " + Arrays.toString(left.toArray())); + System.out.println("Right : " + Arrays.toString(right.toArray())); + } + return merge(sort(left, counter, debug), sort(right, counter, debug), counter, debug); + } +} diff --git a/vol3/src/main/java/ru/mifi/practice/vol3/QuickSort.java b/vol3/src/main/java/ru/mifi/practice/vol3/QuickSort.java new file mode 100644 index 0000000..3b05116 --- /dev/null +++ b/vol3/src/main/java/ru/mifi/practice/vol3/QuickSort.java @@ -0,0 +1,47 @@ +package ru.mifi.practice.vol3; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public final class QuickSort> implements Sort { + @Override + public List sort(List array, Counter counter, boolean debug) { + List sortable = new ArrayList<>(array); + quickSort(sortable, 0, sortable.size() - 1, counter, debug); + return sortable; + } + + private void quickSort(List array, int low, int high, Counter counter, boolean debug) { + if (low >= high - 1) { + return; + } + int middle = part(array, low, high, counter, debug); + quickSort(array, low, middle, counter, debug); + quickSort(array, middle + 1, high, counter, debug); + } + + private int part(List array, int low, int high, Counter counter, boolean debug) { + E baseElement = array.get(baseIndex(low, high, counter, debug)); + int i = low - 1; + int j = high; + while (true) { + do { + ++i; + counter.increment(); + } while (array.get(i).compareTo(baseElement) < 0); + do { + --j; + counter.increment(); + } while (array.get(j).compareTo(baseElement) > 0); + if (i >= j) { + return j; + } + Collections.swap(array, i, j); + } + } + + private int baseIndex(int low, int high, Counter counter, boolean debug) { + return low + (high - low) / 2; + } +} diff --git a/vol3/src/main/java/ru/mifi/practice/vol3/Sort.java b/vol3/src/main/java/ru/mifi/practice/vol3/Sort.java new file mode 100644 index 0000000..6585b3a --- /dev/null +++ b/vol3/src/main/java/ru/mifi/practice/vol3/Sort.java @@ -0,0 +1,27 @@ +package ru.mifi.practice.vol3; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +@FunctionalInterface +public interface Sort> { + List sort(List array, Counter counter, boolean debug); + + interface Counter { + void increment(); + + final class Default implements Counter { + private final AtomicInteger count = new AtomicInteger(0); + + @Override + public void increment() { + count.incrementAndGet(); + } + + @Override + public String toString() { + return String.valueOf(count.intValue()); + } + } + } +}