This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: why cross out cout make result different?


Hello,

This mailing list is for the development of GCC, not for using it.
gcc-help might be more appropriate for this kind of question, although
it doesn't seem to be GCC related.
Please do not send any follow ups to gcc@gcc.gnu.org

On Fri, 2013-08-02 at 18:25 -0700, eric lin wrote:
> 
> I have tried to copy QuickSort c++ programs:
> -----------------------------------------------
> #include <iostream>
> using namespace std;
> 
> 
> class Element
> {
> public: 
>   int getKey() const { return key;};
>   void setKey(int k) { key=k;};
> private:
>   int key;
>   // other fields
> 
> };
> 
> #define InterChange(list, i, j)  t=list[j]; list[i]=list[j]; list[j]=t;

This is probably wrong.  In your code it expands to

if (i < j)
  t = list[j];

list[i] = list[j];
list[j] = t;

Make InterChange a function instead of a macro and try again.

> /*-------------------------------------------------------------------------------------*/
> 
> 
> void QuickSort(Element list[], /* const */ int left, /*const */ int right)
> // Sort records list[left], ..., list[right] into nondescreasing order on field key.
> // Key pivot = list[left].key is arbitrarily chosen as the pivot key.  Pointer i and j
> // are used to partition the sublist so that at any time list[m].key <= pivot, m < i;
> // and list[m].key >= pivot, m>j.  It is assumed that list[left].key <=list[right+1].key.
> {
> Element t;
> 
>   if (left<right) {
>      int i = left,
>          j=right+1,
>          pivot=list[left].getKey();
>      do {
>         do i++;  while(list[i].getKey() < pivot);
>         do j--;  while(list[j].getKey() > pivot);
>         if (i<j) InterChange(list, i, j);
>       } while(i<j);
>      InterChange(list, left, j);
>  
>      cout << "---show bankaccount1[0]= " << list[0].getKey() << " bankaccount1[1]= " << list[1].getKey() <<  " bankaccount1[7]= " << list[7].getKey() << " its left= " << left << endl;
>      QuickSort(list, left, j-1);
>      QuickSort(list, j+1, right);
>   }
> }
> 
> /*--------------------------------------------------------------------------------------------*/
> 
> int main() {
>   Element bankaccount1[10];
>   int l1, r1;
> 
>   bankaccount1[0].setKey(26);
>   bankaccount1[1].setKey(5);
>   bankaccount1[2].setKey(37);
>   bankaccount1[3].setKey(1);
>   bankaccount1[4].setKey(61);
>   bankaccount1[5].setKey(11);
>   bankaccount1[6].setKey(59);
>   bankaccount1[7].setKey(15);
>   bankaccount1[8].setKey(48);
>   bankaccount1[9].setKey(19);
>   l1=0;
>   r1=9;
> 
>   for (int i=0; i<10; i++)
>     cout << bankaccount1[i].getKey() << "  " ;
>   cout << endl;
> 
>   QuickSort(bankaccount1, l1, r1);
>   for (int i=0; i<10; i++)
>     cout << bankaccount1[i].getKey() << "  " ;
>   cout << endl;
> 
> return 0;
> }
> /*---------------------------------------------------------------------------------*/
> if I (or you) commnet out cout show bankaccount1 that line, it will show different results
> both result s are not what I expected(accroding to books)
> I am in 4.6.1
> 
> _____________________________________________________________
> Luxmail.com



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]