Copyright © Programs ++
Design by Dzignine
Friday 17 February 2012

Bubble sort in C++


Sorting is used in many programming applications and as there are various sorting algorithms, each
having their respective pros and cons. Bubble sort, is one such search algorithm that is very easy to implement.
Bubble sort can be used to sort both in ascending order as well as in descending order. The basic idea of bubble sort
is to compare two adjoining value and exchange them if they are not in proper order. In every pass, the heaviest element
settles as its appropriate position in the bottom(if is to be sorted in ascending order) and the opposite is true for
sorting in descending order. Bubble sort is useful when a small no. of element are to be sorted as it is a simple algorithm.
However, due to comparatively large no of passes(loop counts), as compared to other sorting algorithms, its is advisable
to use other searching algorithms , e.g quick sort.
A program is given below that sorts an integer array in ascending order using bubble sort algorithm




// Program to implement bubble sort in C++\

#include <iostream>

int main()
{
     int arr[20];
     int size, temp,i,j;
     std::cout<<" Enter the no. of elements you want in the array (max 20 ) : ";
     std::cin>>size;
     std::cout<<" \n Now enter the elements in the array ";
     // loop to insert elements in the array, which is initially empty
     for (i=0; i<size; i++)
     {
          std::cout<<" \n Element "<<i<<" : ";
          std::cin>>arr[i];
     } // end of for loop

     // to sort using bubble sort algorithm(ascending order)

     for (i=0; i<size; i++)
     {
          for (j=0; j<=i; j++)
          {
               if( arr[j] > arr[i]) // exchanges heavy elements with lighter ones
               {
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
               }

          } // end of sub loop
     } // end of main loop

     std::cout<<" \n The sorted array is as follows : \n";
     for (i=0; i<size; i++)
     {
          std::cout<<" \n Element "<<i<<" : "<<arr[i];
     } // end of main loop
     return 0;
} // end of main

------ OUTPUT ------


















Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.

------ Related Posts ------

Insertion Sort in C++ : http://programsplusplus.blogspot.in/2012/02/insertion-sort-in-c.html
Selection Sort in C++ : http://programsplusplus.blogspot.in/2012/02/selection-sort-in-c.html
 

5 comments:

  1. thank you!!
    you have done a great work!!

    ReplyDelete
  2. why 2 for loops are used for exchanging values???? i is for what ??? and j is for what ??????

    ReplyDelete
  3. #include
    #include

    const int MAXSIZE = 10;

    void bubbleSort (int arr[], int size);
    void swap (int& x, int& y);

    int main()
    {
    int nums[] = { 1, 7, 5, 3, 15, 11, 13. 17, 21, 19 };
    int k;

    cout << "BEFORE SORT: ";
    for (k = 0; k < MAXSIZE; k++)
    cout << nums[k] << " ";

    bubbleSort(nums, MAXSIZE);

    cout <= 0 && is Changed);
    {
    isChanged = 0;

    for (int k = 0; k <= last; k++);
    if (arr[k] < arr[k+1])
    {
    swap(arr[k], arr[k+1]);
    isChanged = 1;
    }
    last--;
    }
    }//end bubbleSort()

    void swap(int& x, int& y)
    {
    int temp;
    temp = x;
    x = y;
    y = temp;
    }//end swap


    what is the problem in this coding....
    bro, help me solve it...




    ReplyDelete
  4. nicely written implementation of bubble sort algorithm but it is missing explanations, if anyone wants explanations then they could find it there:

    http://www.hellgeeks.com/bubble-sort/

    ReplyDelete

Comment Here....