» » » Bubble sort algorithm in c with explanation

# Bubble sort algorithm in c with explanation

• Bubble sort is one of the example of sorting algorithm.
• Bubble sort in data structure
• Bubble sort is a procedure of sorting elements in ascending or descending order.
• If we want to sort an array of numbers [4,2,3,1]  using bubble sort it will return [1,2,3,4] after sorting in ascending order.

Bubble sort in c with explanation

• Bubble sort algorithm of ascending order will move higher valued elements to right and lower value elements to left.
• For this we need to compare each adjacent elements in an array and check they are in order or not if not swap those two elements.
• After completion of first iteration highest value in the array will be moved to last or final position.
• In the worst case all the highest elements in array like [9 8 7 5 4] completely in reverse order so we need to iterate all of the elements in N times.
• Repeat this process N- 1 time in worst case to sort set of elements.
• Worse case complexity : O(n2
• In the best case scenario all the elements are already in sorted order so one iteration with no swaps required.
• Best case complexity: O(n)
• Let us see the graphical representation of explanation of bubble sort algorithm in c programming language.

Bubble sort algorithm with example diagram

Bubble sort algorithm pseudocode:

Program #1 : Write a C program to sort list of elements using bubble sort algorithm.

1. #include <stdio.h>
2. #include <stdlib.h>
3. //Bubble sort algorithm in c
4. //www.instanceofjava.com
5. int main(int argc, char *argv[])
6. {
7.    int array, n,i,j,k, swap;
8.
9.   printf("Enter number of elements to sort\n");
10.   scanf("%d", &n);
11.
12.   printf("Enter %d elements \n", n);
13.   for (i = 0; i< n; i++)
14.     scanf("%d", &array[i]);
15.
16.   printf("\nBefore sorting \n ");
17.    for (k = 0; k < n; k++) {
18.       printf("%5d", array[k]);
19.    }
20.
21.    for (i = 1; i < n; i++) {
22.       for (j = 0; j < n - 1; j++) {
23.          if (array[j] > array[j + 1]) {
24.             swap = array[j];
25.             array[j] = array[j + 1];
26.             array[j + 1] = swap;
27.          }
28.       }
29.
30.       }
31.
32.  printf("\nAfter Bubble sort elements are:\n");
33.    for (k = 0; k < n; k++) {
34.       printf("%5d", array[k]);
35.    }
36.
37.   return 0;
38. }

Output:

• Now we will see program on recursive bubble sort algorithm in c
• Bubble sort using recursive function in c

Program #2: Write a c program for bubble sort using recursion

1. #include <stdio.h>
2. #include <stdlib.h>
3. //Bubble sort algorithm in c
4. //www.instanceofjava.com
5. void bubblesort(int array[], int n);
6. int main(int argc, char *argv[])
7. {
8.    int array, n,i,k;
9.
10.   printf("Enter number of elements to sort\n");
11.   scanf("%d", &n);
12.
13.   printf("Enter %d elements \n", n);
14.   for (i = 0; i< n; i++)
15.     scanf("%d", &array[i]);
16.
17.   printf("\nBefore sorting \n ");
18.    for (k = 0; k < n; k++) {
19.       printf("%5d", array[k]);
20.    }
21.  bubblesort(array,n);
22.
23.   getch();
24. }
25.
26. void bubblesort(int array[], int n){
27.       int  i,j,k, swap;
28.   for (i = 1; i < n; i++) {
29.       for (j = 0; j < n - 1; j++) {
30.          if (array[j] > array[j + 1]) {
31.             swap = array[j];
32.             array[j] = array[j + 1];
33.             array[j + 1] = swap;
34.          }
35.       }
36.
37.       }
38.
39.  printf("\nAfter Bubble sort elements are:\n");
40.    for (k = 0; k < n; k++) {
41.       printf("%5d", array[k]);
42.    }
43. }

Output:

1. Enter number of elements to sort
2. 6
3. Enter 6 elements
4. 9 8 2 3 5 1
5.
6. Before sorting
7.      9    8    2    3    5    1
8. After Bubble sort elements are:
9.     1    2    3    5    8    9