0% found this document useful (0 votes)
16 views3 pages

Ijirt142753 Paper

Uploaded by

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

Ijirt142753 Paper

Uploaded by

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

© November 2015 | IJIRT | Volume 2 Issue 6 | ISSN: 2349-6002

BUBBLE SORT
Priyanka kunjwal
Dronacharya College of Engineering
Haryana
When the list is already sorted (best-case), the
ABSTRACT:-Sorting algorithms provide a way complexity of bubble sort is only O (n). By contrast,
to arrange a series of numbers or letters in some most other algorithms, even those with better
predefined order based on some measurable average-case complexity, perform their entire
quantity in the numbers or letters. Thus we may sorting process on the set and thus are more
arrange a series of numbers according to their complex. However, not only does insertion sort have
values in an increasing order or we may arrange this mechanism too, but it also performs better on a
the letters according to decreasing order of their list that is substantially sorted (having a small
ASCII values using sorting. In this paper we will number of inversions).
describe a simple and easy to implement sorting
algorithm called Bubble Sort. Bubble sort should be avoided in the case of large
collections. It will not be efficient in the case of a
INTRODUCTION reverse-ordered collection

Given a list of n numbers or letters the objective of STEP BY STEP EXAMPLE


any sorting algorithm is to arrange the same in a
particular order where the ordering is done based on Let us take the array of numbers "5 1 4 2 8", and sort
some intrinsic property of the inputs. The simplest the array from lowest number to greatest number
way to sort a list of input values is to compare them using bubble sort. In each step, elements written in
pairwise and obtain the proper ordering. Sorting bold are being compared. Three passes will be
algorithms that achieve their goal by comparison are required.
called comparison based sorting algorithm. Bubble
Sort is a simple and easy to implement comparison First Pass
based sorting algorithm. We will describe the
algorithm by sorting a list of n numbers in increasing (5 1 4 2 8) (1 5 4 2 8), Here, algorithm compares
order of magnitude. The same process can be the first two elements, and swaps since 5 > 1.
followed to sort a list of letters according to some
criteria. The input to the algorithm will be a list of n (1 5 4 2 8) ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5
numbers in a random order and the output will be a 28) ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 )
list of the same n numbers arranged in an increasing ( 1 4 2 5 8 ),
order of magnitude.
Now, since these elements are already in order (8 >
PERFORMANCE 5), algorithm does not swap them.

Bubble sort has worst-case and average complexity Second Pass


both О (n2), where n is the number of items being
sorted. There exist many sorting algorithms with (1 4 2 5 8) (1 4 2 5 8)
substantially better worst-case or average
complexity of O (n log n). Even other О (n2) sorting (1 4 2 5 8) ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4
algorithms, such as insertion sort, tend to have better 58) (12458)
performance than bubble sort. Therefore, bubble sort
is not a practical sorting algorithm when n is large. (12458) (12458)

The only significant advantage that bubble sort has Now, the array is already sorted, but the algorithm
over most other implementations, even quicksort, does not know if it is completed. The algorithm
but not insertion sort, is that the ability to detect that needs one whole pass without any swap to know it
the list is sorted is efficiently built into the algorithm. is sorted.

IJIRT 142753 INTERNATIONAL JOURNAL OF INNOVATIVE RESEARCH IN TECHNOLOGY 265


© November 2015 | IJIRT | Volume 2 Issue 6 | ISSN: 2349-6002

Third Pass
for i = 1 to n-1 inclusive do if A[i-1] > A[i] then
(1 2 4 5 8) (1 2 4 5 8) (1 2 4 5 8) (1 2 4 5 8) swap(A[i-1], A[i])
(1 2 4 5 8) (1 2 4 5 8) (1 2 4 5 8) (1 2 4 5 8)
new n = i
IMPLEMENTATION
end if end for n = newn

until n = 0 end procedure


PSEUDOCODE IMPLEMENTATION:
BUBBLE SORT EXAMPLE
The bubble sort algorithm can be easily optimized
by observing that the n-th pass finds the n-th largest /* Bubble sort code */
element and puts it into its final place. So, the inner
loop can avoid looking at the last n-1 items when #include <stdio.h>
running for the n-th time:
int main()
procedure bubble Sort( A : list of sortable items ) n
= length(A) {

repeat int array[100], n, c, d, swap;


swapped = false
printf("Enter number of elements\n"); scanf("%d",
for i = 1 to n-1 inclusive do if A[i-1] > A[i] then &n);
swap(A[i-1], A[i])
printf("Enter %d integers\n", n);
swapped = true end if
for (c = 0; c < n; c++) scanf("%d", &array[c]);
end for n = n - 1
for (c = 0 ; c < ( n - 1 ); c++)
until not swapped end procedure
{
More generally, it can happen that more than one for (d = 0 ; d < n - c - 1; d++)
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 if (array[d] > array[d+1]) /* For decreasing order
checked again. This allows us to skip over a lot of use < */
the elements, resulting in about a worst case 50% {
improvement in comparison count (though no swap = array[d];
improvement in swap counts ), and adds very little array[d] = array[d+1];
complexity because the new code subsumes the array[d+1] = swap;
"swapped" variable. }
}
}
To accomplish this in pseudocode we write the
following: printf("Sorted list in ascending order:\n");

procedure bubbleSort( A : list of sortable items ) n = for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]);
length(A)
repeat return 0;
}
new n = 0

IJIRT 142753 INTERNATIONAL JOURNAL OF INNOVATIVE RESEARCH IN TECHNOLOGY 266


© November 2015 | IJIRT | Volume 2 Issue 6 | ISSN: 2349-6002

OUTPUT :

REFRENCES:

[1] 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.

[2] Sorting in the Presence of Branch


Prediction and Caches

[3] 3. Fundamentals of Data Structures by Ellis


Horowitz, Sartaj Sahni and Susan
Anderson-Freed ISBN 81-7371-605-6

IJIRT 142753 INTERNATIONAL JOURNAL OF INNOVATIVE RESEARCH IN TECHNOLOGY 267

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