Close
Close

Sir,how to write program for bubble sort without using temporary variable?

   Srujana C , Dr.AIT

bubble sort program in c without using temporary variable/third variable?


Answers

  •   

    Take a look at https://www.codesdope.com/blog/article/sorting-an-array-using-bubble-sort-in-c/

    Edit

    Swapping two variables without using the third one:

    #include <stdio.h>
    
    int main()
    
    {
        int x = 10, y = 15;
        x = y-x; // x is changed
        y = y-x; // value of y is change to initial value of x.
        /*
        Let's say initial value of x to be X
        y=y-x => y=y-(y-X) => y=X
        y is changed to initial value of x
    
        Let's say initial value of y to be Y
        Now, x(new value) = Y-X
        y(new Value) = X
        x+y = Y-X+X = Y
        */
        x = x+y;
        printf("x = %d\ny = %d\n",x,y);
        return 0;
    }
    

     


    • But sir,there 'temp' is used to swap right? can we code without using a temporary variable?
      - Srujana C
    • I have edited the answer.
      - Amit Kumar

Ask Yours
Post Yours
Write your answer