420 likes | 1.23k Views
bubble sort. Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping. 1 2 3 4 5 6. 101. 12. 42. 35. 5. 77. bubble sort. 42. 77.
E N D
bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 12 42 35 5 77
bubble sort 42 77 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 1 2 3 4 5 6 101 12 42 35 5 77
bubble sort 35 77 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 77 35 5 42
bubble sort 12 77 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 35 77 5 42
bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 77 35 12 5 42 No need to swap
bubble sort 5 101 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 77 35 12 5 42
bubble sort • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed
Insertion Sort • Insertion sort is a simple sorting algorithm that is appropriate for small inputs (not good for large amounts of data) • In each step of an insertion sort, one or more pieces of data are inserted into their correct location in an ordered list (just as a card player picks up cards and places them in his hand in order).
Running Time • The insertion sort is quadratic in the worst and average cases • The running time is linear O(N) if input is pre-sorted. • The running time depends on the amount of the input and the specific ordering of input • An inversion is a pair of elements that are out of order in an array • Any ordered pair (i,j) has the property that i<j but Ai>Aj • E.g., {8, 5, 9, 2, 6, 3} has 10 inversions • Number of inversions measures the unsortedness • The average number of inversion in an array of N distinct numbers in N(N-1)/4
Merge sort • Merge sort uses linear extra memory merging two sorted lists • Additional work in copying to the temporary array and back • Excessive copying can be avoided with more work, but the linear extra memory can not be removed without excessive time penalities. • Switching the role of a and tempArray
Quick Sort • Quick sort is fast because the partitioning step can be performed quickly and in place • Significantly faster than merging step • Without an extra array • Best case: O(NlogN): two half-sized recursive calls with linear overhead • Worst case: occurs when the partition repeatedly generates an empty subsets. • T(N)=T(N-1)+N • The running time is then O(N2) • Average case is O(NlogN)
Summary II A sorting algorithm is in-place if it uses only a small Number of memory in addition to that needed to store the input array. In other words, only a constant Number of array elements are stored outside the input array at any time.
Some Remarks • Insertion-sort is a good choice for small input size (say, less than 50) and for sequences that are already “almost” sorted. • Merge-sort is difficult to run in-place, is an excellent algorithm for situations where the input can not fit into main memory, but must be stored in blocks on an external memory device, e.g., disks. • Quick sort is an excellent choice for general-purpose, in-memory sorting. In spite of its slow worst-case running time. The constant factors hidden in O(nlgn) for average case are quite small.