简体   繁体   English

使用插入排序对链表进行排序

[英]Sorting a linked list using insertion sort

I have to design my own insertion sort but I'm using javas linked list. 我必须设计自己的插入排序,但是我使用的是Java链表。

private static void insertionSort( LinkedList<Integer> data, int first, int last ){
    System.out.println("insertion sort");
    ListIterator itr = data.listIterator(first+1);
    int count = first+1;
    while (count<=last){//itr.hasNext() &&  
        ListIterator itr2 = data.listIterator(itr.nextIndex()-1);
        int tmp = (Integer) itr.next();
        while( itr2.hasPrevious() && tmp < (Integer)itr2.previous()){
            itr2.next();
            itr2.set(itr2.previous());
        }
        data.set(itr2.nextIndex(), tmp);
        count++;
    }

it will eventually be sorting parts of the linked list, however for now im just passing it the whole list and trying. 它最终将对链接列表的各个部分进行排序,但是现在我只是将整个列表传递给它并进行尝试。 it seems to be inserting into the list however it dosn't move the things in the list back. 它似乎正在插入列表中,但不会将列表中的内容移回列表中。 I feel like I'm really close. 我觉得我真的很近。 does anyone have any pointers? 有人有指针吗?

I believe that in you implementation you are doing some "weird" things with the iterators, when you call set . 我相信在实现中,当您调用set时,您在使用迭代器来做一些“怪异”的事情。 The use of them for such applications is not "that" necessary, since the LinkedList class provides methods like size , get , and set . 将它们用于此类应用程序不是必需的,因为LinkedList类提供了诸如sizegetset之类的方法。

Based on what you were trying to do and looking at the insertion sort algorithm pseudocode here , I wrote this method: 根据您尝试执行的操作并在此处查看插入排序算法的伪代码,我编写了以下方法:

public static void insertionSort(LinkedList<Integer> linkedList, int first, int last) {
        for (int i = 2; i <= last; i++) {
            for (int k = i; k > first
                    && (linkedList.get(k - 1) < linkedList.get(k - 2)); k--) {
                Integer temp = linkedList.get(k - 2);
                linkedList.set(k - 2, linkedList.get(k - 1));
                linkedList.set(k - 1, temp);
            }
        }
    }

However, this method has two restrictions has two restrictions: 但是,此方法有两个限制,有两个限制:

  1. 1 <= first 1 <=首先
  2. last <= linkedList.size() 最后一个<= linkedList.size()

Examples: 例子:

Sorting the entire linked list: 排序整个链表:
Input: LL = [2, 1, 8, 6, 4, 0, 3, 9, 5, 7] 输入: LL = [2、1、8、6、4、0、3、9、5、7]
Call: insertionSort(LL, 1, ll.size()) 呼叫: insertSort(LL,1,ll.size())
Output: [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] 输出:[0,1,2,3,4,5,6,7,8,9]

Sorting the first four elements in the linked list: 对链接列表中的前四个元素进行排序:
Input: LL = [2, 1, 8, 6, 4, 0, 3, 9, 5, 7] 输入: LL = [2、1、8、6、4、0、3、9、5、7]
Call: insertionSort(LL, 1, 4) 呼叫: insertSort(LL,1,4)
Output: [ 1 , 2 , 6 , 8 , 4, 0, 3, 9, 5, 7] 输出:[1,2,6,8,4,0,3,9,5,7]

Sorting the last 2 elements in the linked list: 对链接列表中的最后2个元素进行排序:
Input: LL = [2, 1, 8, 6, 4, 0, 3, 9, 5, 7] 输入: LL = [2、1、8、6、4、0、3、9、5、7]
Call: insertionSort(LL, 9, LL.size()) 呼叫: insertSort(LL,9,LL.size())
Output: [2, 1, 8, 6, 4, 0, 3, 9, 5 , 7 ] 输出:[2,1,8,6,4,0,3,9,5,7]

Sorting 4 elements within the linked list: 在链接列表中对4个元素进行排序:
Input: LL = [2, 1, 8, 6, 4, 0, 3, 9, 5, 7] 输入: LL = [2、1、8、6、4、0、3、9、5、7]
Call: insertionSort(LL, 3, 6) 呼叫: insertSort(LL,3,6)
Output: [2, 1, 0 , 4 , 6 , 8 , 3, 9, 5, 7] 输出:[2,1,0,4,6,8,3,9,5,7]

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

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