Tuesday, September 18, 2012

Bubble Sort Simple Program in C++

 This is a simple demonstration of Bubble Sort, I made this in C++ for my class fellows

You should use Dev C++ Compiler to compile this, this might not work in TC

1. The program will first ask the total number of values
2. Using Bubble Sort, it will sort all the values
3. Finally you will get a answer is sorted form


#include<iostream.h>
#include<conio.h>
main()
{
      int value[10];
      int temp;
      int values;
      cout<<"How many values do you want ? ";
    cin>>values;
    for(int b=1; b<=values; b++)
    {
    cout<<"Enter Value ";
    cin>>value[b];
    }         
              for (int b = 1; b < values-1; b++)
            {
                for (int a = 1; a < values; a++)
                {
                    if (value[a] > value[a + 1])
                    {
                        temp = value[a];
                        value[a] = value[a + 1];
                        value[a + 1] = temp;
                    }
                }
            }
            cout<<"\t\tSorted Answer\n\n";
            for(int b=1; b<=values; b++)
            {
                    cout<<value[b];
                    cout<<"\n";
             }
      getche();
      return 0;
      }

Download a .exe of this project from here


No comments:

Post a Comment