The C++ Bubble Sort

Published on: February 11, 2011

In this tutorial, I introduce the concept of a Bubble Sort. This code allows you to sort objects/values/variables in your program from largest to smallest or smallest to largest. It's very simple and it's the first program before I introduce the Index sort for you which will be a little different.

You will need to know about Arrays, the for loop, and the if statement for this program. If you are not familiar with any of them, please have a look at my other tutorials and familiarize yourself before doing this tutorial.

If you have any questions about the program or anything in general relating to beginner C++ Programming, feel free to ask. I hope you enjoy the video and if you have any requests feel free to let me know.

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.

Codesnip:

//Name:  Nazmus
//Program: Bubble Sort
//Website: EasyProgramming.net

#include <iostream>

using namespace std;

int main()
{
 int numb[7];
 int i, j;

 for(i=0;i<=6;i++)
 {
  cout << "Please enter number: ";
  cin >> numb[i];
 }

 for(i=0;i<=5;i++)
 {
  for(j=i+1;j<=6;j++)
  {
   int temp;

   if(numb[i] > numb[j])
   {
    temp = numb[i];
    numb[i] = numb[j];
    numb[j] = temp;
   }
  }
 }

 for(i=0;i<=6;i++)
 { 
  cout << endl <<  numb[i] << endl;
 }

 cin.ignore();
 cin.get();

 //system("pause");
}



Comments: