Bubblesort written in C with example step-by-step

Oct 16

Source code of sorting algorithm “bubblesort” written in C. Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.

Step-by-step example
Let us take the array of numbers “5 1 4 2 8″, and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared.

- First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
- Second Pass:
( 1 4 2 5 8 ) ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
Finally, the array is sorted, and the algorithm can terminate.

Source Code in C:


// © Codeido - www.codeido.com - Developed by Paolo Musolino

#include <stdio.h>

void swap(int *a, int *b)
{	
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
	
	
}
//---------------//---------------//---------------
int confronto(int x, int y)
{
	if(x<y) return -1;
	else if(x>y) return 1;
	else {
		return 0;
	}
	
}

void bubbleSort(int a[], int n)
{
	int i, j;
	for (i=0; i<n-1; i++) 
	{
		for (j=n-1; j>i; j--) 
		{
			if (confronto(a[j], a[j-1])<0) 
			
				swap(&a[j], &a[j-1]);
			
		}
	}
}


//Main Function
int main ()
{
	int a[14]={5, 10, 11, 9, 7, 8, 4, 5, 7, 2, 45, 1, 4, 20};
	int i;
	bubbleSort(a, 14);
	printf("\n");
	for (i=0; i<14; i++) {
		printf("%d ", a[i]);
	}
	
	
	return 0;
}

Share

14 comments

  1. Dreamin. I love blogging. You all express your feelings the right way, because they are your feeling, focus on your blog it is great.

  2. Nice blog here! Also your website loads up fast! What host are you using? I wish my website loaded up as fast as yours lol

  3. @Attorney: Thank you very much

    @Bill: I use this: http://www.bluehost.com/track/codeido

  4. Hey, possibly this post is a bit off topic but in any event, I have been surfing about your website and it looks really great. apparent that you know your subject matter I am building a new blog and hard put to make it appear good, and supply really good content. I have discovered a much on your site and I look forward to additional updates and will be returning.

  5. The Science of Getting Rich /

    Hi, perhaps this is not on topic but anyway, i’ve been browsing about your blog and it looks really great. fervent I’m making a new blog and hard-pressed to have it appear great, and offer really good subject matter. I have learned a lot here and I look forward to more updates and will be back.

  6. Thank you all for your compliments. We will be online soon with many new updates:) maybe today!

  7. I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one these days.. :)

  8. Very nice information,I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work Look forward to reading more from you in the future.

  9. very good \o/

  10. I would like to appreciate it for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I loved your style I will join for your feed please keep posting!

  11. thanks admin..!

  12. chaitanya /

    i am extremely happy with u r explanation with example…………if i had any doubts in further i will definetley choose dis site…………

  13. A very good example …well explained …thanks!

Trackbacks/Pingbacks

  1. Tweets that mention Bubblesort written in C with example step-by-step | Codeido -- Topsy.com - [...] This post was mentioned on Twitter by Paolo Musolino, Paolo Musolino. Paolo Musolino said: Bubblesort written in C with …
  2. Leadsort written in C | Codeido - [...] sorting algorithm “leadsort” is a variant of “bubblesort” that allows you to sort the array in reverse. Computational complexity= …
  3. rca 46la45rq - rca 46la45rq First off I want to say wonderful blog! I had a quick question which I'd like to ask …

Leave a Reply