Introduction To Algorithms (1997) : Steven Skiena
Introduction To Algorithms (1997) : Steven Skiena
n
j=2
t
j
+ c
6
n
j=2
(t
j
1)
+c
7
n
j=2
(t
j
1) + c
8
What are the t
j
s? They depend on the particular input.
Best Case
If its already sorted, all t
j
s are 1.
Hence, the best case time is
c
1
n + (c
2
+ c
4
+ c
5
+ c
8
)(n 1) = Cn + D
where C and D are constants.
Worst Case
If the input is sorted in descending order, we will have to
slide all of the already-sorted elements, so t
j
= j, and step 5
is executed
n
j=2
j = (n
2
+ n)/2 1