//单链表的选择排序
public class SelectSortList{
//单链表节点的定义
public static class Node{
public int value;
public Node next;
public Node(int data)
{
this.value=data;
}
}
//单链表的选择排序
public static Node selectSortList(Node head)
{
if(head==null)
{
return head;
}
Node q=head;
Node p=head.next;
int tmp=0;
while(head!=null)
{
while(p!=null)
{
if(p.value<head.value)
{
tmp=head.value;
head.value=p.value;
p.value=tmp;
}
p=p.next;
}
head=head.next;
p=head;
}
return q;
}
//打印链表的内容
public static void PrintList(Node head)
{
while(head!=null)
{
System.out.print(head.value+" ");
head=head.next;
}
System.out.println();
}
public static void main(String[]args)
{
//System.out.println("Hello");
Node node=new Node(4);
node.next=new Node(3);
node.next.next=new Node(2);
node.next.next.next=new Node(5);
PrintList(selectSortList(node));
}
}
单链表的选择排序
最新推荐文章于 2022-10-15 01:40:48 发布