Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 307 Fundamentals of Computer Science 1 bubble sort  Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to.

Similar presentations


Presentation on theme: "CS 307 Fundamentals of Computer Science 1 bubble sort  Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to."— Presentation transcript:

1 CS 307 Fundamentals of Computer Science 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 5 12 3542 77 101 1 2 3 4 5 6

2 CS 307 Fundamentals of Computer Science 2 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 5 12 3542 77 101 1 2 3 4 5 6 Swap 4277

3 CS 307 Fundamentals of Computer Science 3 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 5 12 3577 42 101 1 2 3 4 5 6 Swap 3577

4 CS 307 Fundamentals of Computer Science 4 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 5 12 7735 42 101 1 2 3 4 5 6 Swap 1277

5 CS 307 Fundamentals of Computer Science 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 5 77 1235 42 101 1 2 3 4 5 6 No need to swap

6 CS 307 Fundamentals of Computer Science 6 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 5 77 1235 42 101 1 2 3 4 5 6 Swap 5101

7 CS 307 Fundamentals of Computer Science 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 77 1235 42 5 1 2 3 4 5 6 101 Largest value correctly placed

8 CS 307 Fundamentals of Computer Science 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 CS 307 Fundamentals of Computer Science 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 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 CS 307 Fundamentals of Computer Science 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 CS 307 Fundamentals of Computer Science 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(N 2 )  Average case is O(NlogN)

12 CS 307 Fundamentals of Computer Science 12 Summary I SortBest CaseWorst CaseAverage Case InsertionO(n)O(n 2 ) BubbleO(n)O(n 2 ) HeapO(nlog 2 n) QuickO(nlog 2 n)O(n 2 )O(nlog 2 n) MergeO(nlog 2 n)

13 CS 307 Fundamentals of Computer Science 13 Summary II NameIn placeMethod Bubble sortyesExchanging Insertion sortyesInsertion Heap SortyesHeap Merge sortnoMerging QuicksortyesPartitioning 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 CS 307 Fundamentals of Computer Science 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 CS 307 Fundamentals of Computer Science 15 Summary III NameStableMethod Bubble sortyesExchanging Insertion sortyesInsertion Heap SortnoHeap Merge sortyesMerging QuicksortnoPartitioning


Download ppt "CS 307 Fundamentals of Computer Science 1 bubble sort  Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to."

Similar presentations


Ads by Google