SlideShare a Scribd company logo
Centro de Investigación y Estudios Avanzados
CINVESTAV UNIDAD GUADALAJARA
Computer Science
Student: Luis Adrian Parra Avellaneda
Analysis of Algorithms
P.H.D Hugo Iván Piza
Analysis of Selection Sort and Optimized Bubble Sort
September 2016
1
Index
Contents
Selection Sort ................................................................................................................................ 2
Is there a loop invariant? ................................................................................................................. 2
Is the subarray [1 … p] sorted?....................................................................................................... 2
Stability of the algorithm .................................................................................................................. 3
Mathematical analysis of selection sort........................................................................................... 3
Equation of selection sort ............................................................................................................ 4
Best case for selection sort ......................................................................................................... 4
The worst case is when tp=p and A = n-1.................................................................................... 4
Analysis of only comparisons.......................................................................................................... 5
Analysis of only assignations .......................................................................................................... 6
Graphics and verifications ............................................................................................................... 7
Best case of selection sort .......................................................................................................... 7
Worst case of selection sort ........................................................................................................ 8
Best vs Worst .................................................................................................................................. 9
Optimized bubble sort .................................................................................................................... 10
Is there a loop invariant? ............................................................................................................... 10
Is the subarray [1 … p] sorted?..................................................................................................... 10
Mathematical analysis of optimized bubble sort............................................................................ 11
Analysis of the equation of bubble sort ......................................................................................... 11
The best case for Bubble Sort................................................................................................... 11
The worst case for Bubble Sort ................................................................................................. 12
Analysis of only comparisons........................................................................................................ 12
Best case of comparisons ......................................................................................................... 13
Worst case of comparisons ....................................................................................................... 13
Analysis of only assignations ........................................................................................................ 13
Best case of assignations.......................................................................................................... 13
Worst case of assignations ....................................................................................................... 14
Graphics and verifications ............................................................................................................. 14
Bibliography....................................................................................................................................... 17
2
Selection Sort
The selection sort algorithm consists in separate the array un two list, the first list will be
sorted, and the other list don´t, the algorithm finds the minimum element in the second sub
array and gong to replace the element in the pivot to the minimum element in the second
sub array, and the process will be repeated for each index of the array. Below is showed the
code of the selection sort:
for (j=1 to n-1)
int min=j;
for(i=j+1 to n)
if(a[i] < a[min])
min=i;
if(min!=j)
swap(a[j], a[min]);
Is there a loop invariant?
The array is divided in two sections, the left array and right subarray, the value in the pivot
is always greater than the maximum value of the left subarray, and the elements in the left
subarray are sorted
1. Initialization: The array is trivially sorted when the size of this is 1, and this element
are the minimum and maximum
2. Maintenance: The sub array A [1…. p-1] is sorted in each iteration, and the element
in the pivot is greater than the maximum element in the sub array A [1…. p-1]
3. Termination: In the last iterations, the position of the pivot is n-1, this the elements
in the array [1…n-1] are sorted, and the pivot is the minimum value of the right
subarray, however the size of the subarray is 1, and the element in the pivot is the
minimum element, but is greater than the maximum element in the left subarray, thus
the array is sorted
The variable min always takes the minimum value of the right subarray A [p…n]
1. Initialization: When the array consists in one single element, the maximum value
will be this element, min=A [1]
2. Maintenance: The algorithm will search the minimum element in the sub array A [p
… n], because A [1] <A [2] <…A [p]…A [n], the inner loops will search the minimum
element, and in the last iteration of the inner loop the variable A[p] will be the
minimum value of the right subarray
3. Termination: In the last iterations, the value A[n] is the greater, and A [1] <A [2]
<…A [n], and thus this invariant is correct
Is the subarray [1 … p] sorted?
Yes, because the element in the pivot is the minimum element of the [p+1 … n] subarray,
and this example was showed in the loop invariant example, but this can prove by induction
Base:
3
𝑊ℎ𝑒𝑛 𝑡ℎ𝑒 𝑎𝑟𝑟𝑎𝑦 𝑠𝑖𝑧𝑒 𝑖𝑠 1, 𝐴[1]𝑖𝑠 𝑠𝑜𝑟𝑡𝑒𝑑
Induction Step:
We know that the A[p] contains the minimum element of the right subarray, thus each
element of the array A [1 … p] is sorted
𝐴[1] < 𝐴[2] < ⋯ < 𝐴[𝑝]
Stability of the algorithm
The algorithm is not stable, in the next example is showed an example of that
A1 and A2 are equals
𝐴𝑟𝑟𝑎𝑦 = 𝐴1 𝐴2 … … … … . 𝐵 𝐶 𝐷
Where C is the minimum value in the first iteration
𝐴𝑟𝑟𝑎𝑦 = 𝐶𝐴2 … … … … . 𝐵 𝐴1 𝐷
And now, B is the minimum value in the second iteration, A2 and B will be swapped
𝐴𝑟𝑟𝑎𝑦 = 𝐶𝐵 … … … … . 𝐴2 𝐴1 𝐷
Thus, these variables not remain the same order, thus this algorithm is not stable
Mathematical analysis of selection sort
Selection Sort Cost Times
for (j = 1; j < n; j++) C1 𝑛
int Min = j; C2 𝑛 − 1
for ( i = j+1; i <= n; i++) C3
∑ 𝑘 =
𝑛(𝑛 + 1)
2
𝑛
𝑘=2
if (a[i] < a[Min]) C4
∑ 𝑘 − 1 =
𝑛(𝑛 − 1)
2
𝑛
𝑘=2
Min = i; C5
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
if(Min != j) C6 𝑛 − 1
swap(a[j], a[Min]); C7 3 ∗ 𝐴 𝑤ℎ𝑒𝑟𝑒 𝐴 → {𝑛 − 1 ≥ 𝐴 ≥ 0}
4
Equation of selection sort
𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
) + 𝐶5 ∑( 𝑡𝑝 − 1) + 𝐶6( 𝑛 − 1) + 3
𝑛
𝑝=2
𝐶7
∗ 𝐴
Best case for selection sort
The best case running time when tp=1, and A = 0:
𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
) + 𝐶5 ∑(1 − 1) + 𝐶6( 𝑛 − 1) + 3𝐶7 ∗ 0
𝑛
𝑝=2
𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
) + 𝐶5 ∑(0) + 𝐶6( 𝑛 − 1) + 3𝐶7(0)
𝑛
𝑝=2
𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
) + 𝐶6( 𝑛 − 1)
𝑇(𝑛) = (
𝐶3
2
+
𝐶4
2
) 𝑛2
+ (𝐶1 + 𝐶2 +
𝐶3
2
−
𝐶4
2
) 𝑛 − (𝐶2 − 𝐶3)
𝑇(𝑛) = 𝐶1𝑛2
+ 𝐶2𝑛 + 𝐶3
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛²
The worst case is when tp=p and A = n-1
𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
)
+ 𝐶5 ∑( 𝑝 − 1) + 𝐶6( 𝑛 − 1) + 𝐶7(𝑛 − 1)
𝑛
𝑝=2
Selection Sort Cost Times
for (j = 1; j < n; j++) C1 𝑛
int Min = j; C2 𝑛 − 1
for ( i = j+1; i <= n; i++) C3
∑ 𝑘 =
𝑛(𝑛 + 1)
2
𝑛
𝑘=2
if (a[i] < a[Min]) C4
∑ 𝑘 − 1 =
𝑛(𝑛 − 1)
2
𝑛
𝑘=2
Min = i; C5
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
if(Min != j) C6 𝑛 − 1
swap(a[j], a[Min]); C7 3 ∗ 𝐴 𝑤ℎ𝑒𝑟𝑒 𝐴 → {𝑛 − 1 ≥ 𝐴 ≥ 0}
5
𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
) + 𝐶5 (
𝑛( 𝑛 − 1)
2
− ( 𝑛 − 1))
+ 𝐶6( 𝑛 − 1) + 𝑐7(𝑛 − 1)
𝑇(𝑛) = [𝐶3 + 𝐶4 +
𝐶5
2
] 𝑛2
+ [𝐶1 + 𝐶2 +
𝐶3
2
−
𝐶4
2
+ 𝐶5 + 𝐶6 + 𝐶7] 𝑛 + [𝑐2 + 𝑐5 − 𝑐6 − 𝑐7]
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛²
Why the complexity is n² in the best and the worst case? Because in the best case, the
algorithm remains make comparisons, and doesn´t have a flag that indicate that the array
is sorted
Analysis of only comparisons
SELECTION SORT COST TIMES
for (j = 1; j < n; j++) C1 𝑛
int Min = j; C2
for ( i = j+1; i <= n; i++) C3
∑ 𝑘 =
𝑛(𝑛 + 1)
2
𝑛
𝑘=2
if (a[i] < a[Min]) C4
∑ 𝑘 − 1 =
𝑛(𝑛 − 1)
2
𝑛
𝑘=2
Min = i; C5
if(Min != j) C6 𝑛 − 1
swap(a[j], a[Min]); C7
𝑇(𝑛) = 𝐶1𝑛 + 𝐶3 (
𝑛(𝑛 + 1)
2
) + 𝑐4 (
𝑛(𝑛 − 1)
2
) + 𝐶6(𝑛 − 1)
We watch that the tp variable is not here, thus, in the best case and the worst case the
number of comparisons are the same, and the equation not depends of tp, thus the
equation in the best and worst case is:
𝑇(𝑛) = [
𝐶3
2
+
𝐶4
2
] 𝑛2
+ [𝐶1 +
𝐶3
2
−
𝐶4
2
+ 𝐶6] 𝑛 − 𝐶6
And the complexity is
𝑇(𝑛) = [
𝐶3
2
+
𝐶4
2
] 𝑛2
+ [𝐶1 +
𝐶3
2
−
𝐶4
2
+ 𝐶6] 𝑛 − 𝐶6
𝑇(𝑛) = 𝑂(𝑛2
)
6
Analysis of only assignations
SELECTION SORT COST TIMES
for (j = 1; j < n; j++) C1
int Min = j; C2 𝑛 − 1
for ( i = j+1; i <= n; i++) C3
if (a[i] < a[Min]) C4
Min = i; C5
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
if(Min != j) C6
swap(a[j], a[Min]); C7 𝐵𝑒𝑠𝑡: 0 𝑊𝑜𝑟𝑠𝑡 = 3 ∗ (𝑛 − 1)
The equations that represents the time of this algorithm is the following:
In the best case, p=1, and in the swap block, the instructions are not executed
𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ∑(1 − 1)
𝑛
𝑝=2
+ 𝐶7 ∗ 0
𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ∑ 0
𝑛
𝑝=2
+ 0
𝑇(𝑛) = 𝐶2(𝑛 − 1)
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛
We watch that in the best case, the number of assignations is linear, now let´s analyze the
equation in the worst case, in the worst case tp=p
𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ∑(𝑝 − 1)
𝑛
𝑝=2
+ 3𝐶7(𝑛 − 1)
𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 (
𝑛(𝑛 − 1)
2
) + 3𝐶7(𝑛 − 1)
𝑇(𝑛) =
𝐶5
2
𝑛2
+ [𝐶2 −
𝐶5
2
+ 3𝐶7] 𝑛 − [𝐶2 − 3𝐶7]
𝑇(𝑛) = 𝑂(𝑛2)
In the worst case, the complexity using only assignations is n²
7
Graphics and verifications
A java program was written to prove that the analysis of the algorithms was correct, this
program counts the instructions executed in the best and the worst case
Best case of selection sort
8
Worst case of selection sort
In the two graphics the number of instructions are different, however, in two cases are
showed a quadratic graphic
9
Best vs Worst
With this graphics is proved that que complexity in the worst and best case is n²
10
Optimized bubble sort
The behavior of the bubble sort algorithm is similar to bubbles behavior; each element is
swapped in one position for each iteration, the optimized version of bubble sort contains a
flag that indicates if the array is ordered, when this happened the outer cycle will be break.
The algorithm is showed below:
for(int i=1; i<=n; i++)
bool flag = false;
for(int j=1; j<=n-i; j++)
if(array[j]>array[j+1])
flag = true;
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
if(!flag)
return;
Is there a loop invariant?
The invariant is that the elements in the subarray [1 … p] are the same elements, and
always is displaced the greater value
1. Initialization: The array is trivially sorted when the size of this is 1
2. Maintenance: The array A [1 … p] contains the same elements, because the
element which is displaced is the maximum element, and the other elements remains
in the subarray, and the greater value is displaced to the top (p+1)
3. Termination: The algorithm is finish when the maximum p is 1, the sub array in the
right is sorted, and if the value in the position 1 is greater than A[2], this will be
swapped, and finally the array will be sorted
Is the subarray [1 … p] sorted?
The subarray [1 … p] is not sorted in, because this algorithm put the great value in p position,
but not order the [1 … p] subarray, example:
[2, 3, 7, 1, 5]
The next iterations of the array will be showed
[3, 2,1,5,7] → [2,1,3,5,7] → [1,2,3,5,7]
However, the subarray [p … n] is sorted in each iteration, because the value in the pivot is
the greater value of [1 … p] subarray.
Thus, the subarray [1 … p] will be sorted if we make changes to the algorithm for displace
the numbers in inverse order.
11
Mathematical analysis of optimized bubble sort
Bubble Sort Cost Times
for(int i=1; i<=n; i++) C1 𝑛 + 1
bool flag = false; C2 𝑛
for(int j=1; j<=n-i; j++) C3 𝑛(𝑛 + 1)
2
if(array[j]>array[j+1]) C4 𝑛(𝑛 − 1)
2
flag = true; C5
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
int temp = array[j+1]; C6
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
array[j+1] = array[j]; C7
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
array[j] = temp; C8
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
if(!flag) C9 𝑛
return; C10 Max=1, Min=0
Analysis of the equation of bubble sort
The equations that describes the execution time for bubble sort algorithm is the following,
in that is not included the restrictions for the flag
𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
)
+ [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ∑( 𝑡𝑝 − 1) + 𝐶9𝑛 + 𝐶10
𝑛
𝑝=2
The best case for Bubble Sort
When tp=1 is the best case, and the equation will be analyzed below
𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
)
+ [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ∑(1 − 1) + 𝐶9𝑛 + 𝐶10 ∗ 1
𝑛
𝑝=2
But, what happens when the array is ordered, the final flag will break the cycle, thus, the cycle will be executed
only once, and, all the variables N will be changed to 1
𝑇(𝑛) = 𝐶1 + 𝐶2 + 𝐶3 (
𝑛(1 + 1)
2
) + 𝐶4 (
𝑛(1 − 1)
2
) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ∑(1 − 1) + 𝐶9 + 𝐶10
𝑛
𝑝=2
Why the n in the inner loops remains, because, the inner loops will be executed n times, but the outer loop only
going to run once, and the outer cycle never reach the end, thus the last comparison is not executed
12
𝑇(𝑛) = 𝐶1 + 𝐶2 + 𝐶3( 𝑛) + 𝐶9 + 𝐶10
𝑇(𝑛) = 𝐶3𝑛 + (𝐶1 + 𝐶2 + 𝐶9 + 𝐶10)
𝑇(𝑛) = 𝑎𝑛 + 𝑏
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛
The worst case for Bubble Sort
The worst case is when the array is totally inverted, thus the flag is never executed
in this case, and tp=p
𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
)
+ [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8]∑( 𝑝 − 1) + 𝐶9𝑛 + 𝐶10 ∗ 0
𝑛
𝑝=2
𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 (
𝑛( 𝑛 + 1)
2
) + 𝐶4 (
𝑛( 𝑛 − 1)
2
) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] (
𝑛( 𝑛 − 1)
2
) + 𝐶9𝑛
𝑇(𝑛) =
[𝐶3 + 𝐶4 + 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8]
2
𝑛2
+ [𝐶1 + 𝐶2 +
𝐶3
2
−
𝐶4
2
−
𝐶5
2
−
𝐶6
2
−
𝐶7
2
−
𝐶8
2
+ 𝐶9] 𝑛 + 𝐶1
𝑇(𝑛) = 𝑎𝑛2
+ 𝑏𝑛 + 𝑐
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 𝑂(𝑛²)
Analysis of only comparisons
Bubble Sort Cost Times
for(int i=1; i<=n; i++) C1 𝐵𝑒𝑠𝑡 = 1, 𝑊𝑜𝑟𝑠𝑡 = 𝑛 + 1
bool flag = false; C2
for(int j=1; j<=n-i; j++) C3 𝐵𝑒𝑠𝑡 = 𝑛, 𝑊𝑜𝑟𝑠𝑡 =
𝑛(𝑛 + 1)
2
if(array[j]>array[j+1]) C4 𝐵𝑒𝑠𝑡 = 𝑛 − 1, 𝑊𝑜𝑟𝑠𝑡 =
𝑛(𝑛 − 1)
2
flag = true; C5
int temp = array[j+1]; C6
array[j+1] = array[j]; C7
array[j] = temp; C8
if(!flag) C9 𝑊𝑜𝑟𝑠𝑡 = 𝑛, 𝐵𝑒𝑠𝑡 = 1
return; C10
13
Best case of comparisons
In the best case, the flag is activated in the first iteration, thus in the table there are 2
cases for the values, in this case the outer loop will run once, the equation for the best
case is showed below:
𝑇(𝑛) = 𝐶1 + 𝐶3𝑛 + 𝐶4(𝑛 − 1) + 𝐶9
𝑇(𝑛) = (𝐶3 + 𝐶4)𝑛 + (𝐶1 − 𝐶4 + 𝐶9)
𝑇(𝑛) = 𝑎𝑛 + 𝑏
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛 (𝐿𝑖𝑛𝑒𝑎𝑟)
Worst case of comparisons
In the worst case the flag is never executed
𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶3 (
𝑛(𝑛 + 1)
2
) + 𝐶4 (
𝑛(𝑛 − 1)
2
) + 𝐶9𝑛
𝑇(𝑛) =
(𝐶3 − 𝐶4)
2
𝑛2
+ (𝐶1 +
𝐶3
2
−
𝐶4
2
+ 𝐶9) 𝑛 + 𝐶1
𝑇(𝑛) = 𝑎𝑛2
+ 𝑏𝑛 + 𝑐
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑂(𝑛2
)
Analysis of only assignations
Bubble Sort Cost Times
for(int i=1; i<=n; i++) C1
bool flag = false; C2 𝑊𝑜𝑟𝑠𝑡 = 𝑛, 𝐵𝑒𝑠𝑡 = 1
for(int j=1; j<=n-i; j++) C3
if(array[j]>array[j+1]) C4
flag = true; C5
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
int temp = array[j+1]; C6
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
array[j+1] = array[j]; C7
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
array[j] = temp; C8
∑ 𝑡𝑝 − 1
𝑛
𝑝=2
if(!flag) C9
return; C10
Best case of assignations
In the best case, the flag is activated in the first iteration, and tp=1, and the equation is:
𝑇(𝑛) = 𝐶2 + (𝐶5 + 𝐶6 + 𝐶7 + 𝐶8) ∑(1 − 1)
𝑛
𝑝=2
14
𝑇(𝑛) = 𝐶2
𝑇(𝑛) = 𝑎
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝐶𝑜𝑛𝑠𝑡𝑎𝑛𝑡
Thus the algorithm never joins into the IF in the inner loop, and only the assignation of the
flag is executed
Worst case of assignations
In the worst case, the flag is not activated in each iteration of the outer loop, and tp=p, and
the equation is:
𝑇(𝑛) = 𝐶2𝑛 + (𝐶5 + 𝐶6 + 𝐶7 + 𝐶8) ∑(𝑝 − 1)
𝑛
𝑝=2
𝑇(𝑛) = 𝐶2𝑛 +
(𝐶5 + 𝐶6 + 𝐶7 + 𝐶8)𝑛(𝑛 − 1)
2
𝑇(𝑛) =
𝐶5 + 𝐶6 + 𝐶7 + 𝐶8
2
𝑛2
+ ( 𝐶2 −
𝐶5 + 𝐶6 + 𝐶7 + 𝐶8
2
) 𝑛
𝑇(𝑛) = 𝑎𝑛2
+ 𝑏𝑛
𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑂(𝑛²)
Graphics and verifications
In the worst case is proved with this graphic that the complexity is n²
15
And in the best case should be n, or linear, this is showed in the following graphic
In that graphics is a linear function, and now the comparison between the worst and the
best case will be showed
16
The difference between the functions is terrific, and finally, the values of the size of the
problem will be changed for more details
17
Bibliography
 Cormen, Stein, Rivest, Leiserson (2001). “Introduction to algorithms”, 2nd
edition,
McGrawHill. United States
 Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. (1974) The Design and
Analysis of Computer Algorithms. Addison-Wesley
 Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman.(1983) Data Structures and
Algorithms. Addison-Wesley.

More Related Content

PPT
Insertion sort bubble sort selection sort
PPT
Two dimensional array
PDF
Singly linked list
PPT
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
PPTX
Timing and control unit
PPTX
Unit 1 polynomial manipulation
PPTX
Linked List
PPTX
Binary Tree in Data Structure
Insertion sort bubble sort selection sort
Two dimensional array
Singly linked list
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
Timing and control unit
Unit 1 polynomial manipulation
Linked List
Binary Tree in Data Structure

What's hot (20)

PDF
Algorithms Lecture 5: Sorting Algorithms II
PDF
3d-object-representation.pdf
PPTX
PPTX
Back patching
PPT
01 knapsack using backtracking
PPT
Structure and Enum in c#
PPT
DATA REPRESENTATION
PDF
Rabin karp string matcher
PDF
linked lists in data structures
PDF
Linked List-Types.pdf
PPTX
Ai 7
PDF
Hashing and Hash Tables
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPTX
Top Down Parsing, Predictive Parsing
PDF
sparse matrix in data structure
PPTX
Linked list memory allocation and its types.pptx
PPTX
Different Sorting tecniques in Data Structure
PPTX
Crisp set
PPTX
Graph coloring using backtracking
PPT
One Dimensional Array
Algorithms Lecture 5: Sorting Algorithms II
3d-object-representation.pdf
Back patching
01 knapsack using backtracking
Structure and Enum in c#
DATA REPRESENTATION
Rabin karp string matcher
linked lists in data structures
Linked List-Types.pdf
Ai 7
Hashing and Hash Tables
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Top Down Parsing, Predictive Parsing
sparse matrix in data structure
Linked list memory allocation and its types.pptx
Different Sorting tecniques in Data Structure
Crisp set
Graph coloring using backtracking
One Dimensional Array
Ad

Viewers also liked (8)

PPT
Big oh Representation Used in Time complexities
PPT
Selection sort
PDF
Sorting
PDF
Selection Sort
DOC
Selection sort
PPT
Asymptotic notations
PPTX
Selection sort
PPT
Sorting algorithms
Big oh Representation Used in Time complexities
Selection sort
Sorting
Selection Sort
Selection sort
Asymptotic notations
Selection sort
Sorting algorithms
Ad

Similar to Analisys of Selection Sort and Bubble Sort (20)

PPT
Data Structure (MC501)
PDF
Sorting algorithms bubble sort to merge sort.pdf
PPT
InsertionSortBubbleSortSelectionSort.ppt
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPTX
Searching and Sorting algorithms and working
DOCX
Selection sort lab mannual
PPTX
Data Structure and algorithms for software
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPTX
sorting and searching.pptx
PDF
Selection Sort with Improved Asymptotic Time Bounds
PPTX
Different Searching and Sorting Methods.pptx
PPTX
Selection sort(sorting algorithm in data structure) and its time complexity
PDF
Quicksort analysis
PDF
selection
PDF
selectionsortselectionsortselectionsortselectionsort
PDF
Sorting and Hashing Algorithm full pdfs.
PPT
s4_quick_sort.ppt
PPT
Unit6 C
PPTX
Sorting techniques
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Data Structure (MC501)
Sorting algorithms bubble sort to merge sort.pdf
InsertionSortBubbleSortSelectionSort.ppt
358 33 powerpoint-slides_14-sorting_chapter-14
Searching and Sorting algorithms and working
Selection sort lab mannual
Data Structure and algorithms for software
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
sorting and searching.pptx
Selection Sort with Improved Asymptotic Time Bounds
Different Searching and Sorting Methods.pptx
Selection sort(sorting algorithm in data structure) and its time complexity
Quicksort analysis
selection
selectionsortselectionsortselectionsortselectionsort
Sorting and Hashing Algorithm full pdfs.
s4_quick_sort.ppt
Unit6 C
Sorting techniques
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx

More from Humano Terricola (10)

PPTX
PDF
Portafolio Unidad 4 [Lenguajes y autómatas] - Máquinas de Turing
PDF
Portafolio lenguajes y automatas unidad 3 - Autómatas finitos
PDF
Portafolio unidad 2 [Lenguajes y autómatas]- Expresiones y lenguajes regulares
PDF
Portafolio Lenguajes y Autómatas Unidad 1
PDF
Cuadro comparativo estandares de calidad software
DOCX
Apuntes generación de codigo intermedio
PDF
Generación código intermedio 2
PDF
Configuración del servidor smtp centos
PDF
Servidor proxy en centos
Portafolio Unidad 4 [Lenguajes y autómatas] - Máquinas de Turing
Portafolio lenguajes y automatas unidad 3 - Autómatas finitos
Portafolio unidad 2 [Lenguajes y autómatas]- Expresiones y lenguajes regulares
Portafolio Lenguajes y Autómatas Unidad 1
Cuadro comparativo estandares de calidad software
Apuntes generación de codigo intermedio
Generación código intermedio 2
Configuración del servidor smtp centos
Servidor proxy en centos

Recently uploaded (20)

PPTX
Introduction to Cardiovascular system_structure and functions-1
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PPTX
2Systematics of Living Organisms t-.pptx
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PDF
Sciences of Europe No 170 (2025)
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PDF
An interstellar mission to test astrophysical black holes
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PPTX
Comparative Structure of Integument in Vertebrates.pptx
PPTX
Cell Membrane: Structure, Composition & Functions
PPTX
2. Earth - The Living Planet Module 2ELS
PPTX
neck nodes and dissection types and lymph nodes levels
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
famous lake in india and its disturibution and importance
Introduction to Cardiovascular system_structure and functions-1
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
The KM-GBF monitoring framework – status & key messages.pptx
2Systematics of Living Organisms t-.pptx
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
Sciences of Europe No 170 (2025)
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
TOTAL hIP ARTHROPLASTY Presentation.pptx
An interstellar mission to test astrophysical black holes
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
Comparative Structure of Integument in Vertebrates.pptx
Cell Membrane: Structure, Composition & Functions
2. Earth - The Living Planet Module 2ELS
neck nodes and dissection types and lymph nodes levels
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
Derivatives of integument scales, beaks, horns,.pptx
POSITIONING IN OPERATION THEATRE ROOM.ppt
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
famous lake in india and its disturibution and importance

Analisys of Selection Sort and Bubble Sort

  • 1. Centro de Investigación y Estudios Avanzados CINVESTAV UNIDAD GUADALAJARA Computer Science Student: Luis Adrian Parra Avellaneda Analysis of Algorithms P.H.D Hugo Iván Piza Analysis of Selection Sort and Optimized Bubble Sort September 2016
  • 2. 1 Index Contents Selection Sort ................................................................................................................................ 2 Is there a loop invariant? ................................................................................................................. 2 Is the subarray [1 … p] sorted?....................................................................................................... 2 Stability of the algorithm .................................................................................................................. 3 Mathematical analysis of selection sort........................................................................................... 3 Equation of selection sort ............................................................................................................ 4 Best case for selection sort ......................................................................................................... 4 The worst case is when tp=p and A = n-1.................................................................................... 4 Analysis of only comparisons.......................................................................................................... 5 Analysis of only assignations .......................................................................................................... 6 Graphics and verifications ............................................................................................................... 7 Best case of selection sort .......................................................................................................... 7 Worst case of selection sort ........................................................................................................ 8 Best vs Worst .................................................................................................................................. 9 Optimized bubble sort .................................................................................................................... 10 Is there a loop invariant? ............................................................................................................... 10 Is the subarray [1 … p] sorted?..................................................................................................... 10 Mathematical analysis of optimized bubble sort............................................................................ 11 Analysis of the equation of bubble sort ......................................................................................... 11 The best case for Bubble Sort................................................................................................... 11 The worst case for Bubble Sort ................................................................................................. 12 Analysis of only comparisons........................................................................................................ 12 Best case of comparisons ......................................................................................................... 13 Worst case of comparisons ....................................................................................................... 13 Analysis of only assignations ........................................................................................................ 13 Best case of assignations.......................................................................................................... 13 Worst case of assignations ....................................................................................................... 14 Graphics and verifications ............................................................................................................. 14 Bibliography....................................................................................................................................... 17
  • 3. 2 Selection Sort The selection sort algorithm consists in separate the array un two list, the first list will be sorted, and the other list don´t, the algorithm finds the minimum element in the second sub array and gong to replace the element in the pivot to the minimum element in the second sub array, and the process will be repeated for each index of the array. Below is showed the code of the selection sort: for (j=1 to n-1) int min=j; for(i=j+1 to n) if(a[i] < a[min]) min=i; if(min!=j) swap(a[j], a[min]); Is there a loop invariant? The array is divided in two sections, the left array and right subarray, the value in the pivot is always greater than the maximum value of the left subarray, and the elements in the left subarray are sorted 1. Initialization: The array is trivially sorted when the size of this is 1, and this element are the minimum and maximum 2. Maintenance: The sub array A [1…. p-1] is sorted in each iteration, and the element in the pivot is greater than the maximum element in the sub array A [1…. p-1] 3. Termination: In the last iterations, the position of the pivot is n-1, this the elements in the array [1…n-1] are sorted, and the pivot is the minimum value of the right subarray, however the size of the subarray is 1, and the element in the pivot is the minimum element, but is greater than the maximum element in the left subarray, thus the array is sorted The variable min always takes the minimum value of the right subarray A [p…n] 1. Initialization: When the array consists in one single element, the maximum value will be this element, min=A [1] 2. Maintenance: The algorithm will search the minimum element in the sub array A [p … n], because A [1] <A [2] <…A [p]…A [n], the inner loops will search the minimum element, and in the last iteration of the inner loop the variable A[p] will be the minimum value of the right subarray 3. Termination: In the last iterations, the value A[n] is the greater, and A [1] <A [2] <…A [n], and thus this invariant is correct Is the subarray [1 … p] sorted? Yes, because the element in the pivot is the minimum element of the [p+1 … n] subarray, and this example was showed in the loop invariant example, but this can prove by induction Base:
  • 4. 3 𝑊ℎ𝑒𝑛 𝑡ℎ𝑒 𝑎𝑟𝑟𝑎𝑦 𝑠𝑖𝑧𝑒 𝑖𝑠 1, 𝐴[1]𝑖𝑠 𝑠𝑜𝑟𝑡𝑒𝑑 Induction Step: We know that the A[p] contains the minimum element of the right subarray, thus each element of the array A [1 … p] is sorted 𝐴[1] < 𝐴[2] < ⋯ < 𝐴[𝑝] Stability of the algorithm The algorithm is not stable, in the next example is showed an example of that A1 and A2 are equals 𝐴𝑟𝑟𝑎𝑦 = 𝐴1 𝐴2 … … … … . 𝐵 𝐶 𝐷 Where C is the minimum value in the first iteration 𝐴𝑟𝑟𝑎𝑦 = 𝐶𝐴2 … … … … . 𝐵 𝐴1 𝐷 And now, B is the minimum value in the second iteration, A2 and B will be swapped 𝐴𝑟𝑟𝑎𝑦 = 𝐶𝐵 … … … … . 𝐴2 𝐴1 𝐷 Thus, these variables not remain the same order, thus this algorithm is not stable Mathematical analysis of selection sort Selection Sort Cost Times for (j = 1; j < n; j++) C1 𝑛 int Min = j; C2 𝑛 − 1 for ( i = j+1; i <= n; i++) C3 ∑ 𝑘 = 𝑛(𝑛 + 1) 2 𝑛 𝑘=2 if (a[i] < a[Min]) C4 ∑ 𝑘 − 1 = 𝑛(𝑛 − 1) 2 𝑛 𝑘=2 Min = i; C5 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 if(Min != j) C6 𝑛 − 1 swap(a[j], a[Min]); C7 3 ∗ 𝐴 𝑤ℎ𝑒𝑟𝑒 𝐴 → {𝑛 − 1 ≥ 𝐴 ≥ 0}
  • 5. 4 Equation of selection sort 𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + 𝐶5 ∑( 𝑡𝑝 − 1) + 𝐶6( 𝑛 − 1) + 3 𝑛 𝑝=2 𝐶7 ∗ 𝐴 Best case for selection sort The best case running time when tp=1, and A = 0: 𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + 𝐶5 ∑(1 − 1) + 𝐶6( 𝑛 − 1) + 3𝐶7 ∗ 0 𝑛 𝑝=2 𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + 𝐶5 ∑(0) + 𝐶6( 𝑛 − 1) + 3𝐶7(0) 𝑛 𝑝=2 𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + 𝐶6( 𝑛 − 1) 𝑇(𝑛) = ( 𝐶3 2 + 𝐶4 2 ) 𝑛2 + (𝐶1 + 𝐶2 + 𝐶3 2 − 𝐶4 2 ) 𝑛 − (𝐶2 − 𝐶3) 𝑇(𝑛) = 𝐶1𝑛2 + 𝐶2𝑛 + 𝐶3 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛² The worst case is when tp=p and A = n-1 𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + 𝐶5 ∑( 𝑝 − 1) + 𝐶6( 𝑛 − 1) + 𝐶7(𝑛 − 1) 𝑛 𝑝=2 Selection Sort Cost Times for (j = 1; j < n; j++) C1 𝑛 int Min = j; C2 𝑛 − 1 for ( i = j+1; i <= n; i++) C3 ∑ 𝑘 = 𝑛(𝑛 + 1) 2 𝑛 𝑘=2 if (a[i] < a[Min]) C4 ∑ 𝑘 − 1 = 𝑛(𝑛 − 1) 2 𝑛 𝑘=2 Min = i; C5 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 if(Min != j) C6 𝑛 − 1 swap(a[j], a[Min]); C7 3 ∗ 𝐴 𝑤ℎ𝑒𝑟𝑒 𝐴 → {𝑛 − 1 ≥ 𝐴 ≥ 0}
  • 6. 5 𝑇(𝑛) = 𝐶1(𝑛) + 𝐶2(𝑛 − 1) + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + 𝐶5 ( 𝑛( 𝑛 − 1) 2 − ( 𝑛 − 1)) + 𝐶6( 𝑛 − 1) + 𝑐7(𝑛 − 1) 𝑇(𝑛) = [𝐶3 + 𝐶4 + 𝐶5 2 ] 𝑛2 + [𝐶1 + 𝐶2 + 𝐶3 2 − 𝐶4 2 + 𝐶5 + 𝐶6 + 𝐶7] 𝑛 + [𝑐2 + 𝑐5 − 𝑐6 − 𝑐7] 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛² Why the complexity is n² in the best and the worst case? Because in the best case, the algorithm remains make comparisons, and doesn´t have a flag that indicate that the array is sorted Analysis of only comparisons SELECTION SORT COST TIMES for (j = 1; j < n; j++) C1 𝑛 int Min = j; C2 for ( i = j+1; i <= n; i++) C3 ∑ 𝑘 = 𝑛(𝑛 + 1) 2 𝑛 𝑘=2 if (a[i] < a[Min]) C4 ∑ 𝑘 − 1 = 𝑛(𝑛 − 1) 2 𝑛 𝑘=2 Min = i; C5 if(Min != j) C6 𝑛 − 1 swap(a[j], a[Min]); C7 𝑇(𝑛) = 𝐶1𝑛 + 𝐶3 ( 𝑛(𝑛 + 1) 2 ) + 𝑐4 ( 𝑛(𝑛 − 1) 2 ) + 𝐶6(𝑛 − 1) We watch that the tp variable is not here, thus, in the best case and the worst case the number of comparisons are the same, and the equation not depends of tp, thus the equation in the best and worst case is: 𝑇(𝑛) = [ 𝐶3 2 + 𝐶4 2 ] 𝑛2 + [𝐶1 + 𝐶3 2 − 𝐶4 2 + 𝐶6] 𝑛 − 𝐶6 And the complexity is 𝑇(𝑛) = [ 𝐶3 2 + 𝐶4 2 ] 𝑛2 + [𝐶1 + 𝐶3 2 − 𝐶4 2 + 𝐶6] 𝑛 − 𝐶6 𝑇(𝑛) = 𝑂(𝑛2 )
  • 7. 6 Analysis of only assignations SELECTION SORT COST TIMES for (j = 1; j < n; j++) C1 int Min = j; C2 𝑛 − 1 for ( i = j+1; i <= n; i++) C3 if (a[i] < a[Min]) C4 Min = i; C5 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 if(Min != j) C6 swap(a[j], a[Min]); C7 𝐵𝑒𝑠𝑡: 0 𝑊𝑜𝑟𝑠𝑡 = 3 ∗ (𝑛 − 1) The equations that represents the time of this algorithm is the following: In the best case, p=1, and in the swap block, the instructions are not executed 𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ∑(1 − 1) 𝑛 𝑝=2 + 𝐶7 ∗ 0 𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ∑ 0 𝑛 𝑝=2 + 0 𝑇(𝑛) = 𝐶2(𝑛 − 1) 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛 We watch that in the best case, the number of assignations is linear, now let´s analyze the equation in the worst case, in the worst case tp=p 𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ∑(𝑝 − 1) 𝑛 𝑝=2 + 3𝐶7(𝑛 − 1) 𝑇(𝑛) = 𝐶2(𝑛 − 1) + 𝐶5 ( 𝑛(𝑛 − 1) 2 ) + 3𝐶7(𝑛 − 1) 𝑇(𝑛) = 𝐶5 2 𝑛2 + [𝐶2 − 𝐶5 2 + 3𝐶7] 𝑛 − [𝐶2 − 3𝐶7] 𝑇(𝑛) = 𝑂(𝑛2) In the worst case, the complexity using only assignations is n²
  • 8. 7 Graphics and verifications A java program was written to prove that the analysis of the algorithms was correct, this program counts the instructions executed in the best and the worst case Best case of selection sort
  • 9. 8 Worst case of selection sort In the two graphics the number of instructions are different, however, in two cases are showed a quadratic graphic
  • 10. 9 Best vs Worst With this graphics is proved that que complexity in the worst and best case is n²
  • 11. 10 Optimized bubble sort The behavior of the bubble sort algorithm is similar to bubbles behavior; each element is swapped in one position for each iteration, the optimized version of bubble sort contains a flag that indicates if the array is ordered, when this happened the outer cycle will be break. The algorithm is showed below: for(int i=1; i<=n; i++) bool flag = false; for(int j=1; j<=n-i; j++) if(array[j]>array[j+1]) flag = true; int temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; if(!flag) return; Is there a loop invariant? The invariant is that the elements in the subarray [1 … p] are the same elements, and always is displaced the greater value 1. Initialization: The array is trivially sorted when the size of this is 1 2. Maintenance: The array A [1 … p] contains the same elements, because the element which is displaced is the maximum element, and the other elements remains in the subarray, and the greater value is displaced to the top (p+1) 3. Termination: The algorithm is finish when the maximum p is 1, the sub array in the right is sorted, and if the value in the position 1 is greater than A[2], this will be swapped, and finally the array will be sorted Is the subarray [1 … p] sorted? The subarray [1 … p] is not sorted in, because this algorithm put the great value in p position, but not order the [1 … p] subarray, example: [2, 3, 7, 1, 5] The next iterations of the array will be showed [3, 2,1,5,7] → [2,1,3,5,7] → [1,2,3,5,7] However, the subarray [p … n] is sorted in each iteration, because the value in the pivot is the greater value of [1 … p] subarray. Thus, the subarray [1 … p] will be sorted if we make changes to the algorithm for displace the numbers in inverse order.
  • 12. 11 Mathematical analysis of optimized bubble sort Bubble Sort Cost Times for(int i=1; i<=n; i++) C1 𝑛 + 1 bool flag = false; C2 𝑛 for(int j=1; j<=n-i; j++) C3 𝑛(𝑛 + 1) 2 if(array[j]>array[j+1]) C4 𝑛(𝑛 − 1) 2 flag = true; C5 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 int temp = array[j+1]; C6 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 array[j+1] = array[j]; C7 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 array[j] = temp; C8 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 if(!flag) C9 𝑛 return; C10 Max=1, Min=0 Analysis of the equation of bubble sort The equations that describes the execution time for bubble sort algorithm is the following, in that is not included the restrictions for the flag 𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ∑( 𝑡𝑝 − 1) + 𝐶9𝑛 + 𝐶10 𝑛 𝑝=2 The best case for Bubble Sort When tp=1 is the best case, and the equation will be analyzed below 𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ∑(1 − 1) + 𝐶9𝑛 + 𝐶10 ∗ 1 𝑛 𝑝=2 But, what happens when the array is ordered, the final flag will break the cycle, thus, the cycle will be executed only once, and, all the variables N will be changed to 1 𝑇(𝑛) = 𝐶1 + 𝐶2 + 𝐶3 ( 𝑛(1 + 1) 2 ) + 𝐶4 ( 𝑛(1 − 1) 2 ) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ∑(1 − 1) + 𝐶9 + 𝐶10 𝑛 𝑝=2 Why the n in the inner loops remains, because, the inner loops will be executed n times, but the outer loop only going to run once, and the outer cycle never reach the end, thus the last comparison is not executed
  • 13. 12 𝑇(𝑛) = 𝐶1 + 𝐶2 + 𝐶3( 𝑛) + 𝐶9 + 𝐶10 𝑇(𝑛) = 𝐶3𝑛 + (𝐶1 + 𝐶2 + 𝐶9 + 𝐶10) 𝑇(𝑛) = 𝑎𝑛 + 𝑏 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛 The worst case for Bubble Sort The worst case is when the array is totally inverted, thus the flag is never executed in this case, and tp=p 𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8]∑( 𝑝 − 1) + 𝐶9𝑛 + 𝐶10 ∗ 0 𝑛 𝑝=2 𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶2𝑛 + 𝐶3 ( 𝑛( 𝑛 + 1) 2 ) + 𝐶4 ( 𝑛( 𝑛 − 1) 2 ) + [ 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] ( 𝑛( 𝑛 − 1) 2 ) + 𝐶9𝑛 𝑇(𝑛) = [𝐶3 + 𝐶4 + 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8] 2 𝑛2 + [𝐶1 + 𝐶2 + 𝐶3 2 − 𝐶4 2 − 𝐶5 2 − 𝐶6 2 − 𝐶7 2 − 𝐶8 2 + 𝐶9] 𝑛 + 𝐶1 𝑇(𝑛) = 𝑎𝑛2 + 𝑏𝑛 + 𝑐 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 𝑂(𝑛²) Analysis of only comparisons Bubble Sort Cost Times for(int i=1; i<=n; i++) C1 𝐵𝑒𝑠𝑡 = 1, 𝑊𝑜𝑟𝑠𝑡 = 𝑛 + 1 bool flag = false; C2 for(int j=1; j<=n-i; j++) C3 𝐵𝑒𝑠𝑡 = 𝑛, 𝑊𝑜𝑟𝑠𝑡 = 𝑛(𝑛 + 1) 2 if(array[j]>array[j+1]) C4 𝐵𝑒𝑠𝑡 = 𝑛 − 1, 𝑊𝑜𝑟𝑠𝑡 = 𝑛(𝑛 − 1) 2 flag = true; C5 int temp = array[j+1]; C6 array[j+1] = array[j]; C7 array[j] = temp; C8 if(!flag) C9 𝑊𝑜𝑟𝑠𝑡 = 𝑛, 𝐵𝑒𝑠𝑡 = 1 return; C10
  • 14. 13 Best case of comparisons In the best case, the flag is activated in the first iteration, thus in the table there are 2 cases for the values, in this case the outer loop will run once, the equation for the best case is showed below: 𝑇(𝑛) = 𝐶1 + 𝐶3𝑛 + 𝐶4(𝑛 − 1) + 𝐶9 𝑇(𝑛) = (𝐶3 + 𝐶4)𝑛 + (𝐶1 − 𝐶4 + 𝐶9) 𝑇(𝑛) = 𝑎𝑛 + 𝑏 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑛 (𝐿𝑖𝑛𝑒𝑎𝑟) Worst case of comparisons In the worst case the flag is never executed 𝑇(𝑛) = 𝐶1(𝑛 + 1) + 𝐶3 ( 𝑛(𝑛 + 1) 2 ) + 𝐶4 ( 𝑛(𝑛 − 1) 2 ) + 𝐶9𝑛 𝑇(𝑛) = (𝐶3 − 𝐶4) 2 𝑛2 + (𝐶1 + 𝐶3 2 − 𝐶4 2 + 𝐶9) 𝑛 + 𝐶1 𝑇(𝑛) = 𝑎𝑛2 + 𝑏𝑛 + 𝑐 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑂(𝑛2 ) Analysis of only assignations Bubble Sort Cost Times for(int i=1; i<=n; i++) C1 bool flag = false; C2 𝑊𝑜𝑟𝑠𝑡 = 𝑛, 𝐵𝑒𝑠𝑡 = 1 for(int j=1; j<=n-i; j++) C3 if(array[j]>array[j+1]) C4 flag = true; C5 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 int temp = array[j+1]; C6 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 array[j+1] = array[j]; C7 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 array[j] = temp; C8 ∑ 𝑡𝑝 − 1 𝑛 𝑝=2 if(!flag) C9 return; C10 Best case of assignations In the best case, the flag is activated in the first iteration, and tp=1, and the equation is: 𝑇(𝑛) = 𝐶2 + (𝐶5 + 𝐶6 + 𝐶7 + 𝐶8) ∑(1 − 1) 𝑛 𝑝=2
  • 15. 14 𝑇(𝑛) = 𝐶2 𝑇(𝑛) = 𝑎 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝐶𝑜𝑛𝑠𝑡𝑎𝑛𝑡 Thus the algorithm never joins into the IF in the inner loop, and only the assignation of the flag is executed Worst case of assignations In the worst case, the flag is not activated in each iteration of the outer loop, and tp=p, and the equation is: 𝑇(𝑛) = 𝐶2𝑛 + (𝐶5 + 𝐶6 + 𝐶7 + 𝐶8) ∑(𝑝 − 1) 𝑛 𝑝=2 𝑇(𝑛) = 𝐶2𝑛 + (𝐶5 + 𝐶6 + 𝐶7 + 𝐶8)𝑛(𝑛 − 1) 2 𝑇(𝑛) = 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8 2 𝑛2 + ( 𝐶2 − 𝐶5 + 𝐶6 + 𝐶7 + 𝐶8 2 ) 𝑛 𝑇(𝑛) = 𝑎𝑛2 + 𝑏𝑛 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑂(𝑛²) Graphics and verifications In the worst case is proved with this graphic that the complexity is n²
  • 16. 15 And in the best case should be n, or linear, this is showed in the following graphic In that graphics is a linear function, and now the comparison between the worst and the best case will be showed
  • 17. 16 The difference between the functions is terrific, and finally, the values of the size of the problem will be changed for more details
  • 18. 17 Bibliography  Cormen, Stein, Rivest, Leiserson (2001). “Introduction to algorithms”, 2nd edition, McGrawHill. United States  Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. (1974) The Design and Analysis of Computer Algorithms. Addison-Wesley  Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman.(1983) Data Structures and Algorithms. Addison-Wesley.