0% found this document useful (0 votes)
43 views

Insert Sort

This document discusses algorithms for sorting arrays and integer exponentiation. It analyzes the time complexity of insertion sort and modexp integer exponentiation. Insertion sort works by inserting elements into a sorted portion of the array. It is shown to have a time complexity of O(n^2) as each insertion takes linear time and there are n insertions. Modexp performs integer exponentiation by repeated multiplication. Using the master theorem, its time complexity is shown to be O(n) for multiplying and O(n^3) or O(n^2.59) depending on the multiplication algorithm used.

Uploaded by

atkawa7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Insert Sort

This document discusses algorithms for sorting arrays and integer exponentiation. It analyzes the time complexity of insertion sort and modexp integer exponentiation. Insertion sort works by inserting elements into a sorted portion of the array. It is shown to have a time complexity of O(n^2) as each insertion takes linear time and there are n insertions. Modexp performs integer exponentiation by repeated multiplication. Using the master theorem, its time complexity is shown to be O(n) for multiplying and O(n^3) or O(n^2.59) depending on the multiplication algorithm used.

Uploaded by

atkawa7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

function insertion-sort(A , n )

Input: Ar ray A of lengthat least n.


Output: A[0]..A [n 1] are permuted into sorted order.
if n < 2, return.
insertion-sort( A, n 1).
insert( A, n).
return.
function insert(A , n )
Input: Ar ray A of lengthat least n such that A[0..A [n 2] are sorted.
Output: A[0]..A [n 1] are permuted into sorted order.
if n < 2, return.
if A[n 1] A[n 2], r eturn.
swap( A[n 1], A [n 2]).
insert( A, n 1).
return.
Let T
in
( n) be the cost of insert(A, n) . T hen
T
in
( n) T
in
( n 1) + c, for n > 1.
Th us by the muster t heorem, T
in
( n) i s in O( n).
Let T
is
( n) be the cost of insertion-sort(A, n) . T hen
T
is
( n) T
is
( n 1) + O( n) , for n > 1.
In other words,
T
is
( n) T
is
( n 1) + c n, for n > 1 and for some constant c.
Th us by the muster t heorem, T
is
( n) i s in O( n
2
).
Let n be given and let T
m
( n) be the number of multiplications used in modexpwhen the
exponent e has n bits. T his T
m
satisifes
T
m
( n) T
m
( n 1) + 2.
Th us by the muster t heoremT
m
( n) i s in O( n).
Let T ( n) be the runtime cost of modexp(a, e, N) on n-bit inputs. If we use classical
multiplication, each multiplication costs O( n
2
) so T ( n) i s in O( n
3
). If we use karatsuba
multiplication (the divide and conquer approach of chapter 2.1) , T ( n) i s in O( n
2. 59
).
1

You might also like