Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bubble sort Algorithm Tutorial
Author Message
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #1
Bubble sort Algorithm Tutorial
So, what is the Bubble sort algoriithm? The Bubble sort algorithm is a simple sorting algorithm. It will put an array of numbers from least to greatest or vise versa.

The algorithm works by going down the array and compares the numbers individually with each other.

Say your comparing 2, 9, 4, 7, and 5. The Bubble sort compares 9 and 2, then switches. 2, 9, 4, 7, 5

Now it compares 2 and 9. Two is smaller so nothing happens.
Then it compares 9 and 4./ 2, 4, 9, 7, 5
Now it compares 9 and 7 then 9 and 5./ 2, 4, 7, 5, 9
It repeats the process until everything is correct.

You can see this isn't efficient for large arrays.

Code:
//Bubble sort example by Scorch
#include <iostream>


void BubbleSort(int array[], int length)
{
     for(int i=0;i<length;++i)
     {
         for(int j=length;j>i;--j)
         {
             if(array[i]>array[j])
             {
                 int swap = array[i];
                 array[i] = array[j];
                 array[j] = swap;
             }
         }
        std::cout<<array[i]<<"\n";
     }
}


int main()
{
    int array[] = {9, 4, 12, 134, 26, 19, 47, 94, 668, 1002};
    int length = sizeof(array)/sizeof(array[0]);
    BubbleSort(array, length);
    return 0;
}

This will print from least to greatest. If you want the reverse change this line to.
Code:
for(int j=length;j>i;--j) Least to greatest

for(int j=0;j<i;++j) Greatest to least

Do remember the Bubblesort is simple, but slow. For large arrays the Insertion sort algorithm is much more efficient.
(This post was last modified: 01-16-2011 06:32 PM by Scorch.)
01-16-2011 07:07 AM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #2
RE: Bubble sort Algorithm Tutorial
Looks good, I remember having to bubble sort for an assignment one time. What if you had something like 2 4 8 3 9? Would it go:

Compare 2 && 4 //2 is smaller, stays where it is
Compare 4 && 8 //4 is smaller, stays where it is
Compare 3 && 8 //3 is smaller, stays where it is
Compare 4 && 3 //3 is smaller, swaps
Compare 8 && 9 //8 is smaller, stays where it is

Or would it end up printing 2 4 3 8 9?
01-16-2011 07:41 AM
Find all posts by this user Quote this message in a reply
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #3
RE: Bubble sort Algorithm Tutorial
(01-16-2011 07:41 AM)Hidden Dragon Wrote:  Looks good, I remember having to bubble sort for an assignment one time. What if you had something like 2 4 8 3 9? Would it go:

Compare 2 && 4 //2 is smaller, stays where it is
Compare 4 && 8 //4 is smaller, stays where it is
Compare 3 && 8 //3 is smaller, stays where it is
Compare 4 && 3 //3 is smaller, swaps
Compare 8 && 9 //8 is smaller, stays where it is

Or would it end up printing 2 4 3 8 9?

It would print 2, 4, 3, 8, 9
Then it would go through again.

Compare 2 and 4 again, no change.
Compare 4 and 3, swap. 2, 3, 4, 8, 9.
At this point it won't make another run through.
01-16-2011 07:44 AM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #4
RE: Bubble sort Algorithm Tutorial
Alright, thanks. It's been a while since I've done C++ so I'm pretty rusty.
01-16-2011 07:46 AM
Find all posts by this user Quote this message in a reply
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #5
RE: Bubble sort Algorithm Tutorial
You know assembly. All I can do in assembly is a hello world message box.Tongue
01-16-2011 07:49 AM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #6
RE: Bubble sort Algorithm Tutorial
I sort of know assembly. I'm alright with it, definitely not an expert in any means. I have trouble making a "simple" window. Window programming is a lot harder than console.
01-16-2011 04:52 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: