ArrayList Sorting Using a Predefined Index List

I am trying to sort an ArrayList using a predefined array of indexes. My current example uses a copy of the original ArrayList to sort and therefore does not scale for larger ArrayLists

package sortExample;

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;

public class sortExample {

    public static void main(String[] args) {

        String [] str = new String[] {"a","b","c","d"};
        ArrayList<String> arr1 = new ArrayList<String>(Arrays.asList(str));

        int [] indices = {3,1,2,0};

        ArrayList<String> arr2 = new ArrayList(arr1.size());

        for (int i = 0; i < arr1.size(); i++) {
          arr2.add("0");
        }

        int arrIndex = 0;
        for (int i : indices){
            String st = arr1.get(arrIndex);
            arr2.set(i, st); 
            arrIndex++;
        }

      System.out.println(arr1.toString());
      System.out.println(arr2.toString());
    }

}
+4
source share
4 answers

To reuse the same data see my solution:

public static void main(String[] args) {

    String[] strs = new String[]{"a", "b", "c", "d"};
    int[] indices = {3, 1, 2, 0};

    String tmp;
    for (int i = 0; i < strs.length; i++) {
        if (i != indices[i]) {
            tmp = strs[i];
            strs[i] = strs[indices[i]];
            strs[indices[i]] = tmp;

            indices[indices[i]] = indices[i];
            indices[i] = i;
        }
    }

    for (int i : indices) {
        System.out.print(i + " ");
    }
    System.out.println();
    for (String str : strs) {
        System.out.print(str + " ");
    }
}

Exit:

0 1 2 3
dbca

+2
source

. , {0,1,2,3}. Java (), ++ , Java.

    for (int i = 0; i < arr1.size(); i++) {
        if(i != indices[i]) {
            String st = arr1.get(i);
            int t = indices[i];
            int k = i;
            int j;
            while(i != (j = indices[k])){
                arr1.set(k, arr1.get(j));
                indices[k] = k;
                k = j;
            }
            arr1.set(k, st);
            indices[k] = k;
        }
    }

{3,1,2,0} 0 3. , {3 0 1 2}, st = arr1 [0], arr1 [0] = arr1 [3], arr [3] = arr1 [2], arr1 [2] = arr1 [1], arr1 [1] = st.

+2

There is a (slightly) simpler solution:

int [] indices = {3,1,2,0};
ArrayList<String> arr2 = new ArrayList<String>();

for (int i = 0; i < arr1.size(); i++) {
    arr2.add(arr1.get(indices[i]));
}
+1
source

In the example below, just use the "indexes" for the new array.

public class Sorting {
    public static void main(String[] args) {
         String [] str = new String[] {"a","b","c","d"};

            int [] indices = {3,1,2,0};

            String sorted [] = new String [str.length] ;
            int i = 0;
            for (String string : str) {
                sorted[indices[i]] = string;
                i++;
            }

            for (String string : sorted) {
                System.out.print(string + " ");
            }
    }
}

prints: dbca

+1
source

Source: https://habr.com/ru/post/1616526/


All Articles