1

hello guys i need to sort some elements of integer in an integer array and need to store the index of the sorted list

assume if the elements in array are

x[]= {10,20,40,70,80,50,30};

i need to get the index of the sorted order say in this case i need to get 4,3,5,2,6,0 (ascending) (array x starting from 0)

2

5 Answers 5

4

A simple way (not algorithmically clever) would be to make a new list (or array) of objects from the existing list that contains the value and the index:

class ValueAndIndex implements Comparable<ValueAndIndex> {
    final int value;
    final int index;

    ValueAndIndex(int value, int index) {
        this.value = value;
        this.index = index;
    }

    @Override public int compareTo(ValueAndIndex other) {
       // compare on value;
        if (this.value < other.value) {
            return -1;
        } else if (this.value > other.value) {
            return 1;
        } else {
            return 0;
        }
    }
}

Now, create instances of this class in a list:

List<ValueAndIndex> secondaryList = new ArrayList<ValueAndIndex>(x.length);
for (int i = 0; i < x.length; ++i) {
    secondaryList.add(new ValueAndIndex(x[i], i));
}

Sort this list:

Collections.sort(secondaryList);

Now, the indices are still in this list:

int [] indexesInSortedOrder = new int[x.length];
for (int i = 0; i < secondaryList.size(); ++i) {
    indexesInSortedOrder[i] = secondaryList.get(i).index;
}

System.out.println(Arrays.toString(indexesInSortedOrder));
Sign up to request clarification or add additional context in comments.

Comments

0

Possible solution

 //sort the array intio a new array
 y[] = x;
 Arrays.sort(y); //sort ascending

 //final array of indexes
 int index_array[] = new int[7];

 //iteretate on x arrat
 for(int i=0; i<7; i++)
    //search the position of a value of the original x array into the sorted y array, store the position in the index array
    index_array[i] = arrays.binarySearch(x,y[i]);

2 Comments

This shouldn't be an accepted solution, it will fail for duplicate values and why limit only to the example array with length 7?
This solution will not work. 1) Since both x and y point to same memory location, both will point to sorted array after calling Arrays.sorting. 2) Lets assume array x is not sorted. But still you cannot do binary search with un sorted array.
0

you can create an array

y[] = {0,1,2,3,4,5,6};

And ith any sorting algorithm, when you switching moving two elements in array x, do the same in array y

Comments

0

One way to do (what I understand) you need:

  1. Determine the size n of the original array.
  2. Create result array R and initialize its elements with 0 . . n-1
  3. Finally implement one sort algorithm in the way that it sorts a copy(!) of your original array whilst also switching the elements in R

Example run:

    Copied  Result
    Array 
------------------
1.  2-3-1   0-1-2
2.  2-1-3   0-2-1
3.  1-2-3   2-0-1

Comments

0
public Map sortDecendingDFSGlobal() {
    Map<String, Object> multiValues = new HashMap<String, Object>();

    double[] dfs = this.global_dfs;
    int[] index = new int[dfs.length];

    for (int i = 0; i < dfs.length; i++) {
        index[i] = i;//for required indexing
    }
    for (int i = 0; i < dfs.length; i++) {
        //sorting dfsglobal in decending order
        double temp = dfs[i];
        double swap = dfs[i];
        int swapIndex = i;

        //keeping track of changing indexing during sorting of dfsglobal
        int indStart = index[i];
        int indSwap = index[i];
        int number = i;
        for (int j = i; j < dfs.length; j++) {
            if (temp < dfs[j]) {
                temp = dfs[j];
                swapIndex = j;

                indSwap = index[j];
                number = j;
            }
        }
        dfs[i] = temp;
        dfs[swapIndex] = swap;

        index[i] = indSwap;
        index[number] = indStart;
    }
    //again sorting the index matrix for exact indexing
    for (int i = 0; i < index.length - 1; i++) {
       for(int j = i; j < index.length - 1; j++ )
       {               

           if(dfs[j] == dfs[j + 1] && index[j] > index[j + 1])
           {
               int temp = index[j];
               index[j] = index[j+1];
               index[j + 1] = temp;
           }
       }
    }

    this.sortedDFS = dfs;
    this.arrIndex = index;

    multiValues.put("sorted", dfs);
    multiValues.put("index", index);
    return multiValues;
} //SortedDecendingDFSGlobal()

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.