sorting (introduction)
TRANSCRIPT
![Page 1: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/1.jpg)
Sorting
Arvind Devaraj
![Page 2: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/2.jpg)
Sorting
• Given an array, put the elements in order– Numerical or lexicographic
• Desirable characteristics– Fast– In place (don’t need a second array)– Stability
![Page 3: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/3.jpg)
Insertion Sort
• Simple, able to handle any data• Grow a sorted array from the beginning
– Create an empty array of the proper size– Pick the elements one at a time in any order– Put them in the new array in sorted order
• If the element is not last, make room for it
– Repeat until done• Can be done in place if well designed
![Page 4: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/4.jpg)
Insertion Sort
90 11 27 37111631 4
![Page 5: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/5.jpg)
Insertion Sort
90 11 27 37111631 4
90
![Page 6: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/6.jpg)
Insertion Sort
90 11 27 37111631 4
9011
![Page 7: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/7.jpg)
Insertion Sort
90 11 27
90
37111631 4
2711
![Page 8: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/8.jpg)
Insertion Sort
90 11 27
31 90
37111631 4
2711
![Page 9: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/9.jpg)
Insertion Sort
90 11 27
27 31 90
37111631 4
114
![Page 10: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/10.jpg)
Insertion Sort
90 11 27
16 27 31 90
37111631 4
114
![Page 11: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/11.jpg)
Insertion Sort
90 11 27
11 16 27 31 90
37111631 4
114
![Page 12: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/12.jpg)
Insertion Sort
90 11 27
11 16 27 31 37 90
37111631 4
114
![Page 13: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/13.jpg)
Merge Sort
• Fast, able to handle any data– But can’t be done in place
• View the array as a set of small sorted arrays– Initially only the 1-element “arrays” are sorted
• Merge pairs of sorted arrays– Repeatedly choose the smallest element in each– This produces sorted arrays that are twice as long
• Repeat until only one array remains
![Page 14: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/14.jpg)
Merge Sort
90 11 27 37111631 4
9011
![Page 15: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/15.jpg)
Merge Sort
90 11 27
27 31
37111631 4
9011
![Page 16: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/16.jpg)
Merge Sort
90 11 27
27 31 4 16
37111631 4
9011
![Page 17: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/17.jpg)
Merge Sort
90 11 27
27 31 4 16 11 37
37111631 4
9011
![Page 18: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/18.jpg)
Merge Sort
11 27 31
27 31 4 16 11 37
90
9011
![Page 19: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/19.jpg)
Merge Sort
11 27 31
27 31 4 16 11 37
37161190 4
9011
![Page 20: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/20.jpg)
Merge Sort
11 27 31
11 16 27 31 37 90
37161190 4
114
![Page 21: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/21.jpg)
Divide and Conquer
• Split a problem into simpler subproblems– Keep doing that until trivial subproblems result
• Solve the trivial subproblems• Combine the results to solve a larger problem
– Keep doing that until the full problem is solved• Merge sort illustrates divide and conquer
– But it is a general strategy that is often helpful
![Page 22: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/22.jpg)
Quick Sort
• For example, given80 38 95 84 99 10 79 44 26 87 96 12 43 81 3
we can select the middle entry, 44, and sort the remaining entries into two groups, those less than 44 and those greater than 44:
38 10 26 12 43 3 44 80 95 84 99 79 87 96 81
• If we sort each sub-list, we will have sorted the entire array
![Page 23: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/23.jpg)
A sample heap
• Each node is larger than its children
19
1418
22
321
14
119
15
25
1722
![Page 24: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/24.jpg)
Sorting using Heaps• What do heaps have to do with sorting an array?
Because the binary tree is balanced and left justified, it can be represented as an array– All our operations on binary trees can be represented as
operations on arrays– To sort:
heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node;
}
![Page 25: Sorting (introduction)](https://reader033.vdocuments.us/reader033/viewer/2022052310/554f7ad2b4c9052a518b4a33/html5/thumbnails/25.jpg)
Summary of Sorting Algorithms
• in-place, randomized• fastest (good for large inputs)
O(n log n)expectedquick-sort
• sequential data access• fast (good for huge inputs)O(n log n)merge-sort
• in-place• fast (good for large inputs)O(n log n)heap-sort
O(n2)
O(n2)
Time
insertion-sort
selection-sort
Algorithm Notes
• in-place• slow (good for small inputs)
• in-place• slow (good for small inputs)