Showing posts with label bubble sort. Show all posts
Showing posts with label bubble sort. Show all posts

Friday, June 13, 2014

Bubble Sort in C++

Arranging of numbers are one of the strength of computer by using programming language we were able to make our work much faster, easier and more accurate compared before using the manual operations of arranging a series of numbers. There are many sorting algorithms developed to improve the efficiency of arranging of numbers or words. One of the most versatile sorting algorithm is called bubble sort, this sorting algorithm is very simple to use it will compare all the given numbers and then compare each one of them until such time the biggest numbers are being arrange at the highest position in a given list by the user for instance.

In this article I would like to share with you my simple program that uses bubble sort algorithm to sort a series of numbers given by the user. I create a bubble sort function to take the series of numbers I store each numbers using one dimensional array and then compare its values in our bubble sort function. The function itself will first display the original arrangement of number provided earlier by our user. When we run our program our program will ask the user how many numbers is to process afterwards our program will ask the user to enter a series of numbers.

After the user enter the last number our program will immediately display the original value provided by our user and then display a series of after pass of value it shows how the values is being sorted line by line until it reaches the final sorted list of numbers. I also added a feature of our program that ask the user if he or she wants to run the program again to give another list of values to be sorted by our program.

I hope you will find my work useful in learning how to implement bubble sort using C++ as your programming language.

Thank you very much.



Sample Output of Our Program


Code:Blocks the text editor that I used to write this Bubble Sort Program.

Program Listing

#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

void bubble_sort(int [],int);
int main()
{
    int items[30],numbers=0,i=0;
    char reply;
    do {
     system("cls");
     cout << "\t ===== Bubble Sort Program =====";
     cout << "\n\n";
     cout << "How many items to be process : ";
     cin >> numbers;

     for(i=0;i<numbers;i++) {
     cout << "Enter element item no. " << i+1 << " : ";
     cin >>items[i];
     }
     bubble_sort(items,numbers);
  cout << "Do you want to continue y/n : ";
    cin >> reply;
    if (toupper(reply) == 'N') {
        cout << "\n\n";
        cout << "\t\t Thank You For Using This Software !!!";
        cout << "\n\n";
        break;
    }
    } while (toupper(reply!='Y'));
}

void bubble_sort(int a[],int n)
{
int i=0,j=0,k=0,temp=0;
   cout << "\n";
   cout << "Unsorted List of Values ";
   cout << "\n\n";
    for(k=0;k<n;k++)
         cout << setw(5) <<a[k];
         cout << "\n\n";
    for(i=1;i< n;i++)
    {
         for(j=0;j< n-1;j++)
         if(a[j]>a[j+1])
               {
               temp=a[j];
               a[j]=a[j+1];
               a[j+1]=temp;
               }
    cout << "After Pass Number  : " <<i<< " :=> ";
        for(k=0;k< n;k++)
            cout << setw(5) <<a[k];
            cout << "\n\n";
    }
}