You are on page 1of 9

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/272621833

Improved Selection Sort Algorithm


Article in International Journal of Computer Applications · January 2015
DOI: 10.5120/19314-0774

CITATIONS READS

8 7,735

4 authors, including:

James Ben Hayfron-Acquah Obed Appiah


Kwame Nkrumah University Of Science and Technology University of Energy and Natural Resources
59 PUBLICATIONS 145 CITATIONS 25 PUBLICATIONS 68 CITATIONS

SEE PROFILE SEE PROFILE

Some of the authors of this publication are also working on these related projects:

PhD thesis View project

Designing Algorithm for Malaria Diagnosis Using Fuzzy Logic for Treatment (AMDFLT) in Ghana View project
All content following this page was uploaded by Obed Appiah on 22 February 2015.

The user has requested enhancement of the downloaded file.


International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

Improved Selection Sort Algorithm


J. B. Hayfron-Acquah, Ph.D. Obed Appiah K. Riverson, Ph.D.
Department of Computer Science University of Energy and Natural CSIR
Kwame Nkrumah University of Resources Accra, Ghana
Science and Technology Sunyani, Ghana
Kumasi, Ghana

ABSTRACT increasing or decreasing order.


One of the basic problems of The formal definition of the
Computer Science is sorting a list sorting problem is as follows:
of items. It refers to the Input: A sequence having n
arrangement of numerical or numbers in some random order
alphabetical or character data in
statistical order. Bubble, (a1, a2, a3, ….. an)
Insertion, Selection, Merge, and
Output: A permutation (a‟1, a‟2,
Quick sort are most common ones
a‟3, ….. a‟n) of the input sequence
and they all have different
such that
performances based on the size of
the list to be sorted. As the size a‟1 ≤ a‟2 ≤ a‟3 ≤ ….. a‟n
of a list increases, some of the
sorting algorithm turns to perform For instance, if the given input of
better than others and most cases numbers is 59, 41, 31, 41, 26, 58,
programmers select algorithms then the output sequence returned
that perform well even as the size by a sorting algorithm will be 26,
of the input data increases. As the 31, 41, 41, 58, 59 [1].
size of dataset increases, there is Sorting is considered as a
always the chance of duplication fundamental operation in
or some form of redundancies Computer Science as it is used as
occurring in the list. For an intermediate step in many
example, list of ages of students programs. For example, the
on a university campus is likely to binary search algorithm (one of
have majority of them repeating. the fastest search algorithms)
A new algorithm is proposed requires that data must be sorted
which can perform sorting faster before the search could be done
than most sorting algorithms in accurately at all times. Data is
such cases. The improved generally sorted to facilitate the
selection sort algorithm is a process of searching. As a result
modification of the existing of its vital or key role in
selection sort, but here the computing, several techniques for
number of passes needed to sort sorting have been proposed. The
the list is not solely based on the bubble, insertion, selection,
size of the list, but the number of merge, heap, and quick sort are
distinct values in the dataset. some of the common sorting
This offers a far better algorithms. Due to high number
performance as compared with of sorting algorithms available,
the old selection sort in the case the best one for a particular
where there are redundancies in application depends on various
the list. factors which were summarised
by Jadoon et al. (2011) as:
General Terms
Algorithms, Sorting Algorithms • The size of the list
(number of elements to
Keywords be sorted).
Algorithms, sorting algorithms, • The extent up to which
selection sort, improved selection the given input sequence
sort, redundancies in dataset is already sorted.

1. INTRODUCTION • The probable constraints


on the given input
One of the basic problems of
values.
Computer Science is sorting a list
of items. This is the arrangement • The system architecture
of a set of items either in on which the sorting

29
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

operation will be Repeat steps 3 to 6 until


performed. k=1
• The type of storage Set max=0
devices to be used: main
Repeat for count=1 to
memory or disks [4].
k
Almost all the available sorting
If (a[count]>a[max])
algorithms can be categorized into
two groups based on their Set max=count
difficulty. The complexity of an
algorithm and its relative End if
effectiveness are directly correlated Interchange data at location k
[5]. A standardized notation i.e. and max
Big O(n), is used to describe the
complexity of an algorithm. In this Set k  k - 1
notation, the O represents the Table 1.0 shows the time
complexity of the algorithm and n complexity of the algorithm in
represents the size of the input data three different situation of the
values. The two groups of sorting input list.
algorithms are O(n2), which
includes the bubble, insertion, Table 1.0: Time Complexity of
selection sort and O(nlogn) which Selection Sort Algorithm
includes the merge, heap & quick Best case Worst case Average case
sort.
O(n2) O(n2) O(n2)
1.1 Selection Sort
Algorithm Various improved selection
The concept of the existing sorting algorithms have been
selection sort (SS) algorithm is proposed and all works better
simple and can easily be than the Selection Sort
implemented as compared to others Algorithm. Optimized Selection
such as the merge or quick sorting. Sort Algorithm (OSSA) starts
The algorithm does not need extra sorting the array from both ends.
memory space in order to perform In a single iteration, the smallest
the sorting. The SS simply and largest elements in the
partition the list into two main unsorted part of the array are
logical parts, the sorted part and the searched and swapped[2]. The
unsorted part. Any iteration picks array is logically partition into
a value form the unsorted and three parts; lower-sorted,
places it in the sorted list, making unsorted, uppersorted. The
the sort partition grow in size while search for the maximum and
the unsorted partition shrinks for minimum is done in the unsorted
each iteration. When adding to the partition and the minimum is
sorted list, the algorithm makes moved to the lower-sorted and
sure that the value is added at the the maximum to the upper-
right position to ensure an order sorted. All values in the upper-
sequence of the sorted partition. sorted are greater or equal to the
The process is terminated when the values in the lower-sorted. The
number of items or the size of the process is continued until the
unsorted is one (1). The procedure whole list or array is sorted [3].
to select a value to be moved to the The algorithm is able to half the
sorted list will return minimum run time of the selection sort,
value or maximum value in the O(n2)/2, which is better but still
unsorted partition, which will be exhibit a time complexity of
swapped to position the item O(n2).
correctly. The concept of the Enhance
Selection Sort Algorithm
1.2 Algorithm: Selection (ESSA) is to memorize the
Sort (a[], n) location of the past maximum
Here a is the unsorted input list and and start searching from that
n is the size of the list or number of point in the subsequent
items in the list. After completion iteration[2]. This enables the
of the algorithm the array will algorithm to avoid having to
become sorted. Variable max search for the maximum values
keeps the location of the maximum form the beginning of the
value in each iteration. k n-1 unsorted partition to the end.
This technique limits the number
30
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

of comparisons the algorithm values in the list. This makes it


performs during each iteration, possible to perform multiple
hence performing better than the swapping at each pass unlike the
existing selection sort algorithm. existing selection sort which
The arrangement of the elements performs at most 1 at each pass,
of the list influences the run time hence reducing the run time for
greatly. The same set of data sorting the list. The technique used
may take different times to be is simple; a queue is maintained to
sorted as a result of their keep the locations of all the values
arrangement. The average case that are the same as the value that
of the algorithm is however is held as the Minimum or
O(n2). Maximum. At the end of the list,
all the locations on the queue are
Hybrid Select Sort Algorithm
swapped into their respective
(HSSA) uses a technique that
positions. Where the subsequence
prevent the algorithm from
search will begin from can be
performing unnecessary
computed as (i  i+x),
iterations by evaluating the
where i points to the start of the
content of the unsorted partition
unsorted partition and x is the
for ordered sequence so as to
number of items that were
terminate quickly. When the list
dequeue. The worst case happens
is fully sorted or partially sorted,
when there are no repetition in the
its run time is better when
list, but can guarantee best case run
compared with the existing
time O(n) when all the values in
selection algorithm. The
the list are the same or the number
modified selection sort
of distinct values is relatively
algorithm uses a single Boolean
small.
variable
„FLAG‟ to signal the
termination of execution based
2.1 Improved Selection
on the order of the list, a[i-1] >= Sort Algorithm
a[i] >=a[i+1] [6]. The best 1. Initialise i to 1
scenario is when the list is
already ordered, here the 2. Repeat steps 3-5 until the
algorithm terminate during the i equals n.
first pass, hence will have a run 3. Search from the
time of O(n). What this means beginning of the unsorted
is that, when data is not ordered, part of the list to the end.
the algorithm behaves generally
like the old selection sort 4. Enqueue the locations of
algorithm. all values that are the
same as the Maximum
2. CONCEPT OF value.
IMPROVED 5. Use the indices on the
queue to perform
SELECTION swapping.
SORT
Example of Improved Selection
ALGORITHM Sort (Ascending Order)
Generally, large data sample will
contain a couple of repetitions. List – A[n]
For example sorting the ages of Q
citizens of a country of u
population of about 10 million e
will contain a lot of repetitions. u
If age ranges between 0 to 100 e
then each age value could have a
frequency of about 100,000 –
(10,000,000/100). In terms of
population, more than half will be Q
below the ages of fifty (50). The [
existing selection sort will execute n
such list in the order of O(n2) in the ]
worst case scenario, but the
proposed algorithm can do better. I
The main concept of the proposed n
algorithm is to evaluate the data in i
the list and keep track of distinct t
31
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

i A=[2, 2, 4, 2, 1, 2, 2, 3, 3, 4, 4, 2,
a 3, 4, 1, 2, 3, 4, 4, 2]
l
-----------------------------------------
------------------------
L
i 1 Pass
s
t A [2, 2, 4, 2, 1, 2, 2, 3, 3, 4, 4, 2,
3, 4, 1, 2, 3, 4, 4, 2]
A 2 2 1 5 2 5 Q [4, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0]
Q Indices 4,14 are added to the
queue
After swapping
1 Pass
A 2 2 1 5 2 5 4 4A [1,5 1, 4,52, 2, 2, 2, 3, 3, 4, 4, 2,
3, 4, 2, 2, 3, 4, 4, 2]
st -----------------------------------------
Q 2 ------------------------
Index 2 added to the queue. 2 Pass
A [1, 1, 4, 2, 2, 2, 2, 3, 3, 4, 4, 2,
3, 4, 2, 2, 3, 4, 4, 2]
2nd Pass
A 1 2 2 5 2 5 Q [3, 4, 5, 6, 11, 14, 15, 19, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Q 1 2 4 Indices 3, 4, 5, 6, 11, 14, 15, 19


are added to the queue
Indices 1, 2 and 4 added to the
queue because they all store the After swapping
same value as the minimum value A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4,
during the second (2nd) Pass. 3, 4, 3, 3, 3, 4, 4, 4]
3nd Pass -----------------------------------------
A 1 2 2 2 5 5 ------------------------
3 Pass

Q 6 7 A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4,
3, 4, 3, 3, 3, 4, 4, 4]
Indices 6 and 7 added to the
Q [12, 14, 15, 16, 0, 0, 0, 0, 0, 0,
queue because they all store the
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
same value as the minimum value
during the third (3rd) Pass. Indices 12, 14, 15, 16 are added
to the queue
4 Pass
th

A 1 2 2 2 4 4 After swapping
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
3, 3, 4, 4, 4, 4, 4, 4]
Q 6 7 8 9
-----------------------------------------
Indices 6, 7, 8 and 9 added to ------------------------
the queue because they all store
the same value as the minimum
value during the fourth (4th) 4 Pass
Pass. A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3,
3, 4, 4, 4, 4, 4, 4]
Sorted List
A 1 2 2 2 4 4 Q [14, 15, 16, 17, 18, 19, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The list is sorted at the end of the
fourth iteration or pass. The Indices 14, 15, 16, 17, 18, 19 are
existing selection sort will take added to the queue
more time to sort the same list.
After swapping
Another example with original list
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3,
as follows
3, 4, 4, 4, 4, 4, 4]
Original List

32
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

The Improved Selection Sort the algorithm depends on two main


Algorithm (ISSA) is content factors.
sensitive, in that the nature of data
1. Size of list (n)
distribution of the list will greatly
influence the run time of the 2. Number of distinct
algorithm. The run time of the values in the list.
ISSA depends on the number of dV
distinct values that are found in the
list to be sorted. If the number of Run Time = O(n.dV)
distinct values is big or equal to n, Table 1shows the runtime of a set
then the run time of the algorithm of n values with
can be approximated as O(n2). different number of
However, if the number is very distinct values.
small, the algorithm completes the
sorting in the order of O(n). Table 1 Run time of Improved
Selection Sort Algorithm
Pseudocode (ISSA)
A[n] Run Time Big-O
Number of
Queue[n] // Same size as Distinct Values
the size of the array i  0
1 T=n O(n)
while i < (n-1)
Rear  0 2 T= 2n O(n)

3 T=3n O(n)

... ... ...


Queue[R
ear]  i n-2 T = (n-2)n O(n2)

ji+1
n T = n2 O(n2)
while j<(n)
if Max < A[j]
Max 
Rear 
If Max = A[j]
Rear
1
Queue[Rear] =
j
//Perform the swapping
of values
Front  0 Figure 1: illustrates the
relationship between
While (Front <= Rear) distinct values in the list
Temp and the run time of the
A[Queue[Front]] ISSA.

A[Queue[Front]] Fig 1: Run time of list of size n


and number of distinct values
A[i]  using ISSA. The performance
of the Improved Selection could
Temp i
also be enhanced by introducing
i+1 the FLAG concept in the HSSA
to terminate sorting when the
Front  Front + 1
list is already sorted.
3. ANALYSES OF ISSA
The Improved Selection Sort
Algorithm is very simple to
analyse, considering the fact that
the time complexity or run time of

33
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

Table 2: Estimated run times


Number of distinct values and of various Selection Sort
algorithms when input
worst case run time of ISSA dataset were not sorted
25000 Red. SS OSSA ESSA HSSA ISSA
n
20000 '0% 499500 250000 371580 498501 499500

'10% 499500 250000 377050 498501 449550


15000
'20% 499500 250000 375967 498501 399600
n/2
10000
'30% 499500 250000 378348 498501 349650

5000 n/4
'40% 499500 250000 383873 498501 299700
n/8
n/16
n/32 499500
'50% 250000 398155 498498 249750
0

Figure 2: Number of '60% 499500 250000 399608 498501 199800


distinct
values and '70% 499500 250000 418296 498500 149850
run time
complexity '80% 499500 250000 433374 498495 99900
of ISSA.
Fig 2 illustrates the relationship '90% 499500 250000 463134 498465 49950
between the number of distinct
values in a list and the time needed '100% 499500 250000 49950 998 1000
to sort it. The number is illustrated
as a ratio of the size of the list (n).
If the number of distinct value is
half the size of the list, then the
algorithm will take about half the
time the old selection sort
algorithm takes. From figure 2, as
the number of distinct values
decreases, the run time for the
sorting also decrease.
Decreasing distinct values:

4. ANALYSIS OF SS,
OSSA, ESSA,
HSSA AND ISSA
WITH ASAMPLE
DATASET
A given set of data of size 1000 Figure 3: Estimated run times
was finally used to analyse the of various Selection Sort
performances of the various algorithms when input
selection sort algorithms including dataset were not sorted
Improved Selection Sort Algorithm The actual comparison of these
(ISSA). The number of selection sort algorithms could be
redundancies in the set was done when the dataset is
quantified in terms of percentages randomized. Here the
and 11 different sets of values were performance of the improved
used to test the algorithms. The selection sort algorithm (ISSA)
data redundancies in set 1 through recorded best performance when
11 were 0%, 10%, 20%, 30%, the percentages of redundancies
40%,50%, 60%, 70%, 80%, 90%, exceeds 50%
100%. Table 2 illustrates the run
times for the various algorithms on
the various categories of the
5. CONCLUSION
This paper proposed a new
dataset.
selection sort algorithm which
34
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015

performs better than the existing [2] Jadoon, S., Solehria, S. F.,
selection sort algorithm and in Qayum, M., “Optimized
most cases may have a run time in Selection Sort Algorithm is
order of O(n) which is ideal for faster than Insertion Sort
sorting relatively large set of data. Algorithm: a Comparative
The strength of the algorithm Study” International Journal
depends on the distinct values in of Electrical & Computer
the list and Sciences IJECS-IJENS Vol:
11 No: 02, 2011
[3] Jadoon, S., Faiz S., Rehman
S., Jan H., “Design &
Analysis of Optimized
Selection Sort Algorithm”,
IJECIJENS Volume 11 Issue
01, 2011.
[4] Khairullah, M. “Enhancing
Worst Sorting Algorithms”.
International Journal of
Advanced Science and
Technology Vol. 56, July,
2013
[5] Kapur, E., Kumar, P. and
Gupta, S., “Proposal of a two
way sorting algorithm and
performance comparison with
existing algorithms”.
International Journal of
Computer Science,
Engineering and Applications
(IJCSEA) Vol.2, No.3, June
2012
[6] “Design and Analysis of
Hybrid Selection Sort
Algorithm”. International
Journal of Applied Research
and Studies (iJARS) ISSN:
2278-9480 Volume 2, Issue
7 (July- 2013) www.ijars.in

IJCATM : www.ijcaonline.org

View publication stats

where there are more of such


redundancies or repetitions in the
list, it performs better than the
existing selection sort algorithm
and also a couple of the optimized
selection sort. In situation where
the number of distinct values is
very small, the algorithm may
perform better than even the quick
sort and merge sort algorithm
which have run time O(nlogn).

6. REFERENCES
[1] Cormen, T. H., Leiserson, C.
E., Rivest, R. L., and Stein, C.
2001. Introduction to
Algorithms. MIT Press.
Cambridge. MA. 2nd edition.
2001

35

You might also like