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}
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}
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.
traverse the whole array and maintain the index position of the found values!! :)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.)
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 ?
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.
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;
}
}