【发布时间】:2019-11-23 15:47:28
【问题描述】:
我们在课堂上看到了选择排序算法如何处理数组数据结构。在本实验中,我们将练习如何在链表 ADT 上执行选择排序。 1. 将下面的选择排序伪代码转换为升序排序。 (selectionSort_asc 函数) 一种。在长度为n的链表中找到最小值的节点 湾。在新的结果链表中追加 min 节点 C。从原始链表中删除 min d。重复步骤 a-c 直到原始链表为空 e.返回结果链表 2. 转换以下选择排序伪代码以执行降序排序。 (selectionSort_desc 函数) 一种。在长度为n的链表中找到最大值的节点 湾。在新的结果链表中追加最大节点 C。从原始链表中删除最大值 d。重复步骤 a-c 直到原始链表为空 e.返回结果链表
我在下面尝试了这段代码,但它没有给我正确的输出。
public class Sort {
public static void main(String[] args){
Node head = initializeList("ilovedata");
System.out.println("\n List Before selectionSort_asc");
printList(head);
head = selectionSort_asc(head);
System.out.println("\n List After selectionSort_asc");
printList(head);
// Expected answer: -> a -> a -> d -> e -> i -> l -> o -> t -> v
head = initializeList("ilovedata");
System.out.println("\n List Before selectionSort_desc");
printList(head);
head = selectionSort_desc(head);
System.out.println("\n List After selectionSort_desc");
printList(head);
// Expected answer: -> v -> t -> o -> l -> i -> e -> d -> a -> a
}
public static Node selectionSort_asc(Node head){
Node result = null;
Node curr, prev, min;
while(head!=null) {
curr = head;
prev = null;
min = head;
while(curr.next!=null) {
curr = curr.next;
if(curr.item<min.item) {
prev = min;
min = curr;
}
}
//append the min at the end of result list
Node add_min = new Node(min.item);
if(result==null)
result = add_min;
else {
Node last = result;
while(last.next!=null) {
last = last.next;
}
last.next = add_min;
}
//delete the min node form original list
if(min==head) {
head = head.next;
}
else if(min.next==null){
prev.next = null;
}else {
prev.next = prev.next.next;
min.next = null;
}
}
return result;
}
public static Node selectionSort_desc(Node head){
Node result = null;
Node curr, prev, max;
while(head!=null) {
curr = head;
prev = null;
max = head;
//find out the max node
while(curr.next!=null) {
curr = curr.next;
if(curr.item>max.item) {
prev = max;
max = curr;
}
}
//add max to the end of result list
Node add_max = new Node(max.item);
if(result==null) {
result = add_max;
}
else {
Node last = result;
while(last.next!=null) {
last = last.next;
}
last.next = add_max;
}
//delete min from original list
if(max == head) {
head = head.next;
}
else if(max.next==null){
prev.next = null;
}
else {
prev.next = max.next;
max.next = null;
}
}
return result;
}
// Method that takes a string and insert its characters into a linked list
public static Node initializeList(String str){
Node head= new Node(str.charAt(0)),cur;
int i;
for(cur= head,i=1;i<str.length();i++,cur=cur.next){
cur.next = new Node(str.charAt(i));
}
return head;
}
// Method for printing linked list
public static void printList(Node head){
Node cur = head;
if(head==null)
System.out.print("<EMPTY>");
for(;cur!=null;cur=cur.next){
System.out.print("-> "+cur.item+" ");
}
}
}
【问题讨论】:
-
这段代码的输出是: List Before selectionSort_asc -> i -> l -> o -> v -> e -> d -> a -> t -> a List After selectionSort_asc -> a -> a -> d -> e -> i 列表在 selectionSort_desc -> i -> l -> o -> v -> e -> d -> a -> t -> a 列表后选择Sort_desc -> v - > t -> o -> l -> i -> a
标签: java singly-linked-list selection-sort