Home / Programs / Write a C program that uses a function to sort an array of integers using bubble sort algorithm.
🚀 Programming Example

Write a C program that uses a function to sort an array of integers using bubble sort algorithm.

👁 2,708 Views
💻 Practical Program
📘 Step Learning
Write a C program that uses a function to sort an array of integers using bubble sort algorithm.

💻 Program Code

#include <stdio.h>

void sort (int [], int);

int main (void)
{
	int i;
	int arr[10] = {3,2,7,0,6,4,9,8,1,5 };
	printf ("The array before the sort:\n");
	for ( i = 0; i < 10; ++i )
		printf ("%i ", arr[i]);
	sort (arr, 10);
	printf ("\n\nThe array after the sort:\n");
	for ( i = 0; i < 10; ++i )
		printf ("%i ", arr[i]);
	return 0;
}


void sort (int a[], int n)
{
	int i, j, temp;
	for ( i = 0; i < n - 1; ++i )
		for ( j = 0; j < n-i-1; ++j )
			if ( a[j] > a[j+1] ) {
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
}
 
                        

🖥 Program Output

The array before the sort:
3 2 7 0 6 4 9 8 1 5

The array after the sort:
0 1 2 3 4 5 6 7 8 9
 
                            

📘 Explanation

None
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.