Question

I need select 10 smallest numbers from array(with 2 000 items) and print their indexes.

At first I tried just sorted this array and print values array[0 to 9]. It was the smallest numbers but I lost indexes of this values, which they had i non-sorted array.

Second option was tried use treeMap which works well, but when I have two equal keys it print only one of them, but I need print both of them.

Example of use code with treeMap:

  TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();

  treemap.put(2, "two");
  treemap.put(1, "one");
  treemap.put(3, "three");
  treemap.put(6, "six");
  treemap.put(6, "six2");
  treemap.put(5, "five");      

  Collection<String> coll=treemap.values();
  System.out.println("Value of the collection: "+coll);  

Until now I don't use treeMap, so is possible, that exist some easy way how fix it. Or is better use something else?

I will be grateful for any help

Was it helpful?

Solution

It was the smallest numbers but I lost indexes of this values, which they had i non-sorted array

Then why don't you just create a class to hold this indexes ? Then just sort your array by value and you'll get the index associated.

class MyClass implements Comparable<MyClass>{
    private int index;
    private int value;

    public MyClass(int i, int v){
       this.index = i;
       this.value = v;
    }

    @Override
    public String toString(){
        return "Index: "+index+" Value: "+value;
    }

    @Override
    public int compareTo(MyClass m) {
        return value - m.value;
    }
}

public static void main(String[] args){   
    MyClass[] array = new MyClass[20];

    for(int i = 0; i < array.length; i++){
       array[i] = new MyClass(i, someRandomValue); // Here I used (i*3 + 2)%5
    }
    System.out.println(Arrays.toString(array));
    Arrays.sort(array);
    MyClass [] arraySorted = Arrays.copyOfRange(array, 0, 10); //take the first ten elements
    System.out.println(Arrays.toString(arraySorted));
}


Note :

If you want to sort objects that have the same value by indexes, you can modify your comparator like this :

@Override
public int compareTo(MyClass m) {
    int compareValue = value - m.value;
    if(compareValue == 0)
        return index - m.index;
    return compareValue;
}


Output (with the second compareTo method) :

Before :
[Index: 0 Value: 2, Index: 1 Value: 0, Index: 2 Value: 3, Index: 3 Value: 1, Index: 4 Value: 4, Index: 5 Value: 2, Index: 6 Value: 0, Index: 7 Value: 3, Index: 8 Value: 1, Index: 9 Value: 4, Index: 10 Value: 2, Index: 11 Value: 0, Index: 12 Value: 3, Index: 13 Value: 1, Index: 14 Value: 4]

After :
[Index: 1 Value: 0, Index: 6 Value: 0, Index: 11 Value: 0, Index: 3 Value: 1, Index: 8 Value: 1, Index: 13 Value: 1, Index: 0 Value: 2, Index: 5 Value: 2, Index: 10 Value: 2, Index: 2 Value: 3]

OTHER TIPS

Instead of sorting bare values, sort (value, index) pairs, in respect to the value. Then, you just take first 10 pairs and you have 10 origin indices.

For example, you want to sort:

6 2 7 4

Making (value, index) pairs:

(6, 0) (2, 1) (7, 2) (4, 3) 

Sorting in respect to value:

(2, 1) (4, 3) (6, 0) (7, 2) 

Indices:

1 3 0 2

Your approach with TreeMap is fine. The only modification you have to make looks like this:

class Pair implements Comparable<Pair> {
    int value;
    int index;

    @Override
    int compareTo(Pair other) { return value - other.value };
}

Map<Pair, String> map = new TreeMap<Pair, String>();

Create a simple class to hold two ints, or use generics for any value:

class Item<T> { 
    public int index;
    public T value;
    public Item(int index, T value) {
        this.index = index;
        this.value = value;
    }
}

And make use of Collections.sort.

double[] original = { 1.0, 7.0, 3.0, -1.0, 5.0 };

List<Item> l = new ArrayList<Item<Double>>();
for (int i = 0; i < original.length; i++)
    l.add(new Item<Double>(i, original[i]));

Collections.sort(l, new Comparator<Item<Double>>() {
    public int compare(Item o1, Item o2) {
        return Double.compare(o1.value, o2.value);
    }
});

for (int i = 0; i < 10 && i < l.size(); i++) {
    System.out.printf("index: %f, value: %f%n", 
              l.get(i).index, l.get(i).value);
}

Output:

index: 3, value: -1
index: 0, value: 1
index: 2, value: 3
index: 4, value: 5
index: 1, value: 7

You could do this with a slightly modified Quick Sort, while sorting the array if you exchange the index array as well it would do the trick

public class Quicksort 
{
    private int[] numbers;
    private int number;

    public void sort(int[] values) {
        if (values ==null || values.length==0){
           return;
        }
        this.numbers = values;
        number = values.length;
        quicksort(0, number - 1);
    }

    private void quicksort(int low, int high) {
        int i = low, j = high;
        int pivot = numbers[low + (high-low)/2];

        while (i <= j) {
            while (numbers[i] < pivot) {
                i++;
            }
            while (numbers[j] > pivot) {
               j--;
            }

            if (i <= j) {
                exchange(i, j);
                i++;
                j--;
            }
        }

        if (low < j)
            quicksort(low, j);
        if (i < high)
            quicksort(i, high);
    }

This is where we make the change

    private void exchange(int i, int j) {
    int temp = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = temp;

    // NOTICE THIS exchange the indexes as well
    temp = index_numbers[i];
    index_numbers[i] = index_numbers[j];
    index_numbers[j] = temp;
    }
}

short solution:

  1. Create array of indexes (simple for loop to initialize).

  2. Sort that index array with custom compare function, which compares values for the index in the other array. Note: use a stable sort, if you want to print indexes in order.

  3. Iterate the sorted index array, print indexes and count distinct values (increase counter when value changes), stop when counter becomes 11.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top