 # C Program to sort n numbers using bubble sort

## What is a bubble sort algorithm?

In this tutorial, you will learn how bubble sort 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 components. 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 checks '6' and '7', and at the end of the first pass, the element '5' will be in the correct position.

Now it takes the second pass. The best case in bubble sort is on the 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 is used to sort the elements in C

In this c program, after we are getting the user input list, we add the list into an array. We 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 loop and compare that with all other elements in the inner loop to get that element in the right position. In the first pass, 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]) /* For decreasing order use '<' instead of '>' */
{
temp       = array[j];
array[j]   = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < N; i++) {
printf("%d\n", array[i]); /* prints the sorted list */
}

}   /* End of main*/```
```

## 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```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 