我在互联网上找到了这段代码,它是用于数组的,我想将它改为双向链表(而不是索引,我们应该使用指针),你能帮我如何改变合并方法(我自己改变了排序方法),而且这不是我的家庭工作,我喜欢使用链表!
public class MergeSort {
private DoublyLinkedList LocalDoublyLinkedList;
public MergeSort(DoublyLinkedList list) {
LocalDoublyLinkedList = list;
}
public void sort() {
if (LocalDoublyLinkedList.size() <= 1) {
return;
}
DoublyLinkedList listOne = new DoublyLinkedList();
DoublyLinkedList listTwo = new DoublyLinkedList();
for (int x = 0; x < (LocalDoublyLinkedList.size() / 2); x++) {
listOne.add(x, LocalDoublyLinkedList.getValue(x));
}
for (int x = (LocalDoublyLinkedList.size() / 2) + 1; x < LocalDoublyLinkedList.size`(); x++) {`
listTwo.add(x, LocalDoublyLinkedList.getValue(x));
}
//Split the DoublyLinkedList again
MergeSort sort1 = new MergeSort(listOne);
MergeSort sort2 = new MergeSort(listTwo);
sort1.sort();
sort2.sort();
merge(listOne, listTwo);
}
private void merge(DoublyLinkedList a, DoublyLinkedList b) {
int x = 0;
int y = 0;
int z = 0;
while (x < first.length && y < second.length) {
if (first[x] < second[y]) {
a[z] = first[x];
x++;
} else {
a[z] = second[y];
y++;
}
z++;
}
//copy remaining elements to the tail of a[];
for (int i = x; i < first.length; i++) {
a[z] = first[i];
z++;
}
for (int i = y; i < second.length; i++) {
a[z] = second[i];
z++;
}
}
}发布于 2010-05-30 20:07:39
合并排序需要经常拆分列表。迭代到LinkedList的中间是不是你可以对它执行的最昂贵的操作(好吧,除了排序之外)?我可以看到合并步骤工作得很好(您正在两个链表上向前迭代),但是如果没有O(1)拆分操作,我不确定这个实现是否值得麻烦。
跟进
正如我所指出的,当你在合并阶段已经在做O(n)的事情时,O(n)拆分操作并不会真正增加太多的复杂性。然而,像现在这样进行迭代仍然会遇到麻烦(不是使用Iterator,而是在随机访问特性较差的List上使用get )。
在调试其他问题时,我感到很无聊,所以我写了一个我认为是这个算法的不错的Java实现。我逐字跟踪了维基百科的伪代码,并在其中添加了一些泛型和print语句。如果你有任何问题或顾虑,尽管问。
import java.util.List;
import java.util.LinkedList;
/**
* This class implements the mergesort operation, trying to stay
* as close as possible to the implementation described on the
* Wikipedia page for the algorithm. It is meant to work well
* even on lists with non-constant random-access performance (i.e.
* LinkedList), but assumes that {@code size()} and {@code get(0)}
* are both constant-time.
*
* @author jasonmp85
* @see <a href="http://en.wikipedia.org/wiki/Merge_sort">Merge sort</a>
*/
public class MergeSort {
/**
* Keeps track of the call depth for printing purposes
*/
private static int depth = 0;
/**
* Creates a list of 10 random Longs and sorts it
* using {@link #sort(List)}.
*
* Prints out the original list and the result.
*
*/
public static void main(String[] args) {
LinkedList<Long> list = new LinkedList<Long>();
for(int i = 0; i < 10; i++) {
list.add((long)(Math.random() * 100));
}
System.out.println("ORIGINAL LIST\n" +
"=================\n" +
list + "\n");
List<Long> sorted = sort(list);
System.out.println("\nFINAL LIST\n" +
"=================\n" +
sorted + "\n");
}
/**
* Performs a merge sort of the items in {@code list} and returns a
* new List.
*
* Does not make any calls to {@code List.get()} or {@code List.set()}.
*
* Prints out the steps, indented based on call depth.
*
* @param list the list to sort
*/
public static <T extends Comparable<T>> List<T> sort(List<T> list) {
depth++;
String tabs = getTabs();
System.out.println(tabs + "Sorting: " + list);
if(list.size() <= 1) {
depth--;
return list;
}
List<T> left = new LinkedList<T>();
List<T> right = new LinkedList<T>();
List<T> result = new LinkedList<T>();
int middle = list.size() / 2;
int added = 0;
for(T item: list) {
if(added++ < middle)
left.add(item);
else
right.add(item);
}
left = sort(left);
right = sort(right);
result = merge(left, right);
System.out.println(tabs + "Sorted to: " + result);
depth--;
return result;
}
/**
* Performs the oh-so-important merge step. Merges {@code left}
* and {@code right} into a new list, which is returned.
*
* @param left the left list
* @param right the right list
* @return a sorted version of the two lists' items
*/
private static <T extends Comparable<T>> List<T> merge(List<T> left,
List<T> right) {
String tabs = getTabs();
System.out.println(tabs + "Merging: " + left + " & " + right);
List<T> result = new LinkedList<T>();
while(left.size() > 0 && right.size() > 0) {
if(left.get(0).compareTo(right.get(0)) < 0)
result.add(left.remove(0));
else
result.add(right.remove(0));
}
if(left.size() > 0)
result.addAll(left);
else
result.addAll(right);
return result;
}
/**
* Returns a number of tabs based on the current call depth.
*
*/
private static String getTabs() {
StringBuffer sb = new StringBuffer("");
for(int i = 0; i < depth; i++)
sb.append('\t');
return sb.toString();
}
}要运行
运行代码将代码保存到名为MergeSort.java
javac MergeSort.java
java MergeSort
发布于 2010-05-30 20:01:26
这取决于DoublyLinkedList是什么-它是一个具体的用户定义类型,还是仅仅是一个链表类型的别名?
在第一种情况下,您应该有索引的get/set方法和/或在其中定义的迭代器,这将使任务变得简单。
在后一种情况下,为什么不使用标准的java.util.LinkedList
就List接口而言,该操作可以像这样实现:
<T> List<T> merge(List<T> first, List<T> second, List<T> merged) {
if (first.isEmpty())
merged.adAll(second);
else if (second.isEmpty())
merged.adAll(first);
else {
Iterator<T> firstIter = first.iterator();
Iterator<T> secondIter = second.iterator();
T firstElem = firstIter.next();
T secondElem = secondIter.next();
do {
if (firstElem < secondElem) {
merged.add(firstElem);
firstElem = firstIter.hasNext() ? firstIter.next() : null;
} else {
merged.add(secondElem);
secondElem = secondIter.hasNext() ? secondIter.next() : null;
}
} while (firstIter.hasNext() && secondIter.hasNext());
//copy remaining elements to the tail of merged
if (firstElem != null)
merged.add(firstElem);
if (secondElem != null)
merged.add(secondElem);
while (firstIter.hasNext()) {
merged.add(firstIter.next());
}
while (secondIter.hasNext()) {
merged.add(secondIter.next());
}
}
}这种实现比使用数组更繁琐,主要是因为迭代器被next操作“消耗”,所以必须考虑每个列表中的当前项。有了get,代码会更简单,非常类似于数组解决方案,但是对于大的列表,它会慢得多,正如@sepp2k指出的那样。
更多的注意事项:
localDoublyLinkedList发布于 2011-05-20 07:02:39
我昨天遇到了这个问题。这里有一些想法。
对DoublyLinkedList进行排序与对Array进行排序是不同的,因为不能对列表中的任意项进行基于索引的引用。相反,您需要记住每个递归步骤中的项,然后将它们传递给merge函数。对于每个递归步骤,你只需要记住每个列表的第一项。如果您不记得这些项,您很快就会得到索引,但这会导致在您的merge-function中,您需要使用for-loops遍历整个列表来查找要合并的项。这反过来意味着你得到了O(n^2)的复杂性。
另一个要点是递归到列表中并将列表一分为二的步骤。您可以使用for-loops在递归部分完成此步骤。与merge-part相反,在这一步,for-loops只会产生O(log(n) * n/2)的复杂度,而且这仍然低于整体O(n*log(n))的复杂度。原因如下:
first项和位置为n/2的项。这需要n/2步骤才能找到。
n/4,在另一半中找到n/4。总而言之,每个递归步骤的总长度是n/2.
,
- `4 * n/8` in the 3rd recursion depth
- `8 * n/16` in the 4th recursion depth, and so on...
,
log(n),在每一步中,我们执行n/2步。这等于O(log(n)*n/2)最后是一些代码:
public DoublyLinkedList mergesort(DoublyLinkedList in, int numOfElements) {
in.first = mergesort(in.first, numOfElements);
return in;
}mergeSort:
public ListElement mergesort(ListElement first, int length) {
if(length > 1) {
ListElement second = first;
for(int i=0; i<length/2; i++) {
second = second.next;
}
first = mergesort(first, length/2);
second = mergesort(second, (length+1)/2);
return merge(first, second, length);
} else {
return first;
}
}和合并:
public ListElement merge(ListElement first, ListElement second, int length) {
ListElement result = first.prev; //remember the beginning of the new list will begin after its merged
int right = 0;
for(int i=0; i<length; i++) {
if(first.getKey() <= second.getKey()) {
if(first.next == second) break; //end of first list and all items in the second list are already sorted, thus break
first = first.next;
} else {
if(right==(length+1)/2)
break; //we have merged all elements of the right list into the first list, thus break
if(second == result) result = result.prev; //special case that we are mergin the last element then the result element moves one step back.
ListElement nextSecond = second.next;
//remove second
second.prev.next = second.next;
second.next.prev = second.prev;
//insert second behind first.prev
second.prev = first.prev;
first.prev.next = second;
//insert second before first
second.next = first;
first.prev = second;
//move on to the next item in the second list
second = nextSecond;
right++;
}
}
return result.next; //return the beginning of the merged list
}使用的最大内存量也非常低(不包括列表本身)。如果我错了,请纠正我,但它应该少于400字节(在32位上)。mergeSort上的每次调用将是12字节乘以log(n)的递归深度加上merge变量的20字节,因此: 12*log(n)+20字节。
在100万个项目上测试的P.S.代码(耗时1200ms)。此外,DoublyLinkedList是一个容器,用于存储列表的第一个ListElement。
更新:我已经回答了一个关于使用相同数据结构的Quicksort的类似问题,但是与这个合并排序实现相比,它的运行速度要慢得多。以下是一些更新后的时间,以供参考:
合并排序:
1.000.000 Items: 466ms
8.300.000 Items: 5144ms1.000.000 Items: 696ms
8.300.000 Items: 8131ms请注意,计时特定于我的硬件,您可能会得到不同的结果。
https://stackoverflow.com/questions/2938495
复制相似问题