Documente Academic
Documente Profesional
Documente Cultură
2
lrdex0lLare 0
3
FOR j 1 TO r
1
|l (X[j|>X[lrdex0lLare))
5
lrdex0lLare j
Lare X[lrdex0lLare|
Z
X[lrdex0lLare| X[||
8
X[|| Lare
C. Best-Case Time Complexity of Old Selection Sort.
For the best-case scenario when we have all
Let
Then replacing the values of we have
Thus here in best-case, the complexity of execution time of an
algorithm shows the lower bound and is asymptotically
GHQRWHGZLWK7KHUHIRUHE\LJQRULQJWKHFRQVWDQWDEFDQG
the lower terms of n, and taking only the dominant term i.e. n
2
,
then the asymptotic running time of selection sort will be
Q
2
DQG ZLOO OLH LQ RI VHW RI DV\PSWRWLF IXQFWLRQ LH Q
2
).
Hence we can say that the asymptotic running time of old SS
will be:
D. Worst - Case Time Complexity.
Now for the worst-case scenario when we have all
so
we have
International Journal of Electrical & Computer Sciences IJECS-IJENS Vol: 11 No: 02 21
115002-3838 IJECS-IJENS April 2011 IJENS
I J E N S
(Eq: 01)
Let
If we suppose
and
put it in Equation 01
Then
Thus here in worst-case, the complexity of execution time of
an algorithm shows the upper bound and is asymptotically
denoted with Big-O. Therefore by ignoring the constant a, b,c
and the lower terms of n, and taking only the dominant term
i.e. n
2
, then the asymptotic running time of selection sort will
be of the order of O(n
2
) and will lie in of set of asymptotic
IXQFWLRQ LH Q
2
). Hence we can say that the asymptotic
running time of old SS will be:
It means that the best and worst case asymptotic running time
of selection sort is same i.e
2 DO |ey ;[M|
3 Pul ;[M| |rlo l|e
sorled sequerce ;[1
. . M - 1|
0
1 L M - 1
5 WHILE L > 0 ard ;[L| > |ey
DO ;[L 1| ;[L|
Z L L - 1
8 ;[L 1| |ey
C. Best-Case Time Complexity of Insertion Sort.
Equation 01
If t
j
= 1 for j = 2, 3, ..., n and the best-case running time can be
computed as follows:
Suppose
International Journal of Electrical & Computer Sciences IJECS-IJENS Vol: 11 No: 02 22
115002-3838 IJECS-IJENS April 2011 IJENS
I J E N S
And
Then
The punch line here is that the while-loop in line 5 executed
only once for each j. This happens if given array A is already
sorted.
= O(n)
It is a linear function of n.
D. Worst-Case Time Complexity of Insertion Sort.
When t
j
= j for j = 2, 3, ..., n then the worst-case running time
can be computed from equation (1) as follows:
(Eq: 02)
If we suppose
and
put it in Equation 02
Then
Eq: 03
Suppose
Then
This running time can be expressed as
for
constants a, b, and c that again depends on the statement costs
c
i
. Therefore, is a quadratic function of n.
We usually concentrate on finding the worst-case running
time: the longest running time for any input size n.
IV. OPTIMIZED SELECTION SORT ALGORITHM
(OSSA)
A. Optimized Selection Sort Algorithm
The three main characteristics of this algorithms is that, it is
in-place, stable and easy to understand as compared to other
algorithms of order O(n
2
) [15].
The working mechanism of OSSA is that it starts sorting the
array from both ends. In a single iteration, first the smallest
and largest element in the array are searched, and then both at
the same time are placed at the specific location. This process
is continued until the whole array is sorted [15].
Even though the order of OSSA is still O(n
2
), but its
performance level has been very much improved as compared
to other sorting algorithm of the said order i.e selection sort,
insertion sort etc [15].
B. Pseudo Code & Execution Time of Individual
Statement of Optimized Selection Sort Algorithm
Algorithm OSSA (X, n) Z
X[0..n-1]
S# Iteration Cost Times
1 | 0
2 lor | r - 1 lo |
3 lrdex0lLarelrdex0l3ra|||
1 lor j| 1 lo |
5 |l (X[j| > X(lrdex0lLare|)
lrdex0lLare j
Z |l (X[j| < X[lrdex0l3ra|||)
8 lrdex0l3ra|| j
9 Lare X[lrdex0lLare|
10 3ra|| X[lrdex0l3ra|||
11 X[lrdex0lLare| X[||
12 X[lrdex0l3ra||| X[||
13 |l (lrdex0lLar==|)
Terp X[||
International Journal of Electrical & Computer Sciences IJECS-IJENS Vol: 11 No: 02 23
115002-3838 IJECS-IJENS April 2011 IJENS
I J E N S
X[|| Lare
X[lrdex0l3ra||| Terp
11 E|se X[|| Lare
Terp X[||
X[|| 3ra||
X[lrdex0lLare| Terp
15 E|se X[|| 3ra||
| | 1
C. Best-Case Time Complexity of Optimized Selection
Sort.
Let
A. Worst-Case Time Complexity of Optimized Selection
Sort.
If we put values of all Cs equal to one then we get
International Journal of Electrical & Computer Sciences IJECS-IJENS Vol: 11 No: 02 24
115002-3838 IJECS-IJENS April 2011 IJENS
I J E N S
V. COMPARISON OF SELECTION SORT,
INSERTION SORT AND OPTIMIZED
SELECTION SORT ALGORITHM
Below is the table representing the calculated time, when
multiple values of n are used.
Below is the line graph plotted in response of data given in
above table.
The graph shows that optimized selection is far better than its
competitor algorithms of the same order of O (n
2
).
Conclusion
OSSA is basically based on the logic of old Selection Sort, but
it perform less amount of iteration thats why it save almost
fifty percent of the execution time. OSSA is a new comparison
based sorting algorithm, with an
K^^
/^
K^
^
n Calculated Time
of
SSA
Calculated Time of
Optimized SSA
Calculated Time of
Insertion Sort
n T(n) Old SS T(n) OSSA T(n)
InserT