--- In [email protected], "Brett McCoy" <[EMAIL PROTECTED]> wrote:
>
> On 10/2/07, anand <[EMAIL PROTECTED]> wrote:
> 
> > i have been trying to write a program that sorts an array for its
> > values in increasing order but generates the o/p as the sorted indices
> > of those  respective values.
> > To make my question clear here is an ex. :
> >
> > A[6]={2,6,4,6,2,2};  // array as i/p
> > B[6]={2,2,2,4,6,6};  //sorted array
> >
> > Now the expected o/p is the indices - A[o],A[4],A[5],A[2],A[1],A[3] !!
> >
> > Problem is after sorting the array (which uses swapping) the initial
> > indices are lost!
> > To avoid this i created a new array(similar to A[] & compared it
> > element-wise with sorted array to find a match & then look out for the
> > index with a match.But the place where i get stuck is discarding the
> > initially found matches!!
> >
> >
> > I hope my problem here is clear.
> 
> It's NOT in the least bit clear. You need to show the code that is
> giving you trouble -- it will illustrate the problem far more than
> just describing it. How are you sorting? Did you write your own sort
> algorithm? Are you using qsort()?
> 
> -- Brett
> ------------------------------------------------------------
> "In the rhythm of music a secret is hidden;
>     If I were to divulge it, it would overturn the world."
>                -- Jelaleddin Rumi
>

Hi Brett,
i did not write my own sort algorithm.I am using bubble sort method.
You may look at the foll. code.Here i am showing some random values of
i/p & sorted values(assume that sorted values are available as well).
This code works fine for shown i/p values but if any value repeats
more than twice then the code is not valid anymore!!

int ar[5]={24,3,7,45,7};  //unsorted array with nrs. in random order!!
int egy[5]={3,7,7,24,45};   //1,2,4,0,3 Xpected output as they are
indices of sorted array egy[k]!!!
int k,l,dis=0;
bool check;

main()
{
      for(k=0 ; k<5 ; k++)
      {
              check = false;
                    
              for(l=0; l<5  ;l++)                           
              {  
                if(egy[k] == ar[l] && l!= dis )      // "&& l!= dis"
to avoid previously printed valeus of l ""
                      { 
                             printf("\nIndex = %d",l); 
                             dis=l;                         //assign
dis=l so as to ignore this index in next run!!
                             check = true;         
                       }
                       if (check == true)                   //upon a
successful match,break the loop!!
                       break;
              }
                     
       }
              getchar();
}




I hope now things are little clear.
Thanking you in advance,
anand.

Reply via email to