Hallo.
Ich hab folgendes Problem, ich will in diese Liste einen Selection Sort implementieren:
Nun hab ich mehrere Ideen wie ich den Selection Sort implementieren kann aber irgendwie funktioniert keine so richtig.
Swap Methode:
Selection Sort Idee:
oder
Mein Hauptsächliches Problem besteht darin, die Strings mit ein ander zu vergleichen.
Wäre nett wenn ihr mir helfen könntet, langsam bin ich am verzweifeln.
Gruß JoeBo
Ich hab folgendes Problem, ich will in diese Liste einen Selection Sort implementieren:
Code:
class DoubleList {
class ListNode {
private String value;
private ListNode prev;
private ListNode next;
public ListNode(String value, ListNode prev, ListNode next) {
this.value = value;
this.prev = prev;
this.next = next;
if (next != null) next.prev=this;
if (prev != null) prev.next = this;
}
public void setPrev(ListNode prev) {
this.prev = prev;
}
public void setNext(ListNode next) {
this.next = next;
}
public void setValue(String value) {
this.value = value;
}
public ListNode getPrev() {
return prev;
}
public ListNode getNext() {
return next;
}
public String getValue() {
return value;
}
}
private ListNode head;
private ListNode tail;
public DoubleList() {
head = null;
tail = null;
}
public void push_front(String value) {
head = new ListNode(value, null, head);
if (tail == null) {
tail = head;
}
}
public void push_back(String value) {
tail = new ListNode(value, tail, null);
if (head == null) {
head = tail;
}
}
public ListNode getRoot() {
return head;
}
public ListNode getTail() {
return tail;
}
public ListNode pop_front() {
if (head == null) {
return null;
} else {
ListNode result = head;
head = head.getNext();
head.setPrev(null);
return result;
}
}
public ListNode pop_back() {
if (tail == null) {
return null;
} else {
ListNode result = tail;
tail = tail.getPrev();
tail.setNext(null);
return result;
}
}
public void delete(ListNode node) {
if(node != null) {
if (head == node) {
head = node.getNext();
}
if (tail == node) {
tail = node.getPrev();
}
if (node.prev != null) {
node.getPrev().setNext(node.getNext());
}
if (node.next != null) {
node.next.prev =node.prev;
}
}
}
public void print() {
for(ListNode node = head; node != null; node = node.getNext()) {
System.out.print("(" + node.value + ") ");
}
System.out.println();
}
public int count(){
int result = 0;
ListNode node = head;
while(node != null) {
node = node.getNext();
result++;
}
return result;
}
public ListNode find(String value) {
for(ListNode node = head; node != null; node = node.next) {
if (node.getValue().equals(value)) {
return node;
}
}
return null;
}
}
public class ListExample {
public static void main(String[] params) {
DoubleList l = new DoubleList();
l.push_back("0");
l.push_back("1");
l.push_back("2");
l.push_back("3");
l.push_back("4");
l.push_front("5");
l.push_front("6");
l.push_front("7");
l.push_front("8");
System.out.println(l.count());
l.print();
l.delete(l.find("787"));
l.pop_front();
l.pop_back();
l.print();
}
}
Nun hab ich mehrere Ideen wie ich den Selection Sort implementieren kann aber irgendwie funktioniert keine so richtig.
Swap Methode:
Code:
public void swap(ListNode node1, ListNode node2){
ListNode tmp = node1;
node1 = node2;
node2 = tmp;
}
Code:
public void SelectionSort(){
for(ListNode node = head; node != tail; node=node.next){
ListNode min = new ListNode(node.getValue(), node.getPrev(), node.getNext());
for(ListNode node2 = node.getNext(); node2 != tail; node2=node2.next ){
if(node2.getValue() < min.getValue())
node2.setValue(min);
}
}
}
Code:
public void SelectionSort(){
for(ListNode node = head; node != tail; node=node.next){
int min = node.getValue();
for(ListNode node2 = node.getNext(); node2 != tail; node2=node2.next ){
if(node2.getValue() < min)
node2.setValue(min);
}
}
}
Wäre nett wenn ihr mir helfen könntet, langsam bin ich am verzweifeln.
Gruß JoeBo