10 June 2018

change the order like an given order array with out using new array

i.p:     array={5,342,2334,323,67,8}
          order={5,1,3,2,4,0}
o.p:    array={8  342  323  2334  67  5 }
      

import java.util.*;

class GFG {
      
   
    void print(int ar[],int i)
        {
            System.out.print(ar[i]+" ");
            if(i<ar.length-1)
            print(ar,++i);
        }
        

//stack array allocation performing below function
    void reorder(int i,int ar[],int br[])
        {
              int n=ar[br[i]];
               if(i<ar.length-1)
               reorder(1+i,ar,br);
               ar[i]=n;   //here stack from reverse
        }
       
    public static void main(String[] args)
        {
       GFG ob=new GFG();
       int ar[]={5,342,2334,323,67,8};
       int br[]={5,1,3,2,4,0};
       ob.reorder(0,ar,br);
       ob.print(ar,0);
        }
       
}

No comments:

Post a Comment