Java如何对链表进行排序?
我需要按字母顺序对链表进行排序。我有一个乘客姓名的链接列表,需要乘客姓名按字母顺序排序。你会怎么做?有人有参考资料或视频吗?您可以使用它按字母顺序排序。要按字母顺序排序字符串,您需要使用Java如何对链表进行排序?,java,linked-list,Java,Linked List,我需要按字母顺序对链表进行排序。我有一个乘客姓名的链接列表,需要乘客姓名按字母顺序排序。你会怎么做?有人有参考资料或视频吗?您可以使用它按字母顺序排序。要按字母顺序排序字符串,您需要使用排序器,例如: LinkedList<String> list = new LinkedList<String>(); list.add("abc"); list.add("Bcd"); list.add("aAb"); Collections.sort(list, new Co
排序器,例如:
LinkedList<String> list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Collator.getInstance().compare(o1, o2);
}
});
LinkedList=新建LinkedList();
列表。添加(“abc”);
列表。添加(“Bcd”);
列表。添加(“aAb”);
Collections.sort(list,newcomparator(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回Collator.getInstance().compare(o1,o2);
}
});
因为如果您只调用Collections.sort(list)
,您将无法处理包含大写字符的字符串
例如,在我粘贴的代码中,排序后列表将是:[aAb,abc,Bcd]
但是如果您只调用Collections.sort(list)代码>您将获得:[Bcd、aAb、abc]
注意:使用Collator
时,您可以指定区域设置Collator.getInstance(locale.ENGLISH)
这通常非常方便。如果您想知道如何在不使用标准Java库的情况下对链表排序,我建议您自己看看不同的算法。示例显示了如何实现插入排序,另一篇StackOverflow文章显示了一个,甚至给出了一些示例,说明了如何创建自定义比较函数,以防您想进一步自定义排序。我不这么认为。我将使用带有比较器的ArrayList或排序集合。对LinkedList进行排序是我能想到的最低效的过程。在java8中,您不再需要使用Collections.sort方法,因为LinkedList继承了java.util.List中的方法sort,所以将Fido的答案改编为java8:
LinkedList<String>list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
list.sort( new Comparator<String>(){
@Override
public int compare(String o1,String o2){
return Collator.getInstance().compare(o1,o2);
}
});
LinkedList=newLinkedList();
列表。添加(“abc”);
列表。添加(“Bcd”);
列表。添加(“aAb”);
list.sort(新的比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回Collator.getInstance().compare(o1,o2);
}
});
参考资料:
下面是一个示例,可以在不使用任何标准java库的情况下对java中实现的链表进行排序
package SelFrDemo;
class NodeeSort {
Object value;
NodeeSort next;
NodeeSort(Object val) {
value = val;
next = null;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public NodeeSort getNext() {
return next;
}
public void setNext(NodeeSort next) {
this.next = next;
}
}
public class SortLinkList {
NodeeSort head;
int size = 0;
NodeeSort add(Object val) {
// TODO Auto-generated method stub
if (head == null) {
NodeeSort nodee = new NodeeSort(val);
head = nodee;
size++;
return head;
}
NodeeSort temp = head;
while (temp.next != null) {
temp = temp.next;
}
NodeeSort newNode = new NodeeSort(val);
temp.setNext(newNode);
newNode.setNext(null);
size++;
return head;
}
NodeeSort sort(NodeeSort nodeSort) {
for (int i = size - 1; i >= 1; i--) {
NodeeSort finalval = nodeSort;
NodeeSort tempNode = nodeSort;
for (int j = 0; j < i; j++) {
int val1 = (int) nodeSort.value;
NodeeSort nextnode = nodeSort.next;
int val2 = (int) nextnode.value;
if (val1 > val2) {
if (nodeSort.next.next != null) {
NodeeSort CurrentNext = nodeSort.next.next;
nextnode.next = nodeSort;
nextnode.next.next = CurrentNext;
if (j == 0) {
finalval = nextnode;
} else
nodeSort = nextnode;
for (int l = 1; l < j; l++) {
tempNode = tempNode.next;
}
if (j != 0) {
tempNode.next = nextnode;
nodeSort = tempNode;
}
} else if (nodeSort.next.next == null) {
nextnode.next = nodeSort;
nextnode.next.next = null;
for (int l = 1; l < j; l++) {
tempNode = tempNode.next;
}
tempNode.next = nextnode;
nextnode = tempNode;
nodeSort = tempNode;
}
} else
nodeSort = tempNode;
nodeSort = finalval;
tempNode = nodeSort;
for (int k = 0; k <= j && j < i - 1; k++) {
nodeSort = nodeSort.next;
}
}
}
return nodeSort;
}
public static void main(String[] args) {
SortLinkList objsort = new SortLinkList();
NodeeSort nl1 = objsort.add(9);
NodeeSort nl2 = objsort.add(71);
NodeeSort nl3 = objsort.add(6);
NodeeSort nl4 = objsort.add(81);
NodeeSort nl5 = objsort.add(2);
NodeeSort NodeSort = nl5;
NodeeSort finalsort = objsort.sort(NodeSort);
while (finalsort != null) {
System.out.println(finalsort.getValue());
finalsort = finalsort.getNext();
}
}
}
package SelFrDemo;
类节点排序{
目标价值;
nodeeshort下一步;
节点排序(对象值){
值=val;
next=null;
}
公共对象getValue(){
返回值;
}
公共无效设置值(对象值){
这个值=值;
}
公共节点排序getNext(){
下一步返回;
}
公共void setNext(NodeeSort next){
this.next=next;
}
}
公共类排序链接列表{
头颅;
int size=0;
节点排序添加(对象值){
//TODO自动生成的方法存根
if(head==null){
NodeeSort nodee=新的NodeeSort(val);
头=节点;
大小++;
回流头;
}
节点排序温度=磁头;
while(temp.next!=null){
温度=下一个温度;
}
NodeeSort newNode=新NodeeSort(val);
临时设置下一步(新节点);
newNode.setNext(空);
大小++;
回流头;
}
nodeSort排序(nodeSort nodeSort){
对于(int i=size-1;i>=1;i--){
NodeeSort finalval=nodeSort;
nodeSort tempNode=nodeSort;
对于(int j=0;jval2){
if(nodeSort.next.next!=null){
nodeSort CurrentNext=nodeSort.next.next;
nextnode.next=nodeSort;
nextnode.next.next=CurrentNext;
如果(j==0){
finalval=nextnode;
}否则
nodeSort=nextnode;
对于(int l=1;l节点合并排序(节点头){
if(head==null | | head.next==null){
回流头;
}
节点中间=中间元素(头部);
节点nextofMiddle=middle.next;
middle.next=null;
节点左=合并排序(头);
节点右侧=合并排序(nextofMiddle);
节点sortdList=merge(左、右);
返回排序列表;
}
节点合并(节点左侧、节点右侧){
if(left==null){
返还权;
}
if(right==null){
左转;
}
节点温度=null;
如果(左数据<右数据){
温度=左;
temp.next=合并(左、下、右);
}否则{
温度=右;
temp.next=合并(左、右、下一步);
}
返回温度;
}
节点中间元素(节点头){
节点慢=头部;
节点快=头;
while(fast!=null&&fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}
返回缓慢;
}
自JAVA 8以来的优雅解决方案:
你可以
Node mergeSort(Node head) {
if(head == null || head.next == null) {
return head;
}
Node middle = middleElement(head);
Node nextofMiddle = middle.next;
middle.next = null;
Node left = mergeSort(head);
Node right = mergeSort(nextofMiddle);
Node sortdList = merge(left, right);
return sortdList;
}
Node merge(Node left, Node right) {
if(left == null) {
return right;
}
if(right == null) {
return left;
}
Node temp = null;
if(left.data < right.data) {
temp = left;
temp.next = merge(left.next, right);
} else {
temp = right;
temp.next = merge(left, right.next);
}
return temp;
}
Node middleElement(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
LinkedList<String>list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
list.sort(String::compareToIgnoreCase);
list.sort((o1, o2) -> o1.compareToIgnoreCase(o2));
LinkedList<String> list=new LinkedList<String>();
list.add("bgh");
list.add("asd");
list.add("new");
//lambda expression
list.sort((a,b)->a.compareTo(b));