Download presentation
Presentation is loading. Please wait.
1
Quadratic Sorts & Breaking the O(n2) Barrier
2
Sort Terminology Stable : maintains original ordering for "equal" items Given A B E B C B will always be before B
3
Sort Terminology Adaptive : Performs better when data is already sorted
4
Bubble Sort Size - 1 times:
Walk through array, swap neighbors if out of order
5
Analysis K = 1 : i repeats size - 1 times K = 2 : i repeats size - 2 times K = 3 : i repeats size - 3 times … K = size - 3 : i = repeats 3 times K = size - 2 : i = repeats 2 times K = size - 1 : i = repeats 1 times O(1)
6
( 𝑛−1 2 𝑔𝑟𝑜𝑢𝑝𝑠)(𝑛 𝑐𝑜𝑚𝑝𝑎𝑟𝑖𝑠𝑜𝑛𝑠)= 𝑛−1 2 𝑛 = 𝑛 2 −𝑛 2 = 𝑛 2
Analysis size - 1 size - 2 size - 3 … ( 𝑛−1 2 𝑔𝑟𝑜𝑢𝑝𝑠)(𝑛 𝑐𝑜𝑚𝑝𝑎𝑟𝑖𝑠𝑜𝑛𝑠)= 𝑛−1 2 𝑛 = 𝑛 2 −𝑛 2 = 𝑛 2
7
Analysis O(n2) work (n levels of n comparisons & swaps) Stable
8
Analysis Can make adaptive with done flag
Best case now O(n) (1 pass through n things) Worst still O(n2)
9
Selection Sort Size - 1 times:
Find largest remaining unsorted and swap to end
10
Selection Sort OR Size - 1 times:
Find smallest remaining unsorted and swap to start
11
Analysis O(n) swaps O(n2) comparisons (n passes each that does up to n comparisons) Overall O(n2)
12
Analysis Not adaptive Not stable
Swap can move items out of original order:
13
Insertion Sort Size - 1 times:
Take first unsorted item and swap it to the left until sorted
14
Analysis O(n2) swaps & comparisons Overall O(n2)
15
Analysis Adaptive (If doing n passes of O(1) work) Stable
No big leaps Good choice for small problems / mostly sorted data
16
Breaking N2 Bubble, Insertion, Selction all N2 Why?
17
Inversions Inversions = out of order elements For:
inversions are: (1,2), (1,3), (1,5), (3,5), (4,5) Item 1 2 3 4 5 Value 29 10 14 37 13
18
Inversions Worst case inversions:
Every element inverted vs every following element Inversions Count (1,2), (1,3), (1,4), …, (1,n), n-1 (2,3), (2,4), …, (2,n), n-2 (3,4), …, (3,n), n-3 ⋮ (n-1,n) 1
19
Inversions Worst case inversions:
Every element inverted vs every following element Inversions Count (1,2), (1,3), (1,4), …, (1,n) n-1 (2,3), (2,4), …, (2,n) n-2 (3,4), …, (3,n) n-3 ⋮ (n-2, n-1), (n-2, n) 2 (n-1,n) 1 (N-1) / 2 groups of N = (N2 - N) / 2
20
Limits Array can have n(n-1)/2 n2 inversions
Swap of neighbors can fix only one inversion Any sort algorithm that swaps adjacent pairs is O(n2)
21
Shell Sort Shell sort Insertion sort where items jump multiple spots
22
Shell Sort Multiple passes, descending jump size on each pass
Decrease by at least constant divisor Big O depends on sequence
23
Shell Sort BigO jumpSize = size / 3 repeat until jumpSize length = 0
For each of jumpSize starting locations Do insertion sort starting there, steping by jump size
24
Shell Sort BigO Exact big O depends on sequence
Shown sequence O( 𝑛 ) Best known case : O(n*log(n)2) Lowest possible big O unproven
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.