Sorting Algorithms & Run-Time Complexity · Wesleyan Algorithms Algorithm - a finite sequence of...

32
Wesleyan Sorting Algorithms & Run-Time Complexity L. Hinrichs Nebraska Wesleyan Univ. April 9, 2015 L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 1 / 14

Transcript of Sorting Algorithms & Run-Time Complexity · Wesleyan Algorithms Algorithm - a finite sequence of...

Wesleyan

Sorting Algorithms & Run-Time Complexity

L. Hinrichs

Nebraska Wesleyan Univ.

April 9, 2015

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 1 / 14

Wesleyan

Introduction

AlgorithmsCombinatoricsSorting Algorithms

Bubble sort algorithmProcessRun-Time Complexity

Merge sort algorithmProcessRun-Time Complexity

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 2 / 14

Wesleyan

Algorithms

Algorithm - a finite sequence of unambiguously defined steps thatcarries out a taskSorting Algorithm - arrange certain objects from an array [by size,for our purposes]Assume numbers are unique positive integers

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 3 / 14

Wesleyan

Bubble sort algorithm

n = 5

4 3 1 6 2

3 4 1 6 2

3 1 4 6 2

3 1 4 6 2

3 1 4 2 6...

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 4 / 14

Wesleyan

Bubble sort algorithm

n = 5

4 3 1 6 2

3 4 1 6 2

3 1 4 6 2

3 1 4 6 2

3 1 4 2 6...

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 4 / 14

Wesleyan

Bubble sort algorithm

n = 5

4 3 1 6 2

3 4 1 6 2

3 1 4 6 2

3 1 4 6 2

3 1 4 2 6...

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 4 / 14

Wesleyan

Bubble sort algorithm

n = 5

4 3 1 6 2

3 4 1 6 2

3 1 4 6 2

3 1 4 6 2

3 1 4 2 6...

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 4 / 14

Wesleyan

Bubble sort algorithm

n = 5

4 3 1 6 2

3 4 1 6 2

3 1 4 6 2

3 1 4 6 2

3 1 4 2 6

...

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 4 / 14

Wesleyan

Bubble sort algorithm

n = 5

4 3 1 6 2

3 4 1 6 2

3 1 4 6 2

3 1 4 6 2

3 1 4 2 6...

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 4 / 14

Wesleyan

Bubble sort algorithm

Indexed Array A

9 4 3 8 10 1 2 3 4

Pseudocode Single-Pass BubbleSort(A,n)

for i from 0 to n-2

if (A[i] > A[i + 1])

swap(A[i],A[i+1])

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 5 / 14

Wesleyan

Bubble sort algorithm

Indexed Array A

9 4 3 8 10 1 2 3 4

Pseudocode Single-Pass BubbleSort(A,n)

for i from 0 to n-2

if (A[i] > A[i + 1])

swap(A[i],A[i+1])

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 5 / 14

Wesleyan

Big O Notation

“Landau’s symbol”O describes a “simplified” rate of growth or behavior of a functionIf run-time is a polynomial, O is the orderEx:f (n) = 3n2 + 9n + 2 =⇒ f = O(n2)

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 6 / 14

Wesleyan

Bubble sort algorithm

Pseudocode BubbleSort(A,n)

for k from 0 to n - 1

set fullsort = 0

for i from 0 to n - k - 1

if (A[i] > A[i + 1])

swap(A[i],A[i+1])

fullsort = 1

if fullsort == 0, break

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 7 / 14

Wesleyan

Bubble sort time complexity

n−1∑k=0

n − k − 1 = (n − 1) + (n − 2) + . . . + 0

= n(n − 1)2

= O(n2)

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 8 / 14

Wesleyan

Case Scenarios for Bubble Sort

Best Case:

n − 1 ≤ n=⇒ O(n)

Worst Case:

(n2

)= n!

2!(n − 2)!

= n(n − 1)2

= n2 − n2

≤ n2

=⇒ O(n2)

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 9 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 10 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 10 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 10 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

4 3 1 6 2

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 10 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

3 4 1 2 6

1 3 4 2 6

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 11 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

3 4 1 2 6

1 3 4 2 6

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 11 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

3 4 1 2 6

1 3 4 2 6

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 11 / 14

Wesleyan

Merge sort algorithm

n = 5

4 3 1 6 2

3 4 1 2 6

1 3 4 2 6

1 2 3 4 6

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 11 / 14

Wesleyan

Merge sort algorithm

Let M(n) be the number of comparisons required to sort a list of size n.

Then M(1) = 0 and M(2) = 1.

If n = 2k, M(2k) = 2M(k) + 2k − 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 12 / 14

Wesleyan

Merge sort algorithm

Let M(n) be the number of comparisons required to sort a list of size n.

Then M(1) = 0 and M(2) = 1.

If n = 2k, M(2k) = 2M(k) + 2k − 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 12 / 14

Wesleyan

Merge sort algorithm

Let M(n) be the number of comparisons required to sort a list of size n.

Then M(1) = 0 and M(2) = 1.

If n = 2k, M(2k) = 2M(k) + 2k − 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 12 / 14

Wesleyan

Big O Merge Sort

Let n = 2m.

f (m) = 2f (m − 1) + 2m − 1= 2(2f (m − 2) + 2m−1 − 1) + 2m − 1= 22f (m − 2) + 2 · 2m − 2− 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 13 / 14

Wesleyan

Big O Merge Sort

Let n = 2m.

f (m) = 2f (m − 1) + 2m − 1= 2(2f (m − 2) + 2m−1 − 1) + 2m − 1= 22f (m − 2) + 2 · 2m − 2− 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 13 / 14

Wesleyan

Big O Merge Sort

Let n = 2m.

f (m) = 2f (m − 1) + 2m − 1= 2(2f (m − 2) + 2m−1 − 1) + 2m − 1= 22f (m − 2) + 2 · 2m − 2− 1= 23f (m − 3) + 3 · 2m − 22 − 2− 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 13 / 14

Wesleyan

Big O Merge SortLet n = 2m.

f (m) = 2f (m − 1) + 2m − 1= 2(2f (m − 2) + 2m−1 − 1) + 2m − 1= 22f (m − 2) + 2 · 2m − 2− 1= 23f (m − 3) + 3 · 2m − 22 − 2− 1

...= 2m−2(2f (m −m) + 2− 1) + (m − 1)2m − 2m−2 − 2m−3 − . . .− 1= m2m − 2m−1 − 2m−2 − . . .− 1= m2m − (2m−1 + 2m−2 + . . . + 1)= m2m − (2m − 1)= (m − 1)2m − 1

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 13 / 14

Wesleyan

Big O Merge SortLet n = 2m.f (m) = 2f (m − 1) + 2m − 1

= 2(2f (m − 2) + 2m−1 − 1) + 2m − 1= 22f (m − 2) + 2 · 2m − 2− 1= 23f (m − 3) + 3 · 2m − 22 − 2− 1

...= 2m−2(2f (m −m) + 2− 1) + (m − 1)2m − 2m−2 − 2m−3 − . . .− 1= m2m − 2m−1 − 2m−2 − . . .− 1= m2m − (2m−1 + 2m−2 + . . . + 1)= m2m − (2m − 1)= (m − 1)2m − 1≈ (log2 n)n since n = 2m =⇒ m = log2 n

=⇒ O(n log n)L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 13 / 14

Wesleyan

References

A Walk Through Combinatorics 3rd Ed., Miklos Bona, WorldScientific 2011Bubble Sort Algorithm, YouTube, MyCodeSchool,https://www.youtube.com/watch?v=Jdtq5uKz-w4

Sorting Algorithm Animations, Website, David R. Martin,http://www.sorting-algorithms.com/

L. Hinrichs (Nebraska Wesleyan Univ.) Sorting Algorithms & Run-Time Complexity April 9, 2015 14 / 14