-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
234 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>ru.mifi.practice</groupId> | ||
<artifactId>AaDS</artifactId> | ||
<version>2024.2</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>vol3</artifactId> | ||
<name>vol3</name> | ||
<description>Модуль №3. Сортировка</description> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ru.mifi.practice.vol3; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class CountSort implements Sort<Integer> { | ||
private final int maxElement; | ||
|
||
public CountSort(int maxElement) { | ||
this.maxElement = maxElement; | ||
} | ||
|
||
@Override | ||
public List<Integer> sort(List<Integer> array, Counter counter, boolean debug) { | ||
List<Integer> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> generateSlice(int length) { | ||
Random r = new Random(new Date().getTime()); | ||
List<Integer> 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<Integer> slice = generateSlice(10000); | ||
// List<Integer> 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<Integer> { | ||
MERGE(new MergeSort<>()), | ||
QUICK(new QuickSort<>()), | ||
COUNT(new CountSort(MAX_GENERATED_ELEMENT_VALUE)); | ||
|
||
private final Sort<Integer> sort; | ||
|
||
Algorithms(Sort<Integer> sort) { | ||
this.sort = sort; | ||
} | ||
|
||
@Override | ||
public List<Integer> sort(List<Integer> array, Counter counter, boolean debug) { | ||
return sort.sort(array, counter, debug); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<E extends Comparable<E>> implements Sort<E> { | ||
public static <E extends Comparable<E>> List<E> merge(List<E> left, List<E> right, Counter counter, boolean debug) { | ||
List<E> 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<E> sort(List<E> array, Counter counter, boolean debug) { | ||
if (array.size() <= 1) { | ||
return array; | ||
} | ||
List<E> left = new ArrayList<>(array.size() / 2); | ||
List<E> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<E extends Comparable<E>> implements Sort<E> { | ||
@Override | ||
public List<E> sort(List<E> array, Counter counter, boolean debug) { | ||
List<E> sortable = new ArrayList<>(array); | ||
quickSort(sortable, 0, sortable.size() - 1, counter, debug); | ||
return sortable; | ||
} | ||
|
||
private void quickSort(List<E> 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<E> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ru.mifi.practice.vol3; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@FunctionalInterface | ||
public interface Sort<E extends Comparable<E>> { | ||
List<E> sort(List<E> 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()); | ||
} | ||
} | ||
} | ||
} |