简体   繁体   English

如何使用Java集合链表进行选择排序算法?

[英]How to make a selection sort algorithm using Java collection linkedlist?

I'm new to Java and I need to make a selection sort algorithm using Java LinkedList.我是 Java 新手,我需要使用 Java LinkedList 进行选择排序算法。 I tried making the insertion sort, but I can't turn it into a selection sort.我尝试进行插入排序,但无法将其转换为选择排序。 This is my selection sort code:这是我的选择排序代码:

import java.util.*;
public class select {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedList<Integer> data = new LinkedList<>();

        System.out.println("Enter total count of elements-> ");
        int num = input.nextInt();

        while(num>0){
            data.add(input.nextInt());
            num--;
        }

        System.out.println("Original data:\n" +data);

        LinkedList<Integer> sortedData=sort(data);
        System.out.println("Sorted data:\n"+sortedData);
    }
    
    public static LinkedList<Integer> sort(LinkedList<Integer> data) {
        
        ListIterator<Integer> iterator=data.listIterator();
        while (iterator.hasNext()) {
            int key=iterator.next();
            

            for(int i = 0 ;i<data.size()-1;i++) {
                for(int j = i+1; j < data.size(); j++){
                    if(data.get(j) < key){
                        int x = data.get(j);
                        int y = key;
                        swap(data, x, y);
                    }
                }
                
            }
        }
        return data;
    }

    private static void swap(LinkedList<Integer> data, int x, int y) {
        int index1 = data.indexOf(x);
        int index2 = data.indexOf(y);

        if(index1 == -1 || index2== -2){
            return;
        }
    }
}

The sorted data is always the same as the original data and I don't know which went wrong.排序后的数据总是和原始数据一样,不知道哪里出错了。

The problem seems to be that your swap method doesn't actually swap the values.问题似乎是您的swap方法实际上并未交换值。

These two lines will get the indices of your data, as you'd expect.正如您所期望的,这两行将获得您的数据的索引。

int index1 = data.indexOf(x);
int index2 = data.indexOf(y);

But all this does is ensure that your indices are valid, meaning the data were found.但这一切都是为了确保您的索引有效,这意味着找到了数据。 (Although the second check should also be -1 because the #indexOf method always returns the index, if found, or -1. Never -2.) (虽然第二次检查也应该是-1因为#indexOf方法总是返回索引,如果找到,或者 -1。永远不会 -2。)

if (index1 == -1 || index2 == -1){ // changed to -1 on both
    return;
}

To actually do the swap, you need something like at the end of the other code in your swap method:要实际进行交换,您需要在swap方法中其他代码的末尾添加类似内容:

data.set(index1, y);
data.set(index2, x);

The #set method changes the value at the first argument index to the value in the second argument, so doing it twice will effectively swap the data. #set方法将第一个参数索引处的值更改为第二个参数中的值,因此执行两次将有效地交换数据。

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

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