Bubble Sort in C Program Example

How to write a c program to bubble short in C Programming ?


Solution:
#include <stdio.h>
#define SIZE 7

void bubbleSort(int *A, int length)
{
int i, j;

for(i = 1; i <= length - 1; i++)
{
for(j = 0; j < length - i; j++)
if(A[j] > A[j + 1])
{
int temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}

int main()
{
int arr[SIZE] = {11, 1, 8, 5, 6, 8, 4};
int i;

bubbleSort(arr, SIZE);

for(i = 0; i < SIZE; i++)
printf("%d ", arr[i]);

return 0;
}

https://en.wikipedia.org/wiki/Bubble_sort


Learn More :