Computer Tech
Bubble sort Algorithm Tutorial - Printable Version

+- Computer Tech (https://computertech.createmybb3.com)
+-- Forum: Programming (https://computertech.createmybb3.com/forumdisplay.php?fid=6)
+--- Forum: C, C++ (https://computertech.createmybb3.com/forumdisplay.php?fid=7)
+--- Thread: Bubble sort Algorithm Tutorial (/showthread.php?tid=120)



Bubble sort Algorithm Tutorial - Scorch - 01-16-2011

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;
}
[/code]

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
[/code]

Do remember the Bubblesort is simple, but slow. For large arrays the Insertion sort algorithm is much more efficient.


RE: Bubble sort Algorithm Tutorial - Hidden Dragon - 01-16-2011

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?


RE: Bubble sort Algorithm Tutorial - Scorch - 01-16-2011

(01-16-201107: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.


RE: Bubble sort Algorithm Tutorial - Hidden Dragon - 01-16-2011

Alright, thanks. It's been a while since I've done C++ so I'm pretty rusty.


RE: Bubble sort Algorithm Tutorial - Scorch - 01-16-2011

You know assembly. All I can do in assembly is a hello world message box.Tongue


RE: Bubble sort Algorithm Tutorial - Hidden Dragon - 01-16-2011

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.