38
Sorting algorithms Yaroslav Dmytruk [email protected] m

Sorting algorithms Yaroslav Dmytruk [email protected]

Embed Size (px)

Citation preview

Page 1: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Sorting algorithms Yaroslav Dmytruk

[email protected]

Page 2: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order.

Page 3: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

• Computational complexity

• Memory usage

Evaluation of the algorithm:

Page 4: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

• Quicksort• Heapsort• Shell sort• Bubble sort• Merge sort• Insertion sort• Selection sort

……

many algorithms

Page 5: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Quicksort was invented by Sir Charles Antony Richard Hoare in 1962.

Page 6: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

4 8 1 6 5

PIVOT

We have an unsorted array of five elements.

Left pointer Right pointer

Page 7: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

4

4 8 1 6 5

PIVOT

We choose one element, pivot element. Let it be most left element.

Page 8: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

8 1 6 5

4Now we go to the right pointer. It’s expected that the element must be higher the pivot and it is, so the right pointer move left.

Page 9: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

8 1 6 5

4It’s expected that the element must be higher than the pivot element and it is, so the right pointer move one position left.

Page 10: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

1 8 1 6 5

4Again, It’s expected that the element must be higher than the pivot element but it’s not, 4 higher than 1, so that element move to the left pointer.

Left pointer moves one position right.

Page 11: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

1 8 8 6 5

4Now it’s expected that the element must be lower than the pivot, but it’s not, 8>4, so the element move to the position of the right pointer.

Right pointer moves left.

Page 12: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT

1 4 8 6 5

4The position where pointers match is the position the pivot element.

Page 13: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT

1 4 8 6 5

III

Now we have two groups. If a group consist of less than two elements it’s taught sorted (on it’s right place).

1

Page 14: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT

1 4 8 6 5

Then we start the same procedure for the rest groups.

1

Page 15: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

8PIVO

T

1 4 8 6 5

We choose pivot element, let it be most left element. Then we move to the right pointer.

1

Page 16: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

8

1 4 5 6 5

It’s is expected that the element must be higher than the pivot, but it is not, so the element move to the left pointer.

1

left pointer moves right.

Page 17: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

8

1 4 5 6

Now it is expected that the elemenet must be lower than the pivot and it is, so the left pointer moves right.

1

Page 18: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT8

1 4 5 6 8

We found the position of our pivot as the pointers matched.

1

Page 19: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT8

1 4 5 6 8

We found the position of our pivot as the pointers matched. “8” on it’s right place!

1

Page 20: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT

1 4 5 6 8

We have a group of two elements, so we have to run the same procedure once again.

1

Page 21: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

5PIVO

T

1 4 5 6 8

Let’s watch it without comments.

1

Page 22: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

PIVOT5

1 4 5 6 81

Page 23: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

1 4 5 6 81

The array has been sorted!

Page 24: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Donald L. Shell is a retired American computer scientist who designed the Shell sort sorting algorithm.

6

Page 25: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

3 6 7 9 8 4

We have an unsorted array of seven elements.

7 div 2 = 3

Array’s length

These elements will be compared.

1

Page 26: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

1319

First pass

3 6 79 8 41

Page 27: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

First pass

1 6 7 3 8 4 9

Has been finished!

7 div 2 = 3 div 2 = 1

Page 28: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Second pass

1 6 7 3 8 4 9

These elements will be compared.

Page 29: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

467448

63

73

1673849

Page 30: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Second pass

1 3 4 6 7 8 9

has been finished!

The array has been sorted!

Page 31: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Heapsort is a comparison-based sorting algorithm to create a sorted array (or list), and is part of the selection sort family.

Page 32: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

4 8 1 6 5 0 74

8

488

4 1

64

6

46

5 0 71

7

17

Page 33: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

8

6 7

4 5 0 1

8 6 7 4 5 0 11

81

1

70

70

0

6

5

0

0 65 00

605

04

0

5 0040

507 1060504 04

0

Page 34: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

1000

411

6 7

8

1

7

05

6

04

514 04

0

The array has been sorted!

Page 35: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

A bubble sort, a sorting algorithm that continuously steps through a list, swapping items until they appear in the correct order.

Bubble sort

Page 36: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com
Page 37: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Literature:

• Thimas H. Cormen; Charles E. Leiserson; Ronald L. Rivest; Clifford Stein. Introduction to Algorithms (2nd ed.) The MIT Press. ISBN 0-07-013151-1.• Shell D.L.. A high-speed sorting procedure// Communications of the ACM. — 2. — (1959) (7): 30–32. DOI:10.1145/368370.368387.• http://en.wikipedia.org/wiki/Sorting_algorithm• http://en.wikipedia.org/wiki/Heapsort• http://en.wikipedia.org/wiki/Bubble_sort• http://en.wikipedia.org/wiki/Quicksort• http://en.wikipedia.org/wiki/Shell_sort• www.zutopedia.com

Page 38: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com

Yaroslav [email protected]