- 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.
- #include <stdio.h>
- #include <stdlib.h>
- //Bubble sort algorithm in c
- //www.instanceofjava.com
- int main(int argc, char *argv[])
- {
- int array[100], n,i,j,k, swap;
- printf("Enter number of elements to sort\n");
- scanf("%d", &n);
- printf("Enter %d elements \n", n);
- for (i = 0; i< n; i++)
- scanf("%d", &array[i]);
- printf("\nBefore sorting \n ");
- for (k = 0; k < n; k++) {
- printf("%5d", array[k]);
- }
- for (i = 1; i < n; i++) {
- for (j = 0; j < n - 1; j++) {
- if (array[j] > array[j + 1]) {
- swap = array[j];
- array[j] = array[j + 1];
- array[j + 1] = swap;
- }
- }
- }
- printf("\nAfter Bubble sort elements are:\n");
- for (k = 0; k < n; k++) {
- printf("%5d", array[k]);
- }
- return 0;
- }
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
- #include <stdio.h>
- #include <stdlib.h>
- //Bubble sort algorithm in c
- //www.instanceofjava.com
- void bubblesort(int array[], int n);
- int main(int argc, char *argv[])
- {
- int array[10], n,i,k;
- printf("Enter number of elements to sort\n");
- scanf("%d", &n);
- printf("Enter %d elements \n", n);
- for (i = 0; i< n; i++)
- scanf("%d", &array[i]);
- printf("\nBefore sorting \n ");
- for (k = 0; k < n; k++) {
- printf("%5d", array[k]);
- }
- bubblesort(array,n);
- getch();
- }
- void bubblesort(int array[], int n){
- int i,j,k, swap;
- for (i = 1; i < n; i++) {
- for (j = 0; j < n - 1; j++) {
- if (array[j] > array[j + 1]) {
- swap = array[j];
- array[j] = array[j + 1];
- array[j + 1] = swap;
- }
- }
- }
- printf("\nAfter Bubble sort elements are:\n");
- for (k = 0; k < n; k++) {
- printf("%5d", array[k]);
- }
- }
Output:
- Enter number of elements to sort
- 6
- Enter 6 elements
- 9 8 2 3 5 1
- Before sorting
- 9 8 2 3 5 1
- After Bubble sort elements are:
- 1 2 3 5 8 9
No comments