简体   繁体   English

选择ArrayList中没有collections.sort的排序

[英]Selection Sort without collections.sort in ArrayList

So, why won't this code work it is returning the original list always (I haven't put the return statement, but can someone determine why the logic behind my selection sort algorithm is not working). 因此,为什么此代码不起作用,却总是返回原始列表(我没有放入return语句,但是有人可以确定为什么我的选择排序算法背后的逻辑不起作用)。 Any help would be gladly appreciated! 任何帮助将不胜感激!

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class ArrayListDemo {
    public static void main(String [] args) {
        ArrayList <String> list = new ArrayList <String> (15);
        list.add("Wilson");
        list.add("Annans");
        list.add("Manning");
        list.add("Branday");
        list.add("Smittens");
        list.add(2, "Goddard Fey");
        list.set((list.size()) - 1, "Brand Brandon");
        list.add("Brand Boarders");
        selectionSort(list);
    }
    static void selectionSort(ArrayList<String> a) {
         int smallindex;
         for(int i = 0; i < a.size(); i++) {
              smallindex = i; // set first element as smallest
              for(int j = i + 1; j < a.size(); j++) { // find smallest
                   if (a.get(j).compareTo(a.get(smallindex)) > 0) {
                       swap(a, i, j);
                   }

              }
         }
    }

    static void swap(ArrayList<String> a, int index1, int index2) {
         String i_string = a.get(index1);
         String j_string = a.get(index2);
         String temp = i_string;
         i_string = j_string;
         j_string = temp;
    } 
}

Your swap(ArrayList<String> a, int index1, int index2) method creates local variables - i_string and j_string - and then swaps them. 您的swap(ArrayList<String> a, int index1, int index2)方法创建局部变量i_stringj_string ,然后交换它们。 This has no impact on the contents of the input List a . 这对输入List a的内容没有影响。

To change the value of the i 'th element of the List a you must call a.set(i,newValue) . 要更改List a的第i个元素的值,必须调用a.set(i,newValue)

You can either implement the swap method yourself, or use the existing Collections.swap : 您可以自己实现swap方法,也可以使用现有的Collections.swap

static void selectionSort(ArrayList<String> a) {
    int smallindex;
    for(int i = 0; i < a.size(); i++) {
        smallindex = i; // set first element as smallest
        for(int j = i + 1; j < a.size(); j++) { // find smallest
            if (a.get(j).compareTo(a.get(smallindex)) > 0) {
                Collections.swap(a, i, j);
            }
        }
    }
}

If you wish to learn how to implement swap correctly, you can take a look at the JDK implementation: 如果您想学习如何正确实现swap ,可以看一下JDK实现:

public static void swap(List<?> list, int i, int j) {
    final List l = list;
    l.set(i, l.set(j, l.get(i)));
}

As you can see, this method sets the value returned by l.set(j, l.get(i)) to be the new i 'th element of the List . 如您所见,此方法将l.set(j, l.get(i))返回的值设置为List的第i个元素。 l.set(j, l.get(i)) sets the value l.get(i) as the new j 'th element of the List and returns the previous j 'th element. l.set(j, l.get(i))将值l.get(i)List的第j个元素,并返回先前的第j个元素。 Therefore the new i 'th element of the List becomes the previous j 'th element. 因此,新的i “中的第i个元素List变成以前的j ”个元素。

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

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