Bubble Sort with C++ with Smart Swapping technique


using namespace std;

      int arr[] = {5,1,4,2,8};
      int temp;
      for(int j=0; j< sizeof(arr)/sizeof(int); j++)
          for(int i=0; i<sizeof(arr)/sizeof(int); i++)
                  if(arr[i] > arr[i+1])
                            temp = arr[i];
                            arr[i] = arr[i+1];
                            arr[i+1] = temp;
                            // here's a fun part. Swap two values without using a temp var!
                            arr[i] = arr[i] + arr[i+1];
                            arr[i+1] = arr[i] - arr[i+1];
                            arr[i] = arr[i] - arr[i+1];
      for(int i=0; i<sizeof(arr)/sizeof(int); i++)



About praveenaj

I'm just an average guy who enjoys doing Programming/Web Designs/Pencil art & drinking coffee :)
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s