18
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

Analisys of Selection Sort and Bubble Sort

Embed Size (px)

Citation preview

Page 1: Analisys of Selection Sort and Bubble Sort

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

Page 2: Analisys of Selection Sort and Bubble Sort

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

Page 3: Analisys of Selection Sort and Bubble Sort

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:

Page 4: Analisys of Selection Sort and Bubble Sort

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}

Page 5: Analisys of Selection Sort and Bubble Sort

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}

Page 6: Analisys of Selection Sort and Bubble Sort

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)

Page 7: Analisys of Selection Sort and Bubble Sort

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²

Page 8: Analisys of Selection Sort and Bubble Sort

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

Page 9: Analisys of Selection Sort and Bubble 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

Page 10: Analisys of Selection Sort and Bubble Sort

9

Best vs Worst

With this graphics is proved that que complexity in the worst and best case is n²

Page 11: Analisys of Selection Sort and Bubble Sort

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.

Page 12: Analisys of Selection Sort and Bubble Sort

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

Page 13: Analisys of Selection Sort and Bubble Sort

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

Page 14: Analisys of Selection Sort and Bubble Sort

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

Page 15: Analisys of Selection Sort and Bubble Sort

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²

Page 16: Analisys of Selection Sort and Bubble Sort

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

Page 17: Analisys of Selection Sort and Bubble Sort

16

The difference between the functions is terrific, and finally, the values of the size of the

problem will be changed for more details

Page 18: Analisys of Selection Sort and Bubble Sort

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.