Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Find the the original indices of the sorted aray

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Find the the original indices of the sorted aray

    My unsorted array has values with indices [0 1 2 3 ... array.length]

    2 66 23 61 38 193 31 33 37 30 21 18 36 33 61 80 16 53 6 26 2 21 5 4 8 16 7 11 10 3 1 3 1 1

    After using merge sort, my sorted array is

    1 1 1 2 2 3 3 4 5 6 7 8 10 11 16 16 18 21 21 23 26 30 31 33 33 36 37 38 53 61 61 66 80 193

    My problem is I could not get the original indices of the unsorted array in the order of my sorted array.

    i.e [30 32 33 0 ... 5]

    Regarding my code, I have set a variable pos for current position in the sorted array, ptr2 which compares every value of unsorted array.
    When the value of my sorted array is equal to the value of unsorted array, the code can get the indices then break;.

    However, I do not know what to write when the values of the array has duplicates.

    With this code I am getting the list of indices of:
    30 30 30 0 0 29 29 23 22 18 26 24 28 27 16 16 11 10 10 2 19 9 6 7 7 12 8 4 17 3 3 1 15 5

    The indices should not repeat.

     
    int[]  index=new int[sortedArr.length];
     
     
    for(int pos=0;pos<sortedArr.length;pos++)
    	{			
    		for(int ptr2=0;ptr2<sortedArr.length;ptr2++)
    	     {	
    		 if(sortedArr[pos]==notSortedArr[ptr2])
    		    {
              		  index[pos]=ptr2;	  	  
    			   break;
    		     }
    	     }		
     
    	}
     
    ///print///
    	System.out.print("indices: ");
    	for(int i=0;i<sortedArr.length;i++)
    		 System.out.print(index[i]+" ");
    Last edited by slashGamer; August 5th, 2017 at 03:50 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,845
    Thanks
    50
    Thanked 2,243 Times in 2,215 Posts

    Default Re: Find the the original indices of the sorted aray

    Can you make a small, complete program that compiles, executes and shows the problem for testing?

    Be sure to add comments to the code describing its logic.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. close original frame
    By e93 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 19th, 2014, 04:07 AM
  2. aray print
    By sainisuresh101 in forum Java Native Interface
    Replies: 1
    Last Post: February 15th, 2014, 04:36 PM
  3. Insert original title here: Hello there!
    By Leetment in forum Member Introductions
    Replies: 0
    Last Post: May 9th, 2012, 03:48 PM
  4. Replies: 1
    Last Post: March 15th, 2010, 11:03 PM