I2EA 3 Sorting P
I2EA 3 Sorting P
Sorting
naive algorithm
naive algorithm
1) compute minimum 𝑚𝑖𝑛 {𝑎 (1), … , 𝑎 (𝑛) } ¿ 𝑚𝑖𝑛 {𝑚𝑖𝑛 {𝑎 (1) , 𝑎 (2) }, 𝑎 (3) , … , 𝑎 (𝑛)}
𝑀 (𝑛) comparisons
Sorting
naive algorithm
1) compute minimum 𝑚𝑖𝑛 {𝑎 (1), … , 𝑎 (𝑛) } ¿ 𝑚𝑖𝑛 {𝑚𝑖𝑛 {𝑎 (1) , 𝑎 (2) }, 𝑎 (3) , … , 𝑎 (𝑛)}
𝑀 (𝑛) comparisons
Sorting
naive algorithm
1) compute minimum 𝑚𝑖𝑛 {𝑎 (1), … , 𝑎 (𝑛) } ¿ 𝑚𝑖𝑛 {𝑚𝑖𝑛 {𝑎 (1) , 𝑎 (2) }, 𝑎 (3) , … , 𝑎 (𝑛)}
𝑀 (𝑛) comparisons
Sorting
naive algorithm
1) compute minimum 𝑚𝑖𝑛 {𝑎 (1), … , 𝑎 (𝑛) } ¿ 𝑚𝑖𝑛 {𝑚𝑖𝑛 {𝑎 (1) , 𝑎 (2) }, 𝑎 (3) , … , 𝑎 (𝑛)}
comparisons
2) repeat
𝑠𝑜𝑟𝑡 ( 𝐴)=𝑚𝑖𝑛( 𝐴) ∘ 𝑠𝑜𝑟𝑡 ¿
Sorting
naive algorithm
1) compute minimum 𝑚𝑖𝑛 {𝑎 (1), … , 𝑎 (𝑛) } ¿ 𝑚𝑖𝑛 {𝑚𝑖𝑛 {𝑎 (1) , 𝑎 (2) }, 𝑎 (3) , … , 𝑎 (𝑛)}
comparisons
2) repeat
𝑠𝑜𝑟𝑡 ( 𝐴)=𝑚𝑖𝑛( 𝐴) ∘ 𝑠𝑜𝑟𝑡 ¿
𝑆 (𝑛) comparisons
Sorting
naive algorithm
1) compute minimum 𝑚𝑖𝑛 {𝑎 (1), … , 𝑎 (𝑛) } ¿ 𝑚𝑖𝑛 {𝑚𝑖𝑛 {𝑎 (1) , 𝑎 (2) }, 𝑎 (3) , … , 𝑎 (𝑛)}
comparisons
2) repeat
𝑠𝑜𝑟𝑡 ( 𝐴)=𝑚𝑖𝑛( 𝐴) ∘ 𝑠𝑜𝑟𝑡 ¿
𝑆 (𝑛) comparisons
merge 2 sorted lists of length n/2 Merge Sort
input: sorted sequences
with
(𝑏 (1)) , … , 𝑏(𝑛 /2) ¿
𝑏( 1) ≤ … ≤ 𝑏(𝑛 / 2)
with
(𝑏 (1)) , … , 𝑏(𝑚) ¿
𝑏( 1) ≤ … ≤ 𝑏(𝑛𝑚)
𝑛+𝑚 − 1 comparisons
merge 2 sorted lists of length n/2 Merge Sort
input: sorted sequences
with
(𝑏 (1)) , … , 𝑏(𝑚) ¿
𝑏( 1) ≤ … ≤ 𝑏(𝑛𝑚)
𝑛+𝑚 − 1 comparisons
𝑠𝑜𝑟𝑡 ¿
merge 2 sorted lists of length n/2 Merge Sort
input: sorted sequences
with
(𝑏 (1)) , … , 𝑏(𝑚) ¿
𝑏( 1) ≤ … ≤ 𝑏(𝑛𝑚)
𝑛+𝑚 − 1 comparisons
𝑠𝑜𝑟𝑡 ¿
difference equations (and difference inequalities)
merge 2 sorted lists of length n/2 Merge Sort
input: sorted sequences
with
(𝑏 (1)) , … , 𝑏(𝑚) ¿
𝑏( 1) ≤ … ≤ 𝑏(𝑛𝑚)
𝑛+𝑚 − 1 comparisons
𝑠𝑜𝑟𝑡 ¿
𝑆 (𝑛)<𝑛 ⋅ log 𝑛
can we sort asymptotically faster with comparisons?
can we sort asymptotically faster with comparisons?
no
no
(𝑎 (1) , … , 𝑎( 𝑛))
can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
can we sort asymptotically faster with comparisons?
no
(𝑎 (1) , … , 𝑎( 𝑛)) can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
• at leaves: sorted sequence
• one leaf for each permutation 𝜋
(𝑎 ( 𝜋 (1)) , … , 𝑎 ( 𝜋 (𝑛)))
can we sort asymptotically faster with comparisons?
no
(𝑎 (1) , … , 𝑎( 𝑛)) can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
• at leaves: sorted sequence
• one leaf for each permutation 𝜋
(𝑎 ( 𝜋 (1)) , … , 𝑎 ( 𝜋 (𝑛)))
can we sort asymptotically faster with comparisons?
no
(𝑎 (1) , … , 𝑎( 𝑛)) can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
• at leaves: sorted sequence
• one leaf for each permutation 𝜋
(𝑎 ( 𝜋 (1)) , … , 𝑎 ( 𝜋 (𝑛)))
can we sort asymptotically faster with comparisons?
no
(𝑎 (1) , … , 𝑎( 𝑛)) can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
• at leaves: sorted sequence
• one leaf for each permutation 𝜋
(𝑎 ( 𝜋 (1)) , … , 𝑎 ( 𝜋 (𝑛)))
no
(𝑎 (1) , … , 𝑎( 𝑛)) can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
• at leaves: sorted sequence
• one leaf for each permutation 𝜋
(𝑎 ( 𝜋 (1)) , … , 𝑎 ( 𝜋 (𝑛)))
𝑑
2 ≥ 𝑛 !> 𝑛⋅ (𝑛 − 1 ) ⋅ … ⋅ 𝑛/ 2 >¿
can we sort asymptotically faster with comparisons?
no
(𝑎 (1) , … , 𝑎( 𝑛)) can represent any such algorithm as binary tree
• nodes: comparisons
• at root: input sequence or set to be sorted
• at leaves: sorted sequence
• one leaf for each permutation 𝜋
(𝑎 ( 𝜋 (1)) , … , 𝑎 ( 𝜋 (𝑛)))
𝑑
2 ≥ 𝑛 !> 𝑛⋅ (𝑛 − 1 ) ⋅ … ⋅ 𝑛/ 2 >¿
𝑑>(𝑛/ 2) ⋅ log (𝑛 / 2)
Probabilistic Analysis of Algorithms
two flavors
• random input
• modeling: our lack of understanding of what future inputs we will see
• bad: what if an adversary constructs the input - when it matters?
two flavors
• random input
• modeling: our lack of understanding of what future inputs we will see
• bad: what if an adversary constructs the input - when it matters?
two flavors
• random input
• modeling: our lack of understanding of what future inputs we will see
• bad: what if an adversary constructs the input - when it matters?
the miracle
input: (𝑎 (1) , … , 𝑎( 𝑛)) or set 𝐴= {𝑎 (1) ,… , 𝑎(𝑛) } we assume here: a(i) mutually distinct
input: (𝑎 (1) , … , 𝑎( 𝑛)) or set 𝐴= {𝑎 (1) ,… , 𝑎(𝑛) } we assume here: a(i) mutually distinct
2013
now computer science textbooks continue ….. since decades
here: the algorithm does the random experiments
input: (𝑎 (1) , … , 𝑎( 𝑛)) or set 𝐴= {𝑎 (1) ,… , 𝑎(𝑛) } we assume here: a(i) mutually distinct
𝑛
𝑇 (𝑛)≤ 𝑛+(1/ 𝑛) ⋅ Σ 𝑖=1 (𝑇 (𝑖 − 1)+𝑇 (𝑛 − 𝑖))
now computer science textbooks continue ….. since decades
here: the algorithm does the random experiments
input: (𝑎 (1) , … , 𝑎( 𝑛)) or set 𝐴= {𝑎 (1) ,… , 𝑎(𝑛) } we assume here: a(i) mutually distinct
𝑇 (𝑛) expected run time for sorting n elements in mathematics expected runtime has a definition
𝑛
𝑇 (𝑛)≤ 𝑛+(1/ 𝑛) ⋅ Σ 𝑖=1 (𝑇 (𝑖 − 1)+𝑇 (𝑛 − 𝑖))
now computer science textbooks continue ….. since decades
here: the algorithm does the random experiments
input: (𝑎 (1) , … , 𝑎( 𝑛)) or set 𝐴= {𝑎 (1) ,… , 𝑎(𝑛) } we assume here: a(i) mutually distinct
𝑇 (𝑛) expected run time for sorting n elements in mathematics expected runtime has a definition
𝑛
𝑇 (𝑛)≤ 𝑛+(1/ 𝑛) ⋅ Σ 𝑖=1 (𝑇 (𝑖 − 1)+𝑇 (𝑛 − 𝑖)) this should have a proof!