C Code for Bubble Sort

Searching and sorting have been popular operations in elementry data structures. Although now a days many advanced sorting techniques are used to sort a set of data, the basic sorting methods are still popular and are the used frequently. Here C code for bubble sort is given. The algorithm for bubble sort is quite simple. In each iteration the adjacent elements are compared and they are swapped if their order is not correct. The complexity of this algorithm comes out to be O(n²).

The C code is as follows :

#include "stdio.h"
#include "conio.h"

void main( )
{
	int arr[5] = { 25, 17, 31, 13, 2 } ;
	int i, j, temp ;
	for ( i = 0 ; i <= 3 ; i++ )
	{
		for ( j = 0 ; j <= 3 - i ; j++ )
		{
			if ( arr[j] > arr[j + 1] )
			{
				temp = arr[j] ;
				arr[j] = arr[j + 1] ;
				arr[j + 1] = temp ;
			}
		}
	}

	printf ( "\n\nArray after sorting:\n") ;

	for ( i = 0 ; i <= 4 ; i++ )
		printf ( "%d\t", arr[i] ) ;
}


Related Posts

Share

Filed Under: CCodes

Tags:

About the Author: Software Engineer - Search & Recommendation at Rovi

RSSComments (3)

Leave a Reply | Trackback URL

  1. Nikolai says:

    Hey. Nice code. Maybe you could have mentioned why it’s so famous and tell something about performance stuff.

  2. PRASANNA says:

    it’s very easy to learn!!!!!!!!!!!!!!11

  3. Suman says:

    Well that’s a good program but the program being compiled would have looked much better showing its Validity.

Leave a Reply

*

AWSOM Powered