here you go :
public class Main {
public static void main(String[] args) {
String[] a = new String[]{"C", "D", "G", "H", "I", "L"};
int[] b = new int[] {1, 3, 0, 4, 5, 2};
String[] res = sort(a,b);
for(int i = 0; i < a.length - 1; i++) {
System.out.println(a[i]);
}
System.out.println("Hello World!");
}
public static String[] sort(String[] a, int[] b) {
// since last one is not used we start from last one
int len = a.length;
int prevIndex = b[len - 1];
int count = 1;
while (count < len) {
int targetIndex = b[prevIndex];
a[prevIndex] = a[targetIndex];
prevIndex = targetIndex;
count++;
}
return a;
}
}
|