(Dsa)
(Dsa)
(Dsa)
REPORT ON
BUBBLE SORT IN C
Maulana Abul Kalam Azad University of Technology
West Bengal
Submitted By : -
NIRAJ KUMAR(12500221073)
Finally, as one of the team members, I would like to appreciate all my group
members for their support and coordination, I hope we will achieve more in our
future endeavours.
Contents
1. Abstract
2. Introduction
3. Working & Algorithm
4. Implementation
5. The Complexity of Bubble Sort
6. Example
7. Conclusion
8. References
Abstract
Suppose we want to sort an array, let’s name it arr, with n elements in ascending
order; this is how the bubble sort algorithm will work.
1. Starts from the first index: arr[0] and compares the first and second
element: arr[0] and arr[1]
2. If arr[0] is greater than arr[1], they are swapped
3. Similarly, if arr[1] is greater than arr[2], they are swapped
4. The above process continues until the last element arr[n-1]
All four steps are repeated for each iteration. Upon completing each iteration, the
largest unsorted element is moved to the end of the array. Finally, the program
ends when no elements require swapping, giving us the array in ascending order.
Now that we know the working of the bubble sort let’s implement it in C
programming using different methods.
ALGORITHM:
bubbleSort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
end bubbleSort
This algorithm does the swapping of elements to get the final output in the desired
order. For instance, if you pass an array consisting of the elements: (6, 3, 8, 2, 5,
7), the final array after the bubble sort implementation will be: (2, 3, 5, 6, 7, 8).
Implementation
The bubble sort algorithm can be easily optimized by observing that the n-th pass
finds the n-th largest element and puts it into its final place. So, the inner loop can
avoid looking at the last n-1 items when running for the n-th time:
procedure bubble Sort( A : list of sortable items ) n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i])
swapped = true end if
end for n = n – 1
until not swapped end procedure More generally, it can happen that more than
one element is placed in their final position on a single pass. In particular, after
every pass, all elements after the last swap are sorted, and do not need to be
checked again. This allows us to skip over a lot of the elements, resulting in about
a worst case 50% improvement in comparison count (though no improvement in
swap counts ), and adds very little complexity because the new code subsumes
the "swapped" variable.
To accomplish this in pseudocode we write the following:
procedure bubbleSort (A : list of sortable items ) n = length(A)
repeat
new n = 0
for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i])
new n = i
end if end for n = newn
until n = 0 end procedure
THE COMPLEXITY OF BUBBLE SORT
Time Complexity:
Space Complexity:
● Time Complexity for the standard bubble sort algorithm is O(1) as there
is one additional variable required to hold the swapped elements
temporarily.
● Space complexity for optimized implementation of the bubble sort
algorithm is O(2). This is because two additional variables are required.
Example
#include <stdio.h>
int main(){
int arr[100],n,c,d,swap;
scanf(“%d”,&n);
printf(“enter %d integers\n’,n);
for(c=0;c<n;c++)scanf(%d”,&arr[c]);
for(c=0; c<(n-1);c++){
for(d=0;d<n-c;d++){
if(arr[d]>arr[d+1]){
swap=arr[d];
arr[d][=arr[d+1];
arr[d+1]=swap;}
Return 0;
}
Conclusion
It can be concluded that bubble sort is an effortless way of sorting the elements
of an array, thus having more time complexity. It is a stable and in-place
algorithm which is most used for introducing the concept of sorting algorithms.
It is also used in computer graphics because of its feature to detect the minute
errors and fix them in linear time.
The main disadvantage of Bubble sort can be seen while dealing with an array
containing a huge number of elements. As worst-case complexity of this
algorithm is O(n2), thus a lot more time is taken to sort them. Thus it is more
suitable for teaching sorting algorithms instead of real-life applications.
One of the main advantages of a bubble sort is that it is a very simple algorithm
to describe to a computer. There is only really one task to perform (compare two
values and, if needed, swap them). This makes for a very small and simple
computer program .
References