[英]Sorting an array and keeping original index
I am trying to make a program that will sort the array but keep track of their original indexes. 我正在尝试创建一个程序,它将对数组进行排序,但跟踪它们的原始索引。 I don't want to change the original array so I copied it into list[][]. 我不想更改原始数组,因此我将其复制到list [] []中。
public static void main(String[] args) {
int array[] = {17, 10, 8, 13, 5, 7, 8, 30};
int list[][] = new int[8][2];
int temp1, temp2, index, max;
for(int i=0;i<array.length; i++){
list[i][0]=array[i];
list[i][1]=i;
}
for(int i=0; i <array.length-1; i++){
max = list[i][0];
index = i;
for(int j = i+1; j<array.length;j++){
if(max<list[j][0]){
max = list[j][0];
index = j;
}
}
temp1 = list[i][0];
temp2 = list[i][1];
list[i][0]=max;
list[i][1] = index;
list[index][0]=temp1;
list[index][1]=temp2;
}
for(int n=0; n<list.length;n++){
System.out.println(list[n][0] + " " + list[n][1]);
}
}
So it should print: 所以它应该打印:
30 7 30 7
17 0 17 0
13 3 13 3
10 1 10 1
8 2 8 2 2
8 6 8 6
7 5 7 5
5 4 5 4
But when I run it, it prints: 但是当我运行它时,它会打印:
30 7 30 7
17 7 17 7
13 3 13 3
10 7 10 7
8 6 8 6
8 7 8 7
7 7 7 7
5 4 5 4
Any suggestions? 有什么建议么?
Why not some nice and clean OOP? 为什么不是一些漂亮干净的OOP?
class Element implements Comparable<Element> {
int index, value;
Element(int index, int value){
this.index = index;
this.value = value;
}
public int compareTo(Element e) {
return this.value - e.value;
}
}
Usage: 用法:
int array[] = {17, 10, 8, 13, 5, 7, 8, 30};
// Init the element list
List<Element> elements = new ArrayList<Element>();
for (int i = 0; i < array.length; i++) {
elements.add(new Element(i, array[i]));
}
// Sort and print
Collections.sort(elements);
Collections.reverse(elements); // If you want reverse order
for (Element element : elements) {
System.out.println(element.value + " " + element.index);
}
Personally, I perfer OOP - like in Oneiros' answer. 就个人而言,我更喜欢OOP - 就像在Oneiros的回答中一样。 Based on his answer I altered the code a tiny bit for better cohesion . 根据他的回答,我改变了代码,以获得更好的凝聚力 。
Required imports 需要进口
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Oneiros ' class - altered Oneiros的课程 - 改变了
class Element{
private int index;
private int value;
public Element(int index, int value){
this.index = index;
this.value = value;
}
public int getValue(){
return this.value;
}
@Override
public String toString(){
return value + " " + index;
}
public void print(){
System.out.println(this);
}
}
The Comparator 比较器
class NumberComparator implements Comparator<Element>{
@Override
public int compare(Element a, Element b) {
return b.getValue() - a.getValue(); // ASC: a - b or DESC: b - a
}
}
The main difference is that the Comparator is now a class itself. 主要区别在于比较器现在是一个类。 This allows for higher exchangeability, in case you want to use an other Comparator. 如果您想使用其他比较器,这可以实现更高的可交换性。
Usage 用法
public class Main {
// Holds the sorted numbers and their corresponding indexes
private static List<Element> elements = new ArrayList<>();
public static void main(String[] args) {
// Original array
int[] original = {17, 10, 8, 13, 5, 7, 8, 30};
// Add elements to list for sorting
for (int i = 0; i < original.length; i++) {
elements.add(new Element(i, original[i]));
}
// Actually sort the list
elements.sort(new NumberComparator());
// Print
elements.forEach(System.out::println);
}
}
Another way of implementing it. 实现它的另一种方式。
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class MyMath {
public static void main(String[] args) {
int array[] = { 17, 10, 8, 13, 5, 7, 8, 30 };
Map<Integer, Integer> m1= new HashMap<Integer, Integer>();
int k=0;
for(int i:array){
m1.put(k++, i);
}
m1= MyMath.sortByValues(m1);
for (int key: m1.keySet()){
System.out.println(m1.get(key) + " " + key);
}
}
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(Map.Entry<K,V> entry: entries){
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.