- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Take Input from User
- C Print Integer
- C Add two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply & Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average & Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse Numbers
- C Swap two Numbers
- C Count Positive Negative & Zero
- C Find Largest of two Numbers
- C Find Largest of three Numbers
- C Find Factorial of Number
- C Find HCF & LCM
- C Calculate Area & Perimeter
- C Calculate Area & Circumference
- C Convert Fahrenheit to Centigrade
- C Convert Centigrade to Fahrenheit
- C Print ASCII Values
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find ncR nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd Triangle
- C Print Pascal Triangle
- C Print smiling face
- C One Dimensional Array Program
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge two Array
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Two Dimensional Array Program
- C Add two Matrices
- C Subtract Matrices
- C Transpose Matrix
- C Multiply Matrices
- C Three Dimension Array Program
- C Print String
- C Find Length of String
- C Compare two String
- C Copy String
- C Concatenate String
- C Reverse String
- C Delete Vowels from String
- C Delete Words from Sentence
- C Find Frequency of Character
- C Count Word in Sentence
- C Remove Spaces from String
- C Sort a String
- C Convert Uppercase to Lowercase
- C Convert Lowercase to Uppercase
- C Swap two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read File
- C Write to File
- C Read & Display File
- C Copy File
- C Merge two File
- C List Files in Directory
- C Delete File
- C Encrypt and Decrypt Files
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- Register Now
- Login
- All Test List

To perform bubble sort in C programming, you have to ask to the user to enter the array size then ask to enter array elements, now start sorting the array elements using bubble sort technique.

The logic used in bubble sort is that each consecutive element is compared. If first element is greater than the second, then swapped will be performed otherwise not. Continue swapping until and unless all the possible elements are swapped and arranged in that way, that the first element is smallest and last element is largest in that list.

**Note -** Number of pass = total number of element in the array.

Therefore, we have to run the total number or amount of swapping function as told in the above note or line. For example, if we have total of 10 elements in the array, then we have to perform swapping 10 times as shown in the program given here. And as we all knows that indexing in array starts from 0, then we have to start from 0 to 9 if total number of elements is 10.

Following C program sort the array using bubble sort technique :

/* C Program - Bubble Sort */ #include<stdio.h> #include<conio.h> void main() { clrscr(); int n, i, arr[50], j, temp; printf("Enter total number of elements :"); scanf("%d",&n); printf("Enter %d numbers :",n); for(i=0; i<n; i++) { scanf("%d",&arr[i]); } printf("Sorting array using bubble sort technique...\n"); for(i=0; i<(n-1); i++) { for(j=0; j<(n-i-1); j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf("Elements sorted successfully..!!\n"); printf("Sorted list in ascending order :\n"); for(i=0; i<n; i++) { printf("%d ",arr[i]); } getch(); }

When the above c program is compile and executed, it will produce the following result:

Here are some of the main steps involved in the above program:

- we asked to enter array size
- then to enter array elements of that size
- now for sorting, we have used first (outer) for loop from 0 to (n-1) as array indexing starts
from 0 not 1.
and then second (inner) for loop from o to (n-i-1). As we have to compare two consecutive element, and if first element is greater
than second, then we have to swap. For example, if value of i is 0, then value of j will also be 0, and then using
if statement,
0
^{th}element is compared with (0+1)^{th}or 1^{st}element. If the element present at 0 will be greater than the element present at 1, then the swapping will be perform. The loop will run upto the end that is, if total of 10 elements in the array, then at last the 8^{th}element will be compared with (8+1)^{th}element, that is 9^{th}element which is the last one. - after running the loop to check, compare and swap each and every consecutive element (or every j
^{th}to (j+1)^{th}element), we have sorted the array in ascending order. - finally print out the same array that will show all of its element but in ascending order

Now let's learn how to make the same program using function in C.

The question is, **write a program in C to ask array size and array element at run-time and then sort the array using bubble
sort technique with the help of function. Finally, display the sorted array in ascending order.**

Here is the program that implements bubble sort to sort an array in ascending order using function in C:

// Sorting an array in ascending order using bubble // sort method or technique in C #include<stdio.h> #include<conio.h> void bubbleSort(int arr[], int n); int main() { int arr[100], nos, i; printf("How many element that has to be sorted ? "); scanf("%d", &nos); printf("\n"); for(i=0; i<nos; i++) { printf("Enter integer value for element no.%d: ",i+1); scanf("%d",&arr[i]); } bubbleSort(arr, nos); printf("\n\nFinally sorted array is:\n"); for(i=0; i<nos; i++) { printf("%d\n",arr[i]); } getch(); return 0; } void bubbleSort(int arr[], int no) { int i, j, temp; for(i=no-2; i>=0; i--) { for(j=0; j<=i; j++) { if(arr[j]>arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } }

As the above program is written under **CodeBlocks** IDE, therefore after successfully build and run. Here is the first screenshot
of first sample run:

And here is the second screenshot of first sample run:

All the steps are same as discussed in above one except function. As here we have used function to implement bubble sort. Here are some of the main steps that tells how to use function in this case:

- here we have ask to enter array size and then all elements of that size one by one using loop
- now we have passed the array and array size. Passing array means passing array elements, here array size that comes in square bracket, example, arr[10] is optional.
- after above two steps, we are at function step. In function, we have used same technique as discussed above, that is comparing
each consecutive elements from starting to ending. If first is greater than second then swap, if second is greater than third then
swap and so on. But here we have initialized i with (no-1). Here
**no**indicates to total number of element. For example, if total number of element is 10, then i is initialized to 8, and in second loop, j is initialized with 0. Then at first time or run, the j^{th}element is compared with (j+1)^{th}element. That is 0^{th}element is compared with (0+1)^{th}element (1^{st}) element. At second run, 1^{st}element is compared with 2^{nd}element and so on. This loop will run total of 8 times to compare 8^{th}element with 9^{th}element which is the last element. - after successfully doing all the above steps, the function execution will be over and our array becomes sorted in ascending order.
- now we only have to display the array element with the help of loop

You may also like same program in other programming languages: