Made by: Yaroslav Titov Form 7A Teacher: R.S.Miroshnichenko.
Sorting algorithms Yaroslav Dmytruk [email protected].
-
Upload
clarence-gray -
Category
Documents
-
view
216 -
download
0
Transcript of Sorting algorithms Yaroslav Dmytruk [email protected].
![Page 2: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/3.jpg)
• Computational complexity
• Memory usage
Evaluation of the algorithm:
![Page 4: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/4.jpg)
• Quicksort• Heapsort• Shell sort• Bubble sort• Merge sort• Insertion sort• Selection sort
……
many algorithms
![Page 5: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/5.jpg)
Quicksort was invented by Sir Charles Antony Richard Hoare in 1962.
![Page 6: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/21.jpg)
5PIVO
T
1 4 5 6 8
Let’s watch it without comments.
1
![Page 22: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/22.jpg)
PIVOT5
1 4 5 6 81
![Page 23: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/23.jpg)
1 4 5 6 81
The array has been sorted!
![Page 24: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/25.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/26.jpg)
1319
First pass
3 6 79 8 41
![Page 27: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/27.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/28.jpg)
Second pass
1 6 7 3 8 4 9
These elements will be compared.
![Page 29: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/29.jpg)
467448
63
73
1673849
![Page 30: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/30.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/31.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/32.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/33.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/34.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/35.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/36.jpg)
![Page 37: Sorting algorithms Yaroslav Dmytruk yaroslav.dmytruk@gmail.com.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/37.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649f1e5503460f94c3631c/html5/thumbnails/38.jpg)
Yaroslav [email protected]