article

Bubble Sort Algo

Email
Submitted on: 1/4/2015 5:07:00 AM
By: d1rtyw0rm (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 806
 
     It is a simple algorithm about howto sort an array of a number in chronological order

 
				
The sorting by bubble consists in traversing an array and to interchange each couple of numbers of which first ranks above the second. Several passages on the table will be necessary (exactly items_numbers - 1)

Little exemple :
ORIGINAL ARRAY : 8 3 9 7 1

First Passage

3 8 9 7 1
3 8 9 7 1
3 8 7 9 1
3 8 7 1 9

As of the first passage, the highest number is automatically thorough with the last position. Therefore, we will not need more to check it with other numbers at the time of the next passages. In the code which will follow, variable N discharges this task. In fact, each following passage will contain each one a checking of less than preceding it.

Second Passages

3 8 7 1 9
3 7 8 1 9
3 7 1 8 9

Third Passages

3 7 1 8 9
3 1 7 8 9
Fourth Passage

1 3 7 8 9

The following algorithm sort each element of an array using the bubble sorting. The variables N and K are counter and swap is a variable being used like a temporarily contain to store a value at the time while interchangement as values between two variables.

int n, k, swap;

for (n = num - 1; n > 0; n--)
{
for (k = 0; k < n; k ++)
{
if (tab[k] > tab[k + 1])
{
swap = tab[k];
tab[k] = tab[k + 1];
tab[k + 1] = swap;
}
}
}
-d1rtw0rm


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.