Bubble Sort in C I am exceptionally intrigued to converse with you about this article. The explanation is that this article contains extremely fascinating data. We should go to this article
Bubble Sort is a straightforward calculation that is utilized to sort a given arrangement of n components furnished in type of an exhibit with n number of components. Bubble Sort thinks about every one of the components individually and sorts them in light of their qualities.
Bubble Sort in C
In the accompanying project, we are executing bubble sort in C language. In this program, client would be approached to enter the quantity of components alongside the component values and afterward the program would sort them in climbing request by utilizing bubble arranging calculation rationale.
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
How Bubble Sort Algorithm works?
Beginning with the first element(index = 0), contrast the ongoing component and the following component of the exhibit.
In the event that the ongoing component is more noteworthy than the following component of the cluster, trade them.
In the event that the ongoing component is not exactly the following component, move to the following component. Rehash Stage 1.
Output
Enter number of elements
3
Enter 3 integers
4
9
1
Sorted list in ascending order:
1
4
9
Bubble sort program in C language using function
#include <stdio.h>
void bubble_sort(long [], long);
int main()
{
long array[100], n, c;
printf("Enter number of elements\n");
scanf("%ld", &n);
printf("Enter %ld integers\n", n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%ld\n", array[c]);
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < n - 1; c++) {
for (d = 0 ; d < n - c - 1; d++) {
if (list[d] > list[d+1]) {
/* Swapping */
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
Bubble Sort algorithm to check if an array is sorted or not
#include <stdio.h>
int is_Array_Sorted(int [], int);
int main()
{
int a[100], n, c;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &a[c]);
if (is_Array_Sorted(a, n))
printf("The array is sorted.\n");
else
printf("The array isn't sorted.\n");
return 0;
}
int is_Array_Sorted(int a[], int n) {
int c, d, sorted = 1, t;
for (c = 0 ; c < n - 1; c++) {
for (d = 0 ; d < n - c - 1; d++) {
if (a[d] > a[d+1]) {
t = a[d];
a[d] = a[d+1];
a[d+1] = t;
return 0;
}
}
}
return 1;
}
Final Words
We took in some fascinating data through the article Air pocket Sort in C. Likewise in the event that you feel a little skeptical about this article you can report your questions as a remark box. Much appreciated