Improved Selection Sort Algorithm
Improved Selection Sort Algorithm
Improved Selection Sort Algorithm
net/publication/272621833
CITATIONS READS
8 7,735
4 authors, including:
Some of the authors of this publication are also working on these related projects:
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.
29
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015
Repeat for count=1 to k repetitions. If age ranges between 0 to 100 then each age
value could have a frequency of about 100,000
If (a[count]>a[max]) (10,000,000/100). In terms of population, more than half
Set max=count will be below the ages of fifty (50). The existing selection
sort will execute such list in the order of O(n2) in the worst
End if case scenario, but the proposed algorithm can do better. The
Interchange data at location k and max main concept of the proposed algorithm is to evaluate the
data in the list and keep track of distinct values in the list.
Set k k - 1 This makes it possible to perform multiple swapping at each
pass unlike the existing selection sort which performs at most
Table 1.0 shows the time complexity of the algorithm in
1 at each pass, hence reducing the run time for sorting the
three different situation of the input list.
list. The technique used is simple; a queue is maintained to
Table 1.0: Time Complexity of Selection Sort Algorithm keep the locations of all the values that are the same as the
value that is held as the Minimum or Maximum. At the end
Best case Worst case Average case of the list, all the locations on the queue are swapped into
their respective positions. Where the subsequence search
will begin from can be computed as (i i+x), where i
O(n2) O(n2) O(n2) points to the start of the unsorted partition and x is the
number of items that were dequeue. The worst case happens
Various improved selection sorting algorithms have been when there are no repetition in the list, but can guarantee best
proposed and all works better than the Selection Sort case run time O(n) when all the values in the list are the same
Algorithm. Optimized Selection Sort Algorithm (OSSA) or the number of distinct values is relatively small.
starts sorting the array from both ends. In a single iteration,
the smallest and largest elements in the unsorted part of the 2.1 Improved Selection Sort Algorithm
array are searched and swapped[2]. The array is logically
partition into three parts; lower-sorted, unsorted, upper- 1. Initialise i to 1
sorted. The search for the maximum and minimum is done 2. Repeat steps 3-5 until the i equals n.
in the unsorted partition and the minimum is moved to the
lower-sorted and the maximum to the upper-sorted. All 3. Search from the beginning of the unsorted part of
values in the upper-sorted are greater or equal to the values in the list to the end.
the lower-sorted. The process is continued until the whole 4. Enqueue the locations of all values that are the
list or array is sorted [3]. The algorithm is able to half the same as the Maximum value.
run time of the selection sort, O(n2)/2, which is better but still
exhibit a time complexity of O(n2). 5. Use the indices on the queue to perform swapping.
The concept of the Enhance Selection Sort Algorithm Example of Improved Selection Sort (Ascending Order)
(ESSA) is to memorize the location of the past maximum and
List – A[n]
start searching from that point in the subsequent iteration[2].
This enables the algorithm to avoid having to search for the Queue – Q[n]
maximum values form the beginning of the unsorted partition
to the end. This technique limits the number of comparisons Initial List
the algorithm performs during each iteration, hence A 2 2 1 5 2 5 4 4 5 5
performing better than the existing selection sort algorithm.
The arrangement of the elements of the list influences the run
time greatly. The same set of data may take different times
to be sorted as a result of their arrangement. The average Q
case of the algorithm is however O(n2).
Hybrid Select Sort Algorithm (HSSA) uses a technique that 1st Pass
prevent the algorithm from performing unnecessary iterations
by evaluating the content of the unsorted partition for ordered A 2 2 1 5 2 5 4 4 5 5
sequence so as to terminate quickly. When the list is fully
sorted or partially sorted, its run time is better when
compared with the existing selection algorithm. The modified Q 2
selection sort algorithm uses a single Boolean variable
„FLAG‟ to signal the termination of execution based on the Index 2 added to the queue.
order of the list, a[i-1] >= a[i] >=a[i+1] [6]. The best scenario
is when the list is already ordered, here the algorithm
terminate during the first pass, hence will have a run time of 2nd Pass
O(n). What this means is that, when data is not ordered, the
A 1 2 2 5 2 5 4 4 5 5
algorithm behaves generally like the old selection sort
algorithm.
2. CONCEPT OF IMPROVED Q 1 2 4
SELECTION SORT ALGORITHM Indices 1, 2 and 4 added to the queue because they all store
Generally, large data sample will contain a couple of the same value as the minimum value during the second (2 nd)
repetitions. For example sorting the ages of citizens of a Pass.
country of population of about 10 million will contain a lot of
30
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015
Q 6 7 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 After swapping
same value as the minimum value during the third (3rd) Pass. A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4]
4th Pass The Improved Selection Sort Algorithm (ISSA) is content
A 1 2 2 2 4 4 5 5 5 5 sensitive, in that the nature of data distribution of the list will
greatly influence the run time of the algorithm. The run time
of the ISSA depends on the number of distinct values that are
found in the list to be sorted. If the number of distinct values
Q 6 7 8 9
is big or equal to n, then the run time of the algorithm can be
Indices 6, 7, 8 and 9 added to the queue because they all approximated as O(n2). However, if the number is very
store the same value as the minimum value during the fourth small, the algorithm completes the sorting in the order of
(4th) Pass. O(n).
Sorted List Pseudocode
A 1 2 2 2 4 4 5 5 5 5 A[n]
Queue[n] // Same size as the size of the array
The list is sorted at the end of the fourth iteration or pass.
The existing selection sort will take more time to sort the i 0
same list. while i < (n-1)
Another example with original list as follows 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] if Max < A[j]
Q [4, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Max A[j]
Indices 4,14 are added to the queue 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 //Perform the swapping of values
A [1, 1, 4, 2, 2, 2, 2, 3, 3, 4, 4, 2, 3, 4, 2, 2, 3, 4, 4, 2] Front 0
Q [3, 4, 5, 6, 11, 14, 15, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] While (Front <= Rear)
Indices 3, 4, 5, 6, 11, 14, 15, 19 are added to the queue 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
Q [12, 14, 15, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] The Improved Selection Sort Algorithm is very simple to
analyse, considering the fact that the time complexity or run
Indices 12, 14, 15, 16 are added to the queue
time of the algorithm depends on two main factors.
After swapping
1. Size of list (n)
A [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4]
2. Number of distinct values in the list. dV
-----------------------------------------------------------------
Run Time = O(n.dV)
31
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015
Table 1shows the runtime of a set of n values with different Fig 2 illustrates the relationship between the number of
number of distinct values. distinct values in a list and the time needed to sort it. The
number is illustrated as a ratio of the size of the list (n). If
Table 1 Run time of Improved Selection Sort Algorithm the number of distinct value is half the size of the list, then
(ISSA) the algorithm will take about half the time the old selection
sort algorithm takes. From figure 2, as the number of distinct
Number of Run Time Big-O values decreases, the run time for the sorting also decrease.
Distinct Values
Decreasing distinct values:
1 T=n O(n) 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛
, , , ,…, , ,1
1 2 3 4 𝑛−2 𝑛−1
2 T= 2n O(n)
4. ANALYSIS OF SS, OSSA, ESSA,
3 T=3n O(n) HSSA AND ISSA WITH ASAMPLE
DATASET
... ... ... A given set of data of size 1000 was finally used to analyse
the performances of the various selection sort algorithms
n-2 T = (n-2)n O(n2) including Improved Selection Sort Algorithm (ISSA). The
number of redundancies in the set was quantified in terms of
percentages and 11 different sets of values were used to test
n T = n2 O(n2)
the algorithms. The data redundancies in set 1 through 11
were 0%, 10%, 20%, 30%, 40%,50%, 60%, 70%, 80%, 90%,
100%. Table 2 illustrates the run times for the various
algorithms on the various categories of the dataset.
Table 2: Estimated run times of various Selection Sort
algorithms when input dataset were not sorted
Number of distinct values and '70% 499500 250000 418296 498500 149850
worst case run time of ISSA
'80% 499500 250000 433374 498495 99900
25000
n '90% 499500 250000 463134 498465 49950
20000
'100% 499500 250000 49950 998 1000
15000
n/2
10000
5000 n/4
n/8
n/16
n/32
0
32
International Journal of Computer Applications (0975 – 8887)
Volume 110 – No. 5, January 2015
IJCATM : www.ijcaonline.org 33