SortingAlgorithms - Sorting Algorithms Sorting Alireza...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sorting Algorithms Sorting Alireza Abedinejad Abedi@douran.com Sorting Algorithms Sorting Bubble Sort (Exchange Sort) Selection Sort Insertion Sort Heap Sort Merge Sort Shell Sort Quick Sort Bubble Sort (Exchange/Sinking Sort) (Exchange/Sinking The bubble sort or sinking sort sorts arrays The sinking such that the smallest values end up in the lower positions in the array and the highest values end up in the upper positions. values – Requires several passes through the array. – Pairs of values are compared Pairs » If the pair is in decreasing order then the values are If swapped. swapped. » Otherwise, the pair remain in the same order. Bubble Sort (Exchange/Sinking Sort) (Exchange/Sinking The best case analysis is Θ (n) where the The (n) array is already sorted. array The worst and average cases are Θ (n2). The Bubble Sort Example 1 Bubble 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 7 5 5 5 5 5 3 3 3 3 5 7 3 3 3 3 5 5 5 5 3 3 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 1 9 9 9 9 9 1 1 1 1 8 1 1 1 1 1 9 9 9 9 9 1. 2. 3. 4. 5. 6. 3 3 3 3 3 1 5 5 5 5 1 3 7 7 1 1 5 5 1 1 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 Bubble Sort Example 2 Bubble Selection Sort Selection List of names – Put them in alphabetical order » Find the name that comes first in the alphabet, Find and write it on a second sheet of paper and » Cross out the name on the original list » Continue this cycle until all the names on the Continue original list have been crossed out and written onto the second list, at which point the second list is sorted sorted Selection Sort Selection A slight adjustment to this manual approach slight does away with the need to duplicate space does – As you cross a name off the original list, a free As space opens up space – Instead of writing the minimum value on a Instead second list, exchange it with the value currently in the position where the crossed-off item should go should Analysis – best, worst, and average cases Analysis are: Θ (n2) Selection Sort Example Selection Insertion Sort Insertion The insertion sort inserts the current item The into the proper position by moving the other sorted elements and then inserting the current element. – This can be done with one array because the This same number of elements are maintained same Insertion Sort Insertion The best case analysis is Θ (n) where the The (n) array is already sorted. array The worst and average cases are Θ (n2). The – Every element must be moved. Insertion Sort Example Insertion Sort 7, 3, 5, 6, 1 Start with 7 and insert 3 => 3, 7 Insert 5 => 3, 5, 7 Insert 6 => 3, 5, 6, 7 Insert 1 => 1, 3, 5, 6, 7 Heap Sort Heap Heap Tree is a tree that for all nodes: Node.value>= node.left.value && Node.value>= Node.value>= node.right.value node.right.value Heap Sort Heap SemiHeap Tree is a tree that for all nodes SemiHeap except root we have: except Node.value>= node.left.value && Node.value>= Node.value>= node.right.value node.right.value Heap Sort Heap The data structure of the Heapsort algorithm The is a heap. The data sequence to be sorted is stored as The the labels of the binary tree. As shown later, in the implementation no As pointer structures are necessary to represent the tree, since a complete binary tree can be efficently stored in an array. Heap Sort-DownHeap Heap DownHeap procedure Heap Sort-Array Representation Heap A complete binary tree with n vertices and vertex complete labelling a can be stored most efficiently in an array a: - the root is stored at position 0 - the two direct descendants of a vertex at position v are stored at positions 2v+1 and 2v+2 +2 All vertices at positions 0, ..., n/2-1 are inner All /2-1 nodes, all vertices n/2, ..., n-1 are leaves (integer /2, -1 division / ). division Heap Sort- Array Representation Array Array representation of Heap Heap Sort-Conclusion Heap With its time complexity of O(n log(n)) With )) Heap sort is optimal. Unlike Mergesort, Heapsort requires no Unlike Mergesort Heapsort extra space. Heap sort is not stable. Heap A sorting algorithm is stable, if it leaves the sorting stable if order of equal elements unchanged. order Merge Sort Merge The merge sort algorithm The requires recursion. – The concept used in this algorithm is called The divide-and-conquer. divide-and-conquer. » The array is first split into two smaller arrays. » Each of the smaller two arrays are recursively Each sorted. sorted. » The two arrays are merged back into one array. Merge Sort Merge The base case for the merge sort is when the array The to be sorted only has one element left that does not need to be rearranged. not If there is more than one element to sort, split the If arrays into two sections and call the merge sort. arrays The best, worst, and average cases require The Θ (n log n). Merge Sort Merge void mergesort(int [ ] a, int lo, int hi) void { if (lo<hi) { m = (lo + hi) / 2; mergesort (a, lo, m); mergesort (a, m+1, hi); merge (a, lo, hi); } } Merging sample Merge Sort Analysis Merge Procedure merge requires 2n steps (n steps for Procedure merge copying the sequence to the intermediate array b, another n steps for copying it back to array a). The ). time complexity of mergesort is therefore time mergesort T(n) = 2n + 2 T(n/2) and /2) T(1) = 0 (1) The solution of this recursion yields T(n) = 2n log(n) O(n log(n)) )) A drawback is that Mergesort needs an additional drawback space of (n) for the temporary array b. space for Shell Sort Shell The idea of Shellsort is the following: The a) arrange the data sequence in a two-dimensional array b) sort the columns of the array Shell Sort Shell The effect is that the data sequence is partially sorted. The The process above is repeated, but each time with a The narrower array, i.e. with a smaller number of columns. In the last step, the array consists of only one column. In In each step, the sortedness of the sequence is increased, until in the last step it is completely sorted. However, the number of sorting operations necessary in However, each step is limited, due to the presortedness of the sequence obtained in the preceding steps. Shell Sort Example Shell Let 3 7 9 0 5 1 6 8 4 2 0 6 1 5 7 3 4 9 8 2 be Let the data sequence to be sorted. First, it is arranged in an array with 7 columns (left), then the columns are sorted (right): Shell Sort Example Shell Data elements 8 and 9 have now already come to the end Data of the sequence, but a small element (2) is also still there. In the next step, the sequence is arranged in 3 columns, which are again sorted: which Quick Sort Quick Quick sort is also a divide-and-conquer Quick recursive sorting method. recursive – The difference is the method in which the array The is divided. is » Determine the final position in the sorted array of Determine the first element of the unsorted array. (all values to the left of the element are less than the element and all values to the right of the element are greater than the element). - Pivot point the – After the array is divided, the sort is performed After on the two unsorted sub-arrays. on Quick Sort Quick Best and average cases are Θ (n log n) Best Worst case is Θ (n2) Worst QuickSort Algorithm QuickSort QuickSort QuickSort How do we partiton? QuickSort QuickSort Procedure Code QuickSort Sample QuickSort QuickSort Sample QuickSort Sorting Comparison Sorting Quick Sort and Merge Sort will always be Quick faster than the Selection, Bubble, and Insertion sorts. Quick Sort sorts ``in place'' and the hidden Quick constant in the average case is smaller than Merge Sort's. Merge Sort’s worst case behavior is better Merge than Quick Sort’s worst case. than Sorting Comparison Sorting Bubble sort is O(N2). The number of Ops should come out <= 512 * ). 512 = 262144 Quicksort is O(2N lg N) on the average but can degenerate to ( N2)/2 )/2 in the worst case (try the ordered data set on quicksort). Quicksort is recursive and needs a lot of stack space. Shell sort (named for Mr. Shell) is less than O(N4/3) for this for implementation. Shell sort is iterative and doesn't require much extra memory. Merge sort is O( N lg N) for all data sets, so while it is slower than the Merge best case for quicksort, it doesn't have degenerate cases. It needs additional storage equal to the size of the input array and it is recursive so it needs stack space. Heap sort is guaranteed to be O(N lg N), doesn't degenerate like Heap quicksort and doesn't use extra memory like mergesort, but its implementation has more operations so on average its not as good as quicksort. quicksort. On average, Quick Sort is faster than Heap sort, but Heap sort is On Quick guaranteed to be fast, O(N*log(N)) (worst case = average case). guaranteed Sorting Comparison Sorting Average Bubble Selection Insertion Heap Merge Shell Quick N2 N2 N2 N* Log(N) N* Log(N) N4/3 N* Log(N) Worst Case N2 N2 N2 N* Log(N) N* Log(N) N4/3 N2 Best Case Stable? N N2 Yes Yes Yes No Yes No No N N* Log(N) N* Log(N) N* Log(N) N ...
View Full Document

Ask a homework question - tutors are online