Thread: how tofind no.of comparisons&exchange

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    33

    how tofind no.of comparisons&exchange

    hello i hav an assignment to sort about 10000 numbers and to find no.ofexcahange and no.of comparisons in the sort.the data is to be sorted by selection,insertion and bubble sort...the data is sorted correctly but no.of exchange and comparisons are same which is not possible..plzz help me..

    Code:
    //insertion
     int insertion1(int d1[10000])
    {
        int temp,i,excount=0,comcount=0;;
        int b[10000];
      
        d1[0]=-32767;  //assigning sentinel value
      
             
        for (int i=2;i<=10000;i++)
        {
            temp=d1[i];
            
        int j=i-1;
            
            while ((d1[j]>temp)&& comcount++)
            {
                                     
                  d1[j+1]=d1[j];
                  j--;
                  excount++;
            }
            
          d1[j+1]=temp;
       }                                     
    
    cout<<"Number of exchange are:"<<excount<<"\n";
    cout<<"Number of comparisons are:"<<comcount<<"\n";
    
    
    return 0;
    }
    
    
    
    //bubble
    
    int bubble1(int f1[10000])
    {
        int temp;
        int swapped;
        int excount=0,comcount=0;;
        
        
        for (int i=0;i<10000;i++)
        {
           swapped=0;
            
            for (int j=0;j<10000;j++)
            {
                 if ((f1[j]>f1[j+1]) && comcount++)
                {
                     {temp=f1[j];
                     f1[j]=f1[j+1];
                     f1[j+1]=temp;
                     excount++;
                     swapped=1;}
                     
                }
          
            }
         if (!swapped)
         break;
                          
        }
    
         
        
    
    cout<<"Number of exchange are:"<<excount<<"\n";
    cout<<"Number of comparisons are:"<<comcount<<"\n";
    
    return 0;
    }
    
    
    //selection
    
    
    int selection1(int e1[10000])
    {
        int i,excount=0,comcount=0;
    
    int min;
    
    for( int i=0;i<10000;i++)
    {
         min=i;
         
         for (int j=i+1;j<10000;j++)
         
         {
             if ((e1[min]>e1[j])&& comcount++)
             {
                           
                   min=j;
                   excount++;
                      
             }   
         
         }
             
                 int temp=a1[i];
                     a1[i]=a1[min];
                     a1[min]=temp;
                    
                      
           
         
    
    }
    
    
                              
    
    cout<<"Number of exchange are:"<<excount<<"\n";
    cout<<"Number of comparisons are:"<<comcount<<"\n";
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The following line is bad:
    Code:
    while ((d1[j]>temp)&& comcount++)
    comcount++ will only execute when d1[j]>temp, which is not what you'll want. Also, the body of the while loop won't execute the first time, as comcount++ == 0 == false.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    33
    thanx bro i hope i understand wht u r saying n change the code too!!

Popular pages Recent additions subscribe to a feed