sorting algorithms yaroslav dmytruk yaroslav.dmytruk@gmail.com

Post on 04-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

• Computational complexity

• Memory usage

Evaluation of the algorithm:

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

……

many algorithms

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

4 8 1 6 5

PIVOT

We have an unsorted array of five elements.

Left pointer Right pointer

4

4 8 1 6 5

PIVOT

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

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.

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.

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.

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.

PIVOT

1 4 8 6 5

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

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

PIVOT

1 4 8 6 5

Then we start the same procedure for the rest groups.

1

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

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.

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

PIVOT8

1 4 5 6 8

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

1

PIVOT8

1 4 5 6 8

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

1

PIVOT

1 4 5 6 8

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

1

5PIVO

T

1 4 5 6 8

Let’s watch it without comments.

1

PIVOT5

1 4 5 6 81

1 4 5 6 81

The array has been sorted!

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

6

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

1319

First pass

3 6 79 8 41

First pass

1 6 7 3 8 4 9

Has been finished!

7 div 2 = 3 div 2 = 1

Second pass

1 6 7 3 8 4 9

These elements will be compared.

467448

63

73

1673849

Second pass

1 3 4 6 7 8 9

has been finished!

The array has been sorted!

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

4 8 1 6 5 0 74

8

488

4 1

64

6

46

5 0 71

7

17

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

1000

411

6 7

8

1

7

05

6

04

514 04

0

The array has been sorted!

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

Bubble sort

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

Yaroslav Dmytrukyaroslav.dmytruk@gmail.com

top related