-3

i want to store the original index numbers of array elements before for sorting in ascending order.

assume if the elements in array are

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

i want another array to return with index number of the sorted form like

sorted[] = {0,5,1,4,2,3}

i tried many times. but can't figure out how to get the index numbers. everytime the index number gets changed after the swap.

Note:

  • i can't use comparotor,map,Arrays.sort or anything like that. i have to use loop only.
  • i already searched all the websites. i cant find any specific answer. cause all of them uses comparator map or something that i am not allowed to use at the moment.

    public static int[] sort(int[] numbers) {
        int[] sorted = new int[numbers.length];
        int temp;
        int[] index= new int[numbers.length];
        for (int i = 0; i < numbers.length; i++) {
            for (int j = i+1; j < numbers.length; j++) {
                if (numbers[i] > numbers[j]) {
                    temp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = temp;
                    index[i]= j;
                }
            }
        }
        return index;
    }
    
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
niklaus
  • 21
  • 6

3 Answers3

1

i was able to do it by storing the original array in another array and then use loop to compare the sorted array and store the index .

Thanks @RubioRic @RCInd .

niklaus
  • 21
  • 6
0

I think this is what you're looking for:

  1. keep positions in a map
  2. Sort
  3. Lookup the map for the indexes before the sorting PS: The complexity would come because of the duplicates. You need to handle that with the help of another map

See the working code here...

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.attribute.HashAttributeSet;

public class PositionFinder{


    public static void main(String[] args) {
        Integer []a = {3,2,3,0,1};
        Map <Integer , List<Integer>> map = new HashMap<>();

        for (int i = 0; i < a.length; i++) {
            int j = a[i];
            if(map.get(j)==null){
                map.put(j, new ArrayList<>());
                map.get(j).add(i);
            }
            else{
                map.get(j).add(i);
            }
        }

        System.out.println("Positions ->" + map);

        //sort
        List<Integer> l = new ArrayList<Integer>(Arrays.asList(a));

        Collections.sort(l);

        System.out.println("Sorted List -->" + l );


        //get and print index
        List positionList = new ArrayList<>();
        Map<Integer, Integer> duplicateTrackerMap = new HashMap<>();
        for (Integer key : l) {
            int pos = 0;
            if(duplicateTrackerMap.get(key) != null){
                pos = 1+ duplicateTrackerMap.get(key);

            }
            duplicateTrackerMap.put(key, pos);
            positionList.add(map.get(key).get(pos));
        }
        System.out.println("Sort index -->" +  positionList);
    }


}
Community
  • 1
  • 1
RCInd
  • 344
  • 2
  • 13
0
    int x[] = { 20, 40, 70, 80, 50, 30 };
    int[] array = new int[x.length];
    for (int i = 0; i < x.length; i++) {
        array[i] = i;
    }
    for (int i = (x.length - 1); i >= 0; i--) {
        for (int j = 1; j <= i; j++) {
            int oneMi = j - 1;
            if (x[oneMi] > x[j]) {
                int temp = x[oneMi];
                x[oneMi] = x[j];
                x[j] = temp;
                int swi = array[oneMi];
                array[oneMi] = array[j];
                array[j] = swi;
            }
        }
    }

    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
prithivraj
  • 71
  • 1
  • 12