数组利用有序链表排序
Posted ssdut_yrp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组利用有序链表排序相关的知识,希望对你有一定的参考价值。
public class Link
public long lData;
public Link next;//reference to next Link
public Link(long lData)
this.lData=lData;
public class SortedList
private Link first;
public SortedList()
first =null;
public SortedList(Link[] linkArr)
first=null;
for(int j=0;j<linkArr.length;j++)
insert(linkArr[j]);
public void insert(Link k)
Link previous =null;
Link current=first;
while(current!=null&&k.lData>current.lData)
previous=current;
current=current.next;
if(previous==null)
first=k;
else
previous.next=k;
k.next=current;
public Link remove()
Link temp =first;
first=first.next;
return temp;
public class ListInsertionSortApp
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
int size =10;
Link[] linkArray =new Link[size];
for(int j=0;j<size;j++)
int n = (int) (Math.random()*99);
Link newLink =new Link(n);
linkArray[j]=newLink;
System.out.print("Unsorted array:");
for(int j=0;j<size;j++)
System.out.print(linkArray[j].lData+" ");
System.out.println();
SortedList theSortedList = new SortedList(linkArray);
for(int j=0;j<size;j++)
linkArray[j]=theSortedList.remove();
System.out.print("Sorted array:");
for(int j=0;j<size;j++)
System.out.print(linkArray[j].lData+" ");
以上是关于数组利用有序链表排序的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解