1

How Can I have index of a sorted array in Java?

For instance:

  int[] myIntArray = new int[]{20,100,69,4};

and Arrays.sort(myIntArray) result is:

{4,20,69,100}

What if , index of original array is desired?

which means :

{3,0,2,1}

3
  • 3
    Indexes are constant. Value changes. And please let us know ,what is the actual problem you are facing. Commented Mar 4, 2014 at 5:17
  • I am looking for a function which returns index of an original matrix . let say: Arrays.order(myIntArray)={3,0,2,1} Commented Mar 4, 2014 at 5:20
  • maintain a copy of the original array, this would be less overhead than creating a wrapper class. Just call array.clone(). Commented Mar 4, 2014 at 5:27

5 Answers 5

3

If I understood your problem correctly.

Considering there is no duplicates in you array. After you sort the array.

OPTION 1:

Writing a small helper method.

Passing each value to the below method and getting it's index.

public int findIndex(int[] iarray, int value) {
    for(int i=0; i<iarray.length; i++) 
         if(iarray[i] == value)
             return i;
}

OPTION 2:

Use org.apache.commons.lang Class ArrayUtils

public static int indexOf(int[] array,
                          int valueToFind)

Then,

If you are wanting to store these index in an array, take an array with initial array length and fill that array with returned index.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for being simple, but if there are any repeating values in the array, it will always return the first index the value appeared at
@mdewitt : for the scenario u mentioned, i assume OP has to be smart enuf to traverse the whole array and maintain the index position of the found values!! :)
@mdewitt That's worth mentioning.
2

Declare a class that contains the index and the value, and sets up a comparator that compares just the values.

class IndexAndValue implements Comparable<IndexAndValue> {
    public int index;
    public int value;
    public IndexAndValue(int index, int value) {
        this.index = index;
        this.value = value;
    }
    @Override
    public int compareTo(IndexAndValue other) {
        return Integer.compareTo(value, other.value);
    }
}

Build an array of IndexAndValue of objects that contain the index and value.

IndexAndValue[] myIVArray = new IndexAndValue[myIntArray.length];
for (int i = 0; i < myIntArray.length, i++)
    myIVArray[i] = new IndexAndValue(i, myIntArray[i]);

Now when you call sort on myIVArray, it will use the comparator to sort, which means it will compare the values. But the resulting array contains the IndexAndValue objects, which will keep the indexes together with the values.

(P.S. not tested, so it's possible I botched some syntax...)

(PPS. Public data members are usually evil, but I think it's OK for a small class like this whose purpose is just to hold a few values together. But if you don't like it, make them private and use accessor methods.)

2 Comments

+1 because this deals with returning the correct original index for repeated values in the array
How can I make IndexAndValue ? Build an array of IndexAndValue of objects that contain the index and value. ??
2

I think a way to do this would be to create a class "intWithIndex" implementing Comparable. In that ways you could jeep track of the index.

public class IntWithIndex implements Comparable<IntWithIndex>{
  public int value;
  public int index;

  @Override
  Public int compareTo(intWithIndex x) {
    return value - x.value;
  }
}

public Test {
  IntWithIndex[] myArray = ...
  myArray[].sort;
}

IntWithIndex[].sort should then work, and you can check the initial indices with the index field.

Do you see what I mean ?

1 Comment

No :( more explanation please
0

Additional to @SURESH ATTA, you can use Map to define your data structure, the key would be save as index and value would be your integers.

Comments

0

To get indexArray of sorted value without using any lib. Also take care of duplicate/multiple array element.

import java.util.Arrays;

public class IndexCal {

    public static void main(String args[]) {
        int[] myIntArray = new int[] { 20, 100, 69, 4, 20, 4, 100 };
        int[] copyArray = new int[myIntArray.length];
        System.arraycopy(myIntArray, 0, copyArray, 0, myIntArray.length);
        Arrays.sort(myIntArray);
        int[] indexArray = new int[myIntArray.length];
        Arrays.fill(indexArray, -1);
        for (int i = 0; i < copyArray.length; i++) {
            int skiplength = 0;
            int index = find(copyArray, myIntArray[i], 0);
            while(find(indexArray, index, 0) != -1){
                index = find(copyArray, myIntArray[i], skiplength++);
            }
            indexArray[i] = index;

        }

        for (int i = 0; i < copyArray.length; i++) {
            System.out.println(indexArray[i]);
        }

    }

    public static int find(int[] array, int value, int skiplength) {
        for (int i = 0; i < array.length; i++)
            if (array[i] == value)
                if(skiplength == 0)
                    return i;
                else
                    skiplength--;

        return -1;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.