c - Implement Bubble Sort using a Pointer to Structure -


the structure is

   struct student {     char name[10];     int roll;     int percent; };  struct student s[5]; struct student *p; 

the above declaration global. , bubble sort function.

    void sort(int n)     {     int i,j,k;     struct student temp;     for(i=0;i<=(n-2);i++)     {         for(j=0;j<(n-i-1);j++){         if((p+j)->percent>(p+j+1)->percent){ //define temp variable of structure type         temp=*(p+j);//interchange s[j] , s[j+1]         *(p+j)=*(p+j+1);         *(p+j)=temp;             }         }     } 

i want avoid using dot operator access elements of structure temp variable swapping has been declared of structure type. bubble sort function not working. these lines think messed up. please point out mistake.

if((p+j)->percent>(p+j+1)->percent){             temp=*(p+j);//interchange s[j] , s[j+1]             *(p+j)=*(p+j+1);             *(p+j)=temp; 

the swap logic wrong, first set temp *(p+j), set *(p+j) *(p+j+1) make mistake , write on *(p+j) again. believe changing

*(p+j)=temp; 

to

*(p+j+1)=temp; 

should fix


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -