Für Aufgabe d)
public class HybridSortRandomPivot extends HybridSort {
// TODO: Implement
private Random random;
public HybridSortRandomPivot() { random = new Random(); }
protected int selectPivot(int left, int right) { return random.nextInt(right-left+1)+left; }
}
a)
public int compareTo(Card other) {
// TODO: implement
if (this.value < other.value) { return -1; }
if (this.value > other.value) { return 1; }
if (this.value == other.value) {
if (suitValue(this.suit) < suitValue(other.suit)) { return -1; }
if (suitValue(this.suit) > suitValue(other.suit)) { return 1; } }
return 0; }
//Here numbers were assigned to the colors in ascending order.
private int suitValue(Suit suit) {
switch (suit) {
case Diamonds:
return 0;
case Hearts:
return 1;
case Spades:
return 2;
case Clubs:
return 3;
default: throw new RuntimeException(); } }
}
b)
public void sort(SortArray array, int k) {
assert(k>=0);
// TODO: Implement
this.k = k;
this.array = array;
quicksortRec(0, array.getNumberOfItems()-1); }
protected int selectPiv(int left, int right) {
return left; }
private void quicksortRec(int left, int right) {
if (left >= right) { return; }
if (right-left+1 < k) { insertionSort(left,right); }
else { int partitionIndex = partition(left, right);
quicksortRec(left, partitionIndex-1);
quicksortRec(partitionIndex+1, right); } }
private int partition(int left, int right) {
int pivotIndex = selectPiv(left, right);
Card pivot = array.getElementAt(pivotIndex);
swap(pivotIndex, left);
int lastSmall = left;
for (int j=left; j<=right; j++) {
if (array.getElementAt(j).compareTo(pivot) < 0) {
lastSmall = lastSmall+1;
swap(lastSmall, j); } }
swap(lastSmall, left);
return lastSmall; }
private void insertionSort(int left, int right) {
for (int j=left+1; j<=right; j++) {
Card key = array.getElementAt(j);
int i = j-1;
while (i>=left && array.getElementAt(i).compareTo(key) > 0) {
array.setElementAt(i+1, array.getElementAt(i));
i=i-1; }
array.setElementAt(i+1, key); } }
private void swap(int a, int b) {
if (a==b) {
return; }
Card temp = array.getElementAt(a);
array.setElementAt(a, array.getElementAt(b));
array.setElementAt(b, temp); }
}
c) die Vorlage
public class HybridOptimizer {
/**
* Find the optimal value k of the HybridSort algorithm for
* the given data. Note that we assume that the first local minimum is
* the global minimum.
* @param testData Data on which the optimal k should be calculated
* [USER=49078]@Return[/USER] the optimal k
*/
public static int optimize(ArrayList<Card> testData) {
// TODO: implement
return -1;
}
}
public class Card implements Comparable<Card> {
public enum Suit {
Diamonds,
Hearts,
Spades,
Clubs;
}
private Suit suit;
private int value;
public Card(Suit suit, int value) {
this.suit = suit;
this.value = value;
}
@Override
public int compareTo(Card other) {
return Comparator
.comparingInt((Card c) -> c.value)
.thenComparingInt((Card c) -> c.suit.ordinal())
.compare(this, other);
}
@Override
public String toString() {
return "Card{" + "suit=" + suit.name() + ", value=" + value + '}';
}
public static void main(String... args) {
List<Card> cards = new ArrayList<>();
cards.add(new Card(Suit.Diamonds, 2));
cards.add(new Card(Suit.Hearts, 13));
cards.add(new Card(Suit.Hearts, 2));
cards.add(new Card(Suit.Diamonds, 8));
cards.add(new Card(Suit.Diamonds, 3));
cards.add(new Card(Suit.Spades, 4));
cards.add(new Card(Suit.Diamonds, 5));
cards.add(new Card(Suit.Clubs, 9));
cards.add(new Card(Suit.Diamonds, 5));
cards.add(new Card(Suit.Spades, 11));
cards.add(new Card(Suit.Diamonds, 10));
cards.add(new Card(Suit.Hearts, 1));
cards.add(new Card(Suit.Hearts, 2));
cards.add(new Card(Suit.Spades, 7));
cards.sort(Comparator.naturalOrder());
cards.forEach(System.out::println);
}
}
Card{suit=Hearts, value=1}
Card{suit=Diamonds, value=2}
Card{suit=Hearts, value=2}
Card{suit=Hearts, value=2}
Card{suit=Diamonds, value=3}
Card{suit=Spades, value=4}
Card{suit=Diamonds, value=5}
Card{suit=Diamonds, value=5}
Card{suit=Spades, value=7}
Card{suit=Diamonds, value=8}
Card{suit=Clubs, value=9}
Card{suit=Diamonds, value=10}
Card{suit=Spades, value=11}
Card{suit=Hearts, value=13}
Alles klar! Vielen DankIch hätte noch 2 Vorschläge zum bestehenden Code:
1) Sieht für mich so aus als wenn Suit ein Enum ist, stimmt das? Dann kannst du dir vermutlich den Switch-Case sparen und stattdessen die Ordinale für den Vergleich verwenden https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Enum.html#ordinal()
2) Du hast den Vergleich detailliert aufgeschrieben. Persönlich mache ich das nur noch selten, das kann schnell unübersichtlich und fehleranfällig werden. Du kannst dir vorgefertigte Comparatoren dafür zur Hilfe nehmen.
Diese Vorschläge sehen dann zusammen beispielhaft so aus:
Java:public class Card implements Comparable<Card> { public enum Suit { Diamonds, Hearts, Spades, Clubs; } private Suit suit; private int value; public Card(Suit suit, int value) { this.suit = suit; this.value = value; } @Override public int compareTo(Card other) { return Comparator .comparingInt((Card c) -> c.value) .thenComparingInt((Card c) -> c.suit.ordinal()) .compare(this, other); } @Override public String toString() { return "Card{" + "suit=" + suit.name() + ", value=" + value + '}'; } public static void main(String... args) { List<Card> cards = new ArrayList<>(); cards.add(new Card(Suit.Diamonds, 2)); cards.add(new Card(Suit.Hearts, 13)); cards.add(new Card(Suit.Hearts, 2)); cards.add(new Card(Suit.Diamonds, 8)); cards.add(new Card(Suit.Diamonds, 3)); cards.add(new Card(Suit.Spades, 4)); cards.add(new Card(Suit.Diamonds, 5)); cards.add(new Card(Suit.Clubs, 9)); cards.add(new Card(Suit.Diamonds, 5)); cards.add(new Card(Suit.Spades, 11)); cards.add(new Card(Suit.Diamonds, 10)); cards.add(new Card(Suit.Hearts, 1)); cards.add(new Card(Suit.Hearts, 2)); cards.add(new Card(Suit.Spades, 7)); cards.sort(Comparator.naturalOrder()); cards.forEach(System.out::println); } }
Ausgabe:
Code:Card{suit=Hearts, value=1} Card{suit=Diamonds, value=2} Card{suit=Hearts, value=2} Card{suit=Hearts, value=2} Card{suit=Diamonds, value=3} Card{suit=Spades, value=4} Card{suit=Diamonds, value=5} Card{suit=Diamonds, value=5} Card{suit=Spades, value=7} Card{suit=Diamonds, value=8} Card{suit=Clubs, value=9} Card{suit=Diamonds, value=10} Card{suit=Spades, value=11} Card{suit=Hearts, value=13}
Ansonsten zu deiner Aufgabe c). Das wichtigste ist, dass du für dich herausfindest was "Lokales Minimum" bedeudet. Wenn du das weißt st es eigentlich einfach.