0% found this document useful (0 votes)
10 views5 pages

Comparison of Bubble Sort and Selection

Uploaded by

panopticonvoid
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)
10 views5 pages

Comparison of Bubble Sort and Selection

Uploaded by

panopticonvoid
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/ 5

Comparison of Bubble Sort and Selection Sort with

their Enhanced Versions


Muhammad Ali Raza, Haris Zulfiqar, Farukh Shahbaz, Daud Burhan

Registration BSEE01143183, BSEE01143160, BSEE01143157, BSEE01143308

4th Semester, Department of Electrical Engineering

University of Lahore, Pakistan

[email protected], [email protected], [email protected]

Abstract— In computer science the ordering of data in a list is  The technology used in e-commerce including
one of the most researched topics. In algorithm design, efficient public-key cryptography and digital signatures are
searching and sorting is necessary. Hence, comparison based based on numerical algorithms.
methods have been developed to solve these problems. In this
study, we compare the two most commonly used sorting Algorithms and data structures have utter importance for
algorithms, bubble sort and selection sort with their enhanced
permanently growing areas of engineering, economy, science
versions.
and daily life. The searching and ranking algorithms in search
Keywords— Enhanced selection sort, enhanced bubble sort, engines and the pattern matching algorithms crucial for
reading the human genome are only a few spectacular
selection sort, bubble sort.
examples how algorithms can change our life (Peter Sanders,
Dorothea Wagner, Karlsruhe Institute of Technology 2011) .
I. INTRODUCTION In this study two sorting algorithms, Bubble Sort and
Selection Sort are presented with their enhanced versions.
The use of data structures and algorithms has become an
Moreover, a thorough comparison has been shown between
essential part of any computer application since the age of
these two sorting algorithms.
computing. The data stored in several common data structures
such as arrays, linked lists, queues, stacks, binary trees and II. LITERATURE REVIEW
hash tables require the use of sorting algorithms, to organize
the stored data in a certain order. Many algorithmic techniques
A. Bubble Sort
have been developed overtime for solving computational
problems by designing new complex algorithms and Also known as sinking sort is a way of listing data by
implementing them efficiently [1]. repeatedly going through the list in steps called iterations. In
each step, adjacent pair of data is compared and swapped if
Some of the practical applications of algorithms include:
they are not in the correct order. In this way the smaller
 The Human Genome Project has identified all the elements, rise to the top of the list as if it’s a bubble and the
100,000 genes in human DNA through the use of larger elements move towards the bottom end of the list. The
sophisticated algorithms for finding out the sequence size of the list decreases by a decrement of one from n to 2.
of 3 billion chemical base pairs that make up human This has two outer and inner loops. The outer loop finds the
DNA. maximum or minimum number for replacing in the list. The
 The quick retrieval and access of large amounts of inner loop processes the selected list by the outer loop for
data on the internet has been done with the use of finding and replacing the smallest or largest number ([4], [6]).
algorithms.
The diagram below shows how the elements of an array of
size 5 get reordered in the first iteration.
the start of the list move to the top slowly. This has led to
these types of elements being named rabbits and turtles,
respectively [2, 20].

B. Enhanced Bubble Sort:

According to Jehad Alnihoudthe enhanced bubble sort


After the first iteration, the last position of the array [4] works by inserting an array of elements in the same array by
contains the largest value. searching the smallest and largest elements and interchange
The second diagram below shows the second iteration but the smallest with the first element and the largest with the
this time only array elements array [0] through array [3] is last element, and then decreasing the size of the array by two
considered. for next iteration ([2], [9]).

The procedure is as follows:

1. All elements of the array are added.


2. Initializing two variables, firstind = 0 and lastind =
size – 1.
3. Make an enhanced bubble sort function that takes
in array, its size, firstind and lastind as parameters of
the function.
The third diagram below shows the fourth iteration after 4. In that function, create two variables mincount and
the third iteration (not shown) containing the sorted array. maxcount. The operation of the function is to search
the maximum and minimum value in the array and
store their index respectively in maxcount and
mincount.
5. Put lastind = maxcount and firstind = mincount.
6. Then decrease the value of lastind by one and
increase the value of firstind by one. In this way the
size of the array becomes size – 2 after the first call
and after the second call, it becomes size – 4 and so
A simple code of bubble sort is shown below on.
7. Call the enhanced bubble sort recursively while the
for (int x = 0; x<n; x++)
size of the array is greater than one. Then it returns
{
the sorted array.
for (int y=0; y<n-1;y++)
{
C. Selection Sort:
if (array[y]>array[y+1])
{ Selection sort algorithm works by finding the smallest
int temp = array[y+1]; element in the array and exchanges it with the element in the
first position. Then, it finds the second smallest element and
array[y+1] = array[y];
exchanges it with the element in the second position. This
array[y] = temp;
continues until the array is sorted. Similar to bubble sort, the
} size of the list decreases by a decrement of one from n to 2 in
} each iteration. This has two outer and inner loops. The outer
} loop finds the maximum or minimum number for replacing in
The performance of the bubble sort is largely dependent on the list. The inner loop processes the selected list by the outer
the positions of the elements in the list. Large elements at the loop for finding and replacing the smallest or largest number
start of the list are swiftly swapped, while small elements at [5].
The diagram below shows the first iteration of a list of size This sorting algorithm is not difficult to analyse compared
5 where the smallest value is found and is swapped with the to others as none of the loops build on the data in the list. The
first element of the list. lowest element is selected by checking all elements of the list
and then moving them to the first position of the list. Next, the
remaining elements are scanned and moved accordingly. This
algorithm can be improved by reducing the number of
iterations.

D. Enhanced Selection Sort

Inserts elements in the array and finds the maximum


element to exchange it with the last element. For the next
iteration the size of the array is decreased by one. This reduces
the number of swap operations.
The second iteration below follows the same procedure as The procedure is as follows:
the first iteration. It finds the smallest value excluding the first 1. Inserting elements in the array.
element of the list and swaps with the second element of the 2. Creating Enhanced Selection Sort function with
list. parameters array and its size.
3. The function operates by finding the maximum
element and swapping it with the last index of the
same array.
4. Then decrease the size of the array by one.
5. Call the function of the Enhanced Selection Sort
recursively. The size of the array decrease after every
call.

III. COMPARISON OF BUBBLE SORT WITH ITS ENHANCED


VERSION

Bubble sort asymptotic in worst case is O (n2) due to the


For the rest of the iteration, the same procedure is followed number of iterations and comparisons required. In enhanced
which can be summed up as finding and moving the smallest version, the numbers of comparisons are decreased by two for
element to the beginning of the unsorted list. The simple code each call. For small input of array the difference is small
of selection sort is written below: between bubble sort and its enhanced version, but with larger
sized arrays it is clear that the enhanced version is much faster
for(int i = 0; I < n; i++)
than the bubble sort. In all cases, enhanced version makes O
{
(n/gn) comparisons and bubble sort makes O (n2) comparisons
Int min_index = I;
to sort n elements of array.
for(int y = I; y<n; y++)
{
From the table below, it is noted that enhanced version is
If(array[min_index]>array[i])
much faster than the bubble sort for larger number of elements
{
in an array.
min_index = i;
}
}
Int temp = array[i];
Array[i]= array[min_index];
Array[min_index] = temp;
}
Bubble sort uses more swap times but selection sort avoids
this. If the average number of comparisons is plotted then it
shows that bubble sort is much worse as the number of
elements increases. However, this is based on random input
which might not be true all the time.

IV. COMPARISON OF SELECTION SORT WITH ITS ENHANCED


VERSION
Jehad Alnihoud and Rami Mansi did a real –world case
study to sort students of Al al-Bayt University in Jordan by
Selection Sort performs O (n) swaps but the enhanced
the university number in ascending order. The execution time
version depends upon the condition of the input array. For
for the sorting algorithms is shown in the table below [10]:
example if the array is already sorted, the enhanced version
doesn’t operate on the array but selection sort needs to.
Reading from memory is less expensive than writing.
Similarities between selection sort and its enhanced version
are shown below in the table.

In all cases, this table shows that enhanced version is much


faster than selection sort.

V. COMPARISON BETWEEN SELECTION SORT AND BUBBLE


SORT WITH THEIR ENHANCED VERSIONS
From the above bar chart, it is clear that Enhanced
Bubble Sort shows the worst performance overall. There selection sort increases the efficiency of the selection sort and
are many other sorting algorithms that have better worst-case Enhanced bubble sort speeds up the bubble sort and enhances
or average complexity of O (nlogn) than the worst-case its efficiency.
complexity of O (n2) of bubble sort. The only advantage is So, Enhanced Selection Sort has O (n2) complexity, but is
that the when the list is already sorted the complexity is only faster than selection sort and enhanced bubble sort is much
O (n) of bubble sort ([7], [8]). faster than bubble sort as bubble sort performs O (n2)
operations but its enhanced version performs O(nlgn)
operations to sort n elements.

VI. CONCLUSION

In this study two sorting algorithms, bubble sort and


selection sort with their enhanced versions are presented with
their comparisons. They were analysed, implemented, tested
and compared giving positive results. As from experiments
[quotation], their enhanced sorting algorithms are much better
in relation with order of growth and machine running time.
Making the sorting algorithm efficient makes it easier to
handle larger dataset where less time is required for
processing data such as searching, sorting and retrieving.
However, if the algorithm is short and easy to understand,
fewer mistakes can be made in the future.

References
[1] Aho A., Hopcroft J., and Ullman J., The Design and
Analysis of Computer Algorithms, Addison Wesley, 1974.
[2] Bell D., “The Principles of Sorting,” Computer Journal of
the Association for Computing Machinery, vol. 1, no. 2,
pp. 71-77, 1958.
[3] Deitel H. and Deitel P., C++ How to Program, Prentice
Hall, 2001.
[4] en.wikipedia.org/wiki/Bubble_sort
[5] en.wikipedia.org/wiki/Selection_sort
[6] algorithmist.com/index.php/Bubble_sort
[7] cs.stackexchange.com/questions/13106/why-is-selection-
sort-faster-than-bubble-sort
[8] C++ Programming From problem analysis to program
design Fifth Edition DS Malik pg 569
[9] Enhancement of Selection, Bubble and Insertion Sorting
Algorithm May 25,2014 Research Journal Comsats
[10] An Enhancement of Major Sorting Algorithms Jehad
Alnihoud and Rami Mansi, Sep1 2008, Department of
Computer Science, Al al-Bayt University, Jordarn, Pg. 55

You might also like