简体   繁体   English

Java链表字符串排序算法

[英]java linkedlist string sort algorithm

So, I have several lists of word pairs and I need to sort them in ascending or descending order. 因此,我有几个单词对列表,我需要按升序或降序对其进行排序。 The method I'm using right now is the insertion sort algorithm. 我现在使用的方法是插入排序算法。 And this seems to work fine for the smaller lists. 这似乎适用于较小的列表。 But every time I try to sort a large list, it freezes, with no errors. 但是每次我尝试对大型列表进行排序时,它都会冻结,并且没有错误。 I tried to see what was going on by debugging with printing out "a was swapped for b" and you can see it start working and it slows down and eventually stops like the computer just said, "there's too many, I give up". 我试图通过调试打印出“ a被b交换”来查看发生了什么,您可以看到它开始工作并且变慢并最终停止,就像计算机刚刚说的那样:“太多了,我放弃了”。 My question is, is there something wrong with my code, or do I simply need to use a more efficient method, and if so, which one and what would it look like? 我的问题是,我的代码是否有问题,还是我只需要使用一种更有效的方法?如果是,那是哪种方法?

for (int j=0; j < wordpair_list.size()-1; j++){
    for (int i=0; i < wordpair_list.size()-1; i++){
        String wordA_1 = wordpair_list.get(i).getWordA();
        String wordA_2 = wordpair_list.get(i+1).getWordA();

        if (wordA_1.compareToIgnoreCase(wordA_2) < 0){
            WordPair temp = wordpair_list.get(i);
            wordpair_list.set(i,wordpair_list.get(i+1));
            wordpair_list.set(i+1, temp);           
        }
    }
}

that's for descending. 那是为了下降。 all i do for ascending is swap the '>' in the if statement to '<' 我要做的就是将if语句中的'>'交换为'<'

I think you are performing bubble sort. 我认为您正在执行气泡排序。 As others have pointed out, performing get() and set() operations are expensive with linked lists. 正如其他人指出的那样,使用链表执行get()和set()操作非常昂贵。

I am not conversant with Java, but it appears that you can use ListIterators to carry out bubble sort in O(N^2) 我不熟悉Java,但是似乎可以使用ListIterators在O(N ^ 2)中进行冒泡排序

ListIterator listIterator(int index) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. ListIterator listIterator(int index)从列表中的指定位置开始(按适当顺序)返回此列表中元素的列表迭代器。 Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()). 如果指定的索引超出范围,则抛出IndexOutOfBoundsException(索引<0 ||索引> = size())。

For bubble sort, you just need to swap the adjacent elements, so you can iterate through the list like an array and keep swapping if needed. 对于冒泡排序,您只需要交换相邻的元素,因此可以像数组一样遍历列表,并在需要时保持交换状态。

Moreover, you can skip the section of the list that is already sorted. 此外,您可以跳过列表中已排序的部分。 Take a look at a good bubble sort algorithm. 看一个好的冒泡排序算法。 http://en.wikipedia.org/wiki/Bubble_sort http://en.wikipedia.org/wiki/Bubble_sort

First of all this is not insert sort, this is closer to bubble sort. 首先,这不是插入排序,这更接近气泡排序。 Second, there is nothing wrong with your code, but as expected for this sorting algorithm it is quadratic. 其次,您的代码没有错,但是对于这种排序算法,它是二次方的。 Thus for larger input it may take a long time to finish(eg for 200 000 elements it may take several minutes). 因此,对于较大的输入,可能需要很长时间才能完成(例如,对于20万个元素,可能需要几分钟)。

EDIT: as you are using List the complexity is even higher - up to cubic. 编辑:当您使用列表时,复杂度甚至更高-高达立方。 As set in a list is not constant. 列表中set的值不是恒定的。 You may try to implement the algorithm with Array to save this added complexity. 您可以尝试使用Array来实现该算法,以节省这种增加的复杂性。

Besides the fact that insertion sort is already O(N^2) algorithm, the access (both get and set) to the item in the linked list by the item index is O(N) operation, making your code O(N^3), in other words extremely slow . 除了插入排序已经是O(N ^ 2)算法这一事实之外,通过项索引对链表中的项的访问(获取和设置)都是O(N)操作,从而使代码为O(N ^ 3) ),也就是说速度非常慢

Basically, you have two options: 基本上,您有两种选择:

  • copy the linked list into a temp array, sort an array using your algorithm (array access by index is O(1), overall algorithm will stay at O(N^2) (unless you choose a better one), create a new sorted linked list from the array. 将链接列表复制到临时数组中,使用算法对数组进行排序(按索引对数组的访问为O(1),总体算法将保持在O(N ^ 2)(除非您选择更好的算法),然后创建新的排序后的数组数组中的链表。
  • use some other algorithm that does not need indexed access (for example, it is actually possible to implement insertion sort without indexed operations because you can swap two adjacent items in the linked list, through you will have to use a linked list implementation where you have direct access to the "previous" and "next" links). 使用其他不需要索引访问的算法(例如,实际上可以在没有索引操作的情况下实现插入排序,因为您可以交换链表中的两个相邻项,因为您必须使用链表实现直接访问“上一个”和“下一个”链接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM