Skip to content

Commit

Permalink
Sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Pastor committed Nov 18, 2024
1 parent eb9efb1 commit 8f4f8be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
13 changes: 7 additions & 6 deletions vol3/src/main/java/ru/mifi/practice/vol3/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ private static List<Integer> generateSlice(int length) {
* ДЗ: почему реальная сложность больше в два раза сложности расчетной?
*/
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))));
boolean debug = true;
// 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))));
System.out.println("========");
for (Algorithms algorithm : Algorithms.values()) {
Sort.Counter counter = new Sort.Counter.Default();
var result = algorithm.sort(slice, counter, debug);
algorithm.sort(slice, counter, debug);
System.out.printf("%7s: %s%n", algorithm, counter);
}
}
Expand Down
29 changes: 28 additions & 1 deletion vol3/src/main/java/ru/mifi/practice/vol3/QuickSort.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package ru.mifi.practice.vol3;

import java.util.ArrayList;
import java.util.Arrays;
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) {
if (debug) {
System.out.println("Input : " + Arrays.toString(array.toArray()));
}
List<E> sortable = new ArrayList<>(array);
quickSort(sortable, 0, sortable.size() - 1, counter, debug);
if (debug) {
System.out.println("Output : " + Arrays.toString(sortable.toArray()));
}
return sortable;
}

Expand All @@ -22,22 +29,42 @@ private void quickSort(List<E> array, int low, int high, Counter counter, boolea
}

private int part(List<E> array, int low, int high, Counter counter, boolean debug) {
E baseElement = array.get(baseIndex(low, high, counter, debug));
int index = baseIndex(low, high, counter, debug);
E baseElement = array.get(index);
if (debug) {
System.out.println("Index : [" + index + "] = " + baseElement);
System.out.println("Range : " + low + " - " + high);
System.out.println(Arrays.toString(array.toArray()));
}
int i = low - 1;
int j = high;
while (true) {
do {
++i;
counter.increment();
if (debug) {
System.out.println("Equals : array[" + i + "] < " + baseElement + "; "
+ array.get(i) + " < " + baseElement + " -> " + (array.get(i).compareTo(baseElement) < 0));
}
} while (array.get(i).compareTo(baseElement) < 0);
do {
--j;
counter.increment();
if (debug) {
System.out.println("Equals : array[" + j + "] > " + baseElement + "; "
+ array.get(j) + " > " + baseElement + " -> " + (array.get(j).compareTo(baseElement) > 0));
}
} while (array.get(j).compareTo(baseElement) > 0);
if (i >= j) {
if (debug) {
System.out.println("Middle : " + i);
}
return j;
}
Collections.swap(array, i, j);
if (debug) {
System.out.println(Arrays.toString(array.toArray()));
}
}
}

Expand Down

0 comments on commit 8f4f8be

Please sign in to comment.