C Program to sort n numbers using bubble sort


March 15, 2022, Learn eTutorial
929

What is a bubble sort algorithm?

In this C program, you will learn how the Bubble sort algorithm works. This C program uses Bubble sort to arrange the elements in the proper order. We can call the Bubble sort - a Comparison Sort, as each pass checks the pair of adjacent elements and makes it in proper order by swapping the elements. This process repeats until all the elements are in the appropriate order.

For example, '5, 1, 6, 7, 2' are the numbers to be sorted, 

  • In the first pass, it sorts '5' and '1' and swaps each other.
  • The numbers are'1,5,6,7,2'. It checks '5' and '6' in order. so it does nothing.
  • Now checks '6' and '7', and so on...
  • at the end of the first pass, the element '5' will be in the correct position.

Now it takes the second pass. and repeats the process as in the first pass. 

 The best case in Bubble sort is on the already sorted list.

The worst-case complexity of Bubble sort is o(n**2), where 'n' is the number of elements in the list.

How Bubble sort algorithm is implemented in C programming?

In this C program, after we are getting the user input for a number of terms needed to sort, we add the list elements from the user into an array using a for loop and then print the user input list using another for loop.

Now, use two nested 'for loops' to compare each element with the adjacent elements in the list. If the second element is less than the first, we have to interchange the elements using the swap method with a temp variable.

We are doing that for each element until the array sorts entirely. We take each element from the outer for loop and compare that with all other elements in the inner for loop to get that element in the right position in the first pass. Now we take the next element from the outer loop in the second pass, and so on. Finally, we print the sorted list.

ALGORITHM

STEP 1: Include the Header Libraries into the C program to use the built-in functions in that library.

STEP 2: Initialize the variables used in the program and the Array to store the values.

STEP 3: Accept the number of elements needed from the user using printf and scanf functions. And store that in a variable.

STEP 4: Using for loop accepts the elements into the Array from the user using a scanf.

STEP 5: Using the for loop, print the Array using printf

STEP 6: Open the nested Outer For loop from zero to the number of terms in Array to check every Element.

STEP 7: Open the Inner For loop from zero to the number of terms - the number in the outer loop - 1 to check each Element from the outer loop with the Array non-sorted Elements.

STEP 8: Check the comparing Element is greater than the compared Element using 'if' condition.

STEP 9: if so, using the Swap technique using temporary variable Swap the elements. 

STEP 10: print the Sorted Array using printf and for loop in C programming language.

 

C Source Code

                                          #include <stdio.h>
#define MAXSIZE 10

void main() 
{
    int array[MAXSIZE];                                 /* declares array and variables */
    int i, j, N, temp;
    printf("Enter the value of N\n");
    scanf("%d", & N);
    printf("Enter the elements one by one\n");               /* accept and enter the list elements into a array  */
    for (i = 0; i < N; i++)
    {
       scanf("%d", & array[I]);
    }
    printf("Input array is...\n");
    for (i = 0; i < N; i++)
    {
            printf("%d\n", array[I]);                         /* prints the sorted list */
     }
    for (i = 0 ; i < N - 1; i++)
    {
        for (j = 0 ; j < N - i - 1; j++)
        {
            if (array[j] > array[j+1])                     /* comparing the elements using nested for loop */
            {
                temp       = array[j];
                array[j]   = array[j+1];                 /* swapping the elements using temp */
                array[j+1] = temp;
            }
       }
   }
   printf("Sorted array is...\n");
   for (i = 0; i < N; i++) 
   {
        printf("%d\n", array[I]);            /* prints the sorted list */
    }
}   
                                      

OUTPUT

Enter the value of N
5

Enter the elements one by one
390
234
111
876
345

Input array is
390
234
111
876
345

Sorted array is...
111
234
345
390
876