I want to write a program which take input and save the poistions of the array elements in another array.
let if i enter an array
A[0]= 4
A[1]=1
A[2]=2
A[3]=3
now the output array will hold
1
2
3
0
i-e the posistions of the array elements starting from the smallest number.
As 1 is the smallest number and it is on 1 posistion the output array will hold values like this
B[0]=1 //Posistion of the smallest number
B[1]=2 //Posistion of second smallest number
B[2]=3 //and so on
B[3]=0 //as 4 is on 0 posistion so it will be on this location of out //array.
This is the problem any one to solve it.

It seems as if the simplest way to affect a solution for your scenario would be to implement any of the available sorts, focusing on array index rather than array content.

That's a better description of the project, but you could have posted it in your original thread.

As I see it the trick is to keep track of the original element indexes after you have sorted the array. I can think of two ways to do that.

First, copy the original array, sort the copied array, then loop through the original array to find the index of the element with the same value as the current element in the sorted array and place it in a third array containing the indexes in the original . Sort of a mapping of values in the sorted array to indexes in the original array.

Second option, create a struct holding the value and the index of each element in the original array, make an array of struct and sort it based on value, then display it using the indexes stored in each struct object.

Here's a sample code..The rest is yours..

void swap(int items[], int i1, int i2)
(
   int t = 0;
   t=items[i1];
   items[i1]=items[i2];
   items[i2]=t;
)
int main()
{
   int n=0,m=0;
   const int s=4;
   int num[s]={4,1,2,3};
   for(;n<s;n++)
      std::cout<<num[n]<<std::endl;
   for(n=s-1;n>0;n--)
      for(m=0;m<n;m++)
         if(num[m]>num[m+1])
            swap(num,m,m+1);
   num[s-1]=0;
   std::cout<<std::endl;
   for(n=0;n<s;n++)
      std::cout<<num[n]<<std::endl;
   return 0;
}

Here's a sample code..The rest is yours..

void swap(int items[], int i1, int i2)
(
   int t = 0;
   t=items[i1];
   items[i1]=items[i2];
   items[i2]=t;
)
int main()
{
   int n=0,m=0;
   const int s=4;
   int num[s]={4,1,2,3};
   int old[s]={4,1,2,3};

   for(;n<s;n++)
      std::cout<<num[n]<<std::endl;
   for(n=s-1;n>0;n--)
      for(m=0;m<n;m++)
         if(num[m]>num[m+1])
            swap(num,m,m+1);
   
   std::cout<<std::endl;
   for(n=0;n<s;n++)
      std::cout<<num[n]<<std::endl;

   //...A few aid
   for x=0 to s-1
      for y=0 to s-1
         if num[x] = old[y] then
             the old pos=y

   return 0;
 //done
}

By Just Skimming through your Problem statement one can judge its a sorting problem, what you need to do is to analyze which sort will meets your need, if you've small array and planty of space then the following trivial algorithm will meet your requirements. But this trivial algorithm only requires unique element.

Algorithm.
i) Find the MAX of Input Array in O(n) Time.
ii) Allocate the Large Enough Array and Initialize it with -1.
iii) Loop Through the Input Array and increment the counter of output array positions which equates to input_array value.
iv) Print the Ouput Array (where value is not negative).

again this is a tentative solution, you can think of another, I'll prefer if you check the Thomas H Corman Sorting Techniques, which suites your requirement, if you really want to learn the sorting techniques & mathematically inclined you should check "The Art of Computer Programming" By Knuth.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.