0% found this document useful (0 votes)
17 views12 pages

Bubble Sort in C

Bubble sort is a simple sorting algorithm that repeatedly swaps adjacent elements until the array is sorted, resembling the movement of air bubbles in water. It has a time complexity of O(n^2) in average and worst cases, making it inefficient for large datasets. The document includes a detailed explanation of the algorithm's working, a C program implementation, and example output.

Uploaded by

akkutuppu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Bubble Sort in C

Bubble sort is a simple sorting algorithm that repeatedly swaps adjacent elements until the array is sorted, resembling the movement of air bubbles in water. It has a time complexity of O(n^2) in average and worst cases, making it inefficient for large datasets. The document includes a detailed explanation of the algorithm's working, a C program implementation, and example output.

Uploaded by

akkutuppu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Bubble sort in C

Bubble sort Algorithm

Bubble sort works on the repeatedly swapping of adjacent elements until they are
not in the intended order. It is called bubble sort because the movement of array elements
is just like the movement of air bubbles in the water. Bubbles in water rise up to the
surface; similarly, the array elements in bubble sort move to the end in each iteration.

Although it is simple to use, it is primarily used as an educational tool because the


performance of bubble sort is poor in the real world. It is not suitable for large data
sets. The average and worst-case complexity of Bubble sort is O(n2), where n is a
number of items.
Working of Bubble sort Algorithm
Now, let's see the working of Bubble sort Algorithm.
To understand the working of bubble sort algorithm, let's take an unsorted array. We
are taking a short and accurate array, as we know the complexity of bubble sort is
O(n2).
Let the elements of array are -

First Pass
Sorting will start from the initial two elements. Let compare them to check which is greater.

Here, 32 is greater than 13 (32 > 13), so it is already sorted. Now, compare 32 with 26.
Here, 26 is smaller than 36. So, swapping is required. After swapping new array
will look like -

Now, compare 32 and 35.

Here, 35 is greater than 32. So, there is no swapping required as they are already
sorted.
Now, the comparison will be in between 35 and 10.

Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now, we reach
at the end of the array. After first pass, the array will be -

Now, move to the second iteration.


Second Pass
The same process will be followed for second iteration.

Here, 10 is smaller than 32. So, swapping is required. After swapping, the array will be -

Now, move to the third iteration.


Third Pass
The same process will be followed for third iteration.

Here, 10 is smaller than 26. So, swapping is required. After swapping, the array will be -

Now, move to the fourth iteration.


Fourth pass
Similarly, after the fourth iteration, the array will be -

Hence, there is no swapping required, so the array is completely sorted.

Bubble sort complexity


Now, let's see the time complexity of bubble sort in the best case, average case, and
worst case. We will also see the space complexity of bubble sort.
Bubble sort algorithm
1. Start at index zero, compare the element with the next one (a[0] & a[1] (a is the name of
the array)), and swap if a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] > a[2].
Repeat this process until the end of the array. After doing this, the largest element is
present at the end. This whole thing is known as a pass. In the first pass, we process array
elements from [0,n-1].
2. Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is
present at its correct position. After this step, the largest two elements are present at the
end.
3. Repeat this process n-1 times.
Bubble sort program in C
/* Bubble sort code */
#include <stdio.h>
int main()
{
int array[10], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
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 '>' */
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
return 0;
}
Output:
Enter number of elements
5
Enter 5 integers
50
10
40
20
30
Sorted list in ascending order:
10
20
30
40
50
END

You might also like