Improved Selection Sort Algorithm
Improved Selection Sort Algorithm
Improved Selection Sort Algorithm
Obed Appiah
K. Riverson, Ph.D.
CSIR
Accra, Ghana
ABSTRACT
One of the basic problems of Computer Science is sorting a
list of items. It refers to the arrangement of numerical or
alphabetical or character data in statistical order. Bubble,
Insertion, Selection, Merge, and Quick sort are most common
ones and they all have different performances based on the
size of the list to be sorted. As the size of a list increases,
some of the sorting algorithm turns to perform better than
others and most cases programmers select algorithms that
perform well even as the size of the input data increases. As
the size of dataset increases, there is always the chance of
duplication or some form of redundancies occurring in the
list. For example, list of ages of students on a university
campus is likely to have majority of them repeating. A new
algorithm is proposed which can perform sorting faster than
most sorting algorithms in such cases. The improved
selection sort algorithm is a modification of the existing
selection sort, but here the number of passes needed to sort
the list is not solely based on the size of the list, but the
number of distinct values in the dataset. This offers a far
better performance as compared with the old selection sort in
the case where there are redundancies in the list.
General Terms
Algorithms, Sorting Algorithms
Keywords
Algorithms, sorting algorithms, selection sort, improved
selection sort, redundancies in dataset
1. INTRODUCTION
One of the basic problems of Computer Science is sorting a
list of items. This is the arrangement of a set of items either
in increasing or decreasing order. The formal definition of
the sorting problem is as follows:
Input: A sequence having n numbers in some random order
(a1, a2, a3, .. an)
Output: A permutation (a1, a2, a3, .. an) of the input
sequence such that
a1 a2 a3 .. an
For instance, if the given input of numbers is 59, 41, 31, 41,
26, 58, then the output sequence returned by a sorting
algorithm will be 26, 31, 41, 41, 58, 59 [1].
Sorting is considered as a fundamental operation in
Computer Science as it is used as an intermediate step in
many programs. For example, the binary search algorithm
(one of the fastest search algorithms) requires that data must
be sorted before the search could be done accurately at all
times. Data is generally sorted to facilitate the process of
searching. As a result of its vital or key role in computing,
several techniques for sorting have been proposed. The
bubble, insertion, selection, merge, heap, and quick sort are
29
Worst case
Average case
O(n2)
O(n2)
O(n2)
Initialise i to 1
2.
3.
4.
5.
1st Pass
2. CONCEPT OF IMPROVED
SELECTION SORT ALGORITHM
List A[n]
Queue Q[n]
Initial List
A
2nd Pass
5
30
4 Pass
2
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4]
Q [14, 15, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Indices 14, 15, 16, 17, 18, 19 are added to the queue
Indices 6 and 7 added to the queue because they all store the
same value as the minimum value during the third (3rd) Pass.
4th Pass
After swapping
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4]
Sorted List
Pseudocode
A[n]
Queue[n]
i 0
while i < (n-1)
Rear 0
Original List
Max A[i]
A=[2, 2, 4, 2, 1, 2, 2, 3, 3, 4, 4, 2, 3, 4, 1, 2, 3, 4, 4, 2]
Queue[Rear] i
-----------------------------------------------------------------
ji+1
1 Pass
while j<(n)
A [2, 2, 4, 2, 1, 2, 2, 3, 3, 4, 4, 2, 3, 4, 1, 2, 3, 4, 4, 2]
Q [4, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Max A[j]
Rear -1
After swapping
If Max = A[j]
A [1, 1, 4, 2, 2, 2, 2, 3, 3, 4, 4, 2, 3, 4, 2, 2, 3, 4, 4, 2]
Rear Rear + 1
-----------------------------------------------------------------
Queue[Rear] = j
2 Pass
A [1, 1, 4, 2, 2, 2, 2, 3, 3, 4, 4, 2, 3, 4, 2, 2, 3, 4, 4, 2]
Front 0
Temp A[Queue[Front]]
After swapping
A[Queue[Front]] A[i]
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 3, 4, 3, 3, 3, 4, 4, 4]
A[i] Temp
-----------------------------------------------------------------
ii+1
3 Pass
Front Front + 1
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 3, 4, 3, 3, 3, 4, 4, 4]
3. ANALYSES OF ISSA
1.
2.
31
Run Time
Big-O
T=n
O(n)
T= 2n
O(n)
T=3n
O(n)
...
...
...
n-2
T = (n-2)n
O(n2)
T = n2
O(n2)
, , , ,,
,
,1
1 2 3 4
2 1
Red.
SS
OSSA
ESSA
HSSA
ISSA
'0%
499500
250000
371580
498501
499500
'10%
499500
250000
377050
498501
449550
'20%
499500
250000
375967
498501
399600
'30%
499500
250000
378348
498501
349650
'40%
499500
250000
383873
498501
299700
'50%
499500
250000
398155
498498
249750
'60%
499500
250000
399608
498501
199800
'70%
499500
250000
418296
498500
149850
'80%
499500
250000
433374
498495
99900
'90%
499500
250000
463134
498465
49950
'100%
499500
250000
49950
998
1000
15000
10000
n/2
5000
n/4
n/8
n/16
n/32
32
600000
500000
400000
300000
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
200000
100000
SS
OSSA
ESSA
HSSA
'100%
'90%
'80%
'70%
'60%
'50%
'40%
'30%
'20%
'10%
'0%
ISSA
5. CONCLUSION
IJCATM : www.ijcaonline.org
33