C Programming Code Examples C > Sorting Searching Code Examples Implementing bubble sort algorithm in a C program Implementing bubble sort algorithm in a C program Bubble sort is also known as sinking sort. This algorithm compares each pair of adjacent items and swaps them if they are in the wrong order, and this same process goes on until no swaps are needed. In the following program we are implementing bubble sort in C language. In this program user would be asked to enter the number of elements along with the element values and then the program would sort them in ascending order by using bubble sorting algorithm logic. #include<stdio.h> int main(){ int count, temp, x, j, number[40]; printf("How many numbers are u going to enter?: "); scanf("%d",&count); printf("Enter %d numbers: ",count); for(x=0;x<count;x++) scanf("%d",&number[x]); /* This is the main logic of bubble sort algorithm */ for(x=count-2;x>=0;x--){ for(j=0;j<=x;j++){ if(number[j]>number[j+1]){ temp=number[j]; number[j]=number[j+1]; number[j+1]=temp; } } } printf("Sorted elements: "); for(x=0;x<count;x++) printf(" %d",number[x]); return 0; }