(Dsa)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

A

REPORT ON

BUBBLE SORT IN C
Maulana Abul Kalam Azad University of Technology
West Bengal

Submitted By : -
NIRAJ KUMAR(12500221073)

Dept. of Information Technology, IT 2nd Year


E-mail : - www.neerajkumar2004@gmail.com
Bengal College of Engineering and Technology
Durgapur, West Bengal ,713212

Under the guidance of:


Mrs. Kamini Kanchan (IT)
Mrs. Sulagna Basu Pobi (IT)
Mr. Chiranjeet Sarkar (IT)
Acknowledgement

On this great occasion of accomplishment of our project on Data Structure &


Algorithm, we would like to sincerely express our gratitude to Mrs. Kamini
Kanchan, who has been supported through the completion of this project. We
would also be thankful to our principal Mr PK Prasad of Bengal college of
Engineering and Technology for providing all the required facilities in
completion of this project.

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

Sorting algorithms provide a way to arrange a series of numbers or letters in some


predefined order based on some measurable quantity in the numbers or letters.
Thus we may arrange a series of numbers according to their values in an
increasing order or we may arrange the letters according to decreasing order of
their ASCII values using sorting. In this paper we will describe a simple and easy
to implement sorting algorithm called Bubble Sort.
It compares two adjacent elements to find which one is greater or lesser and
switches them based on the given condition until the final place of the element is
found. In this article, you will learn about bubble sort and how to write a C
program for bubble sort implementation using different ways.
Introduction

Bubble sort is a basic algorithm for arranging a string of numbers or other


elements in the correct order. The method works by examining each set of
adjacent elements in the string, from left to right, switching their positions if they
are out of order. The algorithm then repeats this process until it can run through
the entire string and find no two elements that need to be swapped.
Given a list of n numbers or letters the objective of any sorting algorithm is to
arrange the same in a particular order where the ordering is done based on some
intrinsic property of the inputs. The simplest way to sort a list of input values is
to compare them pairwise and obtain the proper ordering. Sorting algorithms that
achieve their goal by comparison are called comparison based sorting algorithm.
Bubble Sort is a simple and easy to implement comparison based sorting
algorithm. We will describe the algorithm by sorting a list of n numbers in
increasing order of magnitude. The same process can be followed to sort a list of
letters according to some criteria. The input to the algorithm will be a list of n
numbers in a random order and the output will be a list of the same n numbers
arranged in an increasing order of magnitude.
Working & Algorithm
WORKING:
As mentioned, the C program for bubble sort works by comparing and swapping
adjacent elements in an array. Let’s understand this in a step-by-step method:

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:

● Worst Case Complexity: If the array elements are in descending order


and we want to make it in ascending order, it is the worst case. The time
complexity for the worst case is O(n²).
● Best Case Complexity: The best case is when all the elements are
already sorted, and no swapping is required. The time complexity in this
scenario is O(n).
● Average Case Complexity: This is the case when the elements are
jumbled. The time complexity for the average case in bubble sort is
O(n²).

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;

printf(“Enter number of elements\n”);

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;}

Printf(“sorted list in ascending order :\n”,arr[c]);

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

● Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,


and Clifford Stein. Introduction to Algorithms, Second Edition. MIT
Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Problem 2-2,
pg.40.
● LET US C by Yashavant Kanetkar ,published by Manish jain for
BPBPublications,20Ansari road,New Delhi.
● Sorting in the Presence of Branch Prediction and Caches
● Fundamentals of Data Structures by Ellis Horowitz, Sartaj Sahni and
Susan Anderson-Freed ISBN 81-7371-605-6
● Owen Astrachan. Bubble Sort: An Archaeological Algorithmic
Analysis
● Computer Integrated Manufacturing by Spasic PhD, Srdic MSc, Open
Source, 1987.[1]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy