# Bubble Sort Algorithm in C

“** Bubble Sort is an elementary sorting algorithm used to arrange an array of elements in either ascending or descending order. If adjacent items are in the wrong sequence, it compares them and swaps them. The name "Bubble Sort" originates from the smaller or larger elements "bubbling" to the top of the list as the algorithm progresses.**”

The Bubble Sort algorithm is straightforward and simple to grasp. It works by comparing every pair of adjacent array elements, exchanging them if necessary, and then repeating the process for all pairs. Until no further swaps are necessary, the program repeats this operation.

1. see the first and second items in an array.

2. If the primary one is larger than the second one, change the elements.

3. The biggest (or smallest) member will have "bubbled" to the end (or at starting) of the array by the completion of the first time.

4. For the remaining ones, repeat the procedure above, but skip the ones that have already been sorted.

## Implementing Bubble Sort Algorithm in C :

We will use nested loops to traverse through the array and swap out entries if they are out of order in order to create the Bubble Sort algorithm in C. This is the code for Bubble Sort in C:

```
#include <stdio.h>
void bubbleSort(int arr[], int k) {
int n, m, temp;
for (n = 0; n < k - 1; n++) {
for (m= 0; m < k - n - 1; m++) {
if (arr[m] > arr[m+ 1]) {
temp = arr[m];
arr[m] = arr[m+ 1];
arr[m+ 1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 2, 9, 3, 6, 8};
int k = sizeof(arr) / sizeof(arr[0]);
int n ;
printf("Original array: ");
for (n = 0; n < k; n++) {
printf("%d ", arr[n]);
}
printf("\n");
bubbleSort(arr,k);
printf("Sorted array: ");
for (n = 0; n < k; n++) {
printf("%d ", arr[n]);
}
printf("\n");
return 0;
}
```

**Output:**

**Explanation:**

- The above code first defines a function called
`bubbleSort,`

It requires an integer array arr and its size n as its first two inputs. The code then iterates across the array while comparing nearby elements using stacked loops. - The outer loop iterates n - 1 time, which is the number of passes it required to type the array. The inner loop iterates from zero to n - i - 1, in which i is the modern-day generation of the outer loop.
- To summarise, the Bubble Sort algorithm is a simple but powerful technique for sorting an array of statistics in ascending or descending order. It analyses neighbouring factors and swaps them if they may be in the wrong order, with smaller or larger additives "bubbling" to the pinnacle of the listing because the set of rules advances.
- The process of constructing Bubble Sort in the C programming language might assist improve problem-solving abilities. Programmers can better grasp the rationale behind their code and lay a solid basis for more complex programming jobs by breaking an issue down into smaller, more manageable steps.
- While the Bubble Sort technique is straightforward to realise and observe, it can not be the handiest sorting technique for large information sets. Its temporal complexity is O(n2), which suggests that sorting a big variety of factors takes a lengthy time. Other sorting algorithms, such as Quick Sort and Merge Sort, are more effective for big record units and have a time complexity of O(n log n).
- Despite its obstacles, Bubble Sort set of rules is still a useful tool for sorting small information sets, and it's far often utilized in educational settings to train programming concepts inclusive of loops and conditional statements. It is also a good starting point for knowledge extra complicated sorting algorithms.

### Conclusion

Overall, the Bubble Sort set of rules is a precious tool in any programmer's arsenal. While it may no longer be the most efficient sorting set of rules, its simplicity and simplicity of implementation make it a brilliant choice for sorting small facts units or for coaching programming concepts. It is crucial to recognize the change-offs among efficiency and ease whilst selecting a sorting algorithm, and to pick out the one that great suits the necessities of the mission handy.