1 / 15

bubble sort

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.

byrd
Télécharger la présentation

bubble sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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).

  9. 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

  10. 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

  11. 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)

  12. Summary I

  13. 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.

  14. 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.

  15. Summary III

More Related