[C++] Bubble sort

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++] Bubble sort

Post by ayu »

A simple way to sort lists, where the principle is that you compare two numbers with each other and decide if they are to change place, like if one of the values are bigger then the other, then you move forward. It is said that you "bubble" through the list.

I'm sorry about the comments, but these were commented on a whiteboard at the time of creation, so I never added them to the code.
Blame PHPBB2 for the tabs.
void sort(int arr[], int size)
{
bool swap = false;
int temp;

do
{
swap = false;
for(int i = 0; i < (size - 1); ++i)
{
if(arr < arr[i + 1])
{
temp = arr[i + 1];
arr[i + 1] = arr;
arr = temp;
swap = true;
}
}
}
while(swap);
}
"The best place to hide a tree, is in a forest"

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Post by l0ngb1t »

it remind me of the last year it was my 1st year at the institute
i miss that year C/C++ was the 1st language i learned and it's my favorite :D:D:D:D
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

Post Reply