cs 307 fundamentals of computer science 1 bubble sort traverse a collection of elements –move...
TRANSCRIPT
![Page 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/1.jpg)
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
512354277 101
1 2 3 4 5 6
![Page 2: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/2.jpg)
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
512354277 101
1 2 3 4 5 6
Swap42 77
![Page 3: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/3.jpg)
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
512357742 101
1 2 3 4 5 6
Swap35 77
![Page 4: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/4.jpg)
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
512773542 101
1 2 3 4 5 6
Swap12 77
![Page 5: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/5.jpg)
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
577123542 101
1 2 3 4 5 6
No need to swap
![Page 6: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/6.jpg)
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
577123542 101
1 2 3 4 5 6
Swap5 101
![Page 7: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/7.jpg)
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
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
![Page 8: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/8.jpg)
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).
![Page 9: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/9.jpg)
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<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
![Page 10: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/10.jpg)
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
![Page 11: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/11.jpg)
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(N2)
Average case is O(NlogN)
![Page 12: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/12.jpg)
CS 307 Fundamentals of Computer Science
12
Summary I
Sort Best Case Worst Case Average Case
Insertion O(n) O(n2) O(n2)
Bubble O(n) O(n2) O(n2)
Heap O(nlog2n) O(nlog2n) O(nlog2n)
Quick O(nlog2n) O(n2) O(nlog2n)
Merge O(nlog2n) O(nlog2n) O(nlog2n)
![Page 13: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/13.jpg)
CS 307 Fundamentals of Computer Science
13
Summary II
Name In place Method
Bubble sort yes Exchanging
Insertion sort yes Insertion
Heap Sort yes Heap
Merge sort no Merging
Quicksort yes Partitioning
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.
![Page 14: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/14.jpg)
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.
![Page 15: 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](https://reader035.vdocuments.mx/reader035/viewer/2022081202/56649f1e5503460f94c362b5/html5/thumbnails/15.jpg)
CS 307 Fundamentals of Computer Science
15
Summary III
Name Stable Method
Bubble sort yes Exchanging
Insertion sort yes Insertion
Heap Sort no Heap
Merge sort yes Merging
Quicksort no Partitioning