 Division of arrays in such a way that
 The sorted sub arrays do not need to be later
merged.
 This can be done by rearranging the elements in
A(1:n) such that
 A(i) <= A(j) for all i between 1 and m and all j
between m+1 and n.
 Thus the elements in A(1:m) and A(m+1: n) may
be independently sorted. No merge is needed
Instructor: Sadia Arshid
 To sort an array S
 If the number of elements in S is 0 or 1, then
return.
 Pick any element V in S. This is called pivot.
 Partition S-{V} (the remaining elements in S)
into two disjoint groups:
 S1= {x  S – {V} | x <= V},
 S2 = {x  S – {V} | x >= V}
 Return {quickSort(S1) followed by v followed
by quickSort(S2) }
Instructor: Sadia Arshid
Rearrange the array such that
 Element (pivot) is in its final position
 None of elements in A[1] .. A[m] is > pivot
 None of elements in A[m+1] .. A[n] is < pivot
pivot
elements lower
than pivot
elements higher
than pivot
 unsorted   unsorted 
Instructor: Sadia Arshid
Procedure quicksort(low,high)
If high>low
partition(low,high,Pivotpoint)
quicksort(low,pivotpint-1)
quicksort(pivotpoint+1,high)
Instructor: Sadia Arshid
procedure partition(low,high,pivotpoint)
pivotitem=s[low], j=low
for i=low+1 to high
if s[i]<pivotitm
j=j+1
exchange s[i] & s[j]
pivotpoint=j
exchange s[low] & s[pivotpoint]
Instructor: Sadia Arshid
The pivot is the smallest element all the time. Then
i = 0 and if we ignore T(0) = 0, which is insignificant,
the recurrence is
w(n)=case complexity of left side array + case
complexity of right side of array + complexity of
partition
in worst case left array would always b zero so right
array is of size n-1, n recursive call would be for n-1
elements
complexity of partition: n-1
Instructor: Sadia Arshid
w(n)=w(left subarray)+w(right subarray)+n-1
w(n)=w(0)+w(n-1)+n-1
=w(n-1)+n-1
w(n-1)=w(n-2)+n-2
w(n-2)=w(n-3)+n-3
by backward subsituation
w(n)=w(n-2)+n-2+n-1
=w(n-3)+n-3+n-2+n-1
……..
=w(1)+1+2+……..n-1
Instructor: Sadia Arshid
summation series of finite numbers
=n(n-1)/2 є O (n2 )
Instructor: Sadia Arshid
The pivot is the mid element all the time. Then
It equally divides array in two halves, the
recurrence is
T(n)=T(n/2)+T(n/2)+n-1
=2T(n/2)+n-1
………….
=nlgn-(n-1)єO(nlgn)
Instructor: Sadia Arshid
A(n)= 1/n∑ (A(p-1)+A(n-p))+n-1
1/n= probability each element to be pivot
item
A(p-1)+A(n-p)= average time to sort both left and
right sub array if p is pivot point
n-1=time to partition array
p=1
n
Instructor: Sadia Arshid
If pivot point p =1
Resultant sub arrays would be
A(0), A(n-1)
If p=2, A(1), A(n-2)
If p=3,A(2), A(n-3)
.
.
.
p=(n-2), A(n-3), A(2)
p=(n-1), A(n-2),A(1)
P=n,A(n-1),A(0)
Instructor: Sadia Arshid
As there is symmetry, and size of subarray is same for
p=1 & p=n-1, p=2 & p=n-2 …..
So to simplify equation we can say
2∑ A(p-1)=∑ A(p-1)+A(n-p)
Thus our equation would be
A(n)=2/n∑ A(p-1)+ n-1
nA(n)=2∑ A(p-1)+n(n-1)………..(1)
Similarly
(n-1)A(n-1)= 2∑ A(p-1) +(n-1)(n-2)………….(2)
p=1
n
p=
1
n
p=
1
n
p=
1
n
p=
1
n-1
Instructor: Sadia Arshid
by subtracting eq(2) from eq(1)
n A(n)-(n-1)A(n-1)=2A(n-1)+2(n-1)
nA(n)=(n+1)A(n-1)+2(n-1)
dividing whole eq with n(n+1)
A(n)/(n+1)=A(n-1)/n +2(n-1)/n(n+1)
to simplify equation assume
a(n)=A(n)/(n+1)
so
a(n)=a(n-1)+2(n-1)/n(n+1)
for larger value of n, (n-1)/(n+1)≈1
a(n)=a(n-1)+2/n for n>0, a(0)=0
a(n-1)=a(n-2)+2/n-1
a(n-2)=a(n-3)+2/n-2
by backward substitution
a(n-1)=a(n-3)+2/n-2+2/n-1
a(n)=a(n-1)+2/n-2+2/n-1+2/n
Instructor: Sadia Arshid
by generalizing equation
a(n)=a(n-i)+2∑1/k
terminating condition a(0)=0
by taking i=n
a(n)=a(0)+2∑ 1/k
=0+2ln(n), ln(n)=0.69 lg(n)
=1.38 lgn
as a(n)=A(n)/n+1
A(n)=1.38(n+1)lgnє O(nlgn)
k=0
i
K=1
n
Instructor: Sadia Arshid
Instructor: Sadia Arshid
procedure maxmin(i,j,fmax,fmin)
case:
i=j=fmax=fmin=S[i]
case:
i=j-1;if S[i]<S[j]; fmax=S[j], fmin=S[i]
else fmax=S[i],fmin=S[j]
else
mid=floor((i+j)/2)
maxmin(i,mid,gmax,gmin)
maxmin(mid+1,j,hmax,hmin)
fmax=max(gmax,hmax)
fmin=min(gmin,hmin)
Instructor: Sadia Arshid
 Input size=number of elements in array
 Basic operation: comparison, function call
 case complexity: every case
 recurrence relation:
 T(n)=T(floor(n/2))+T(ceil(n/2))+2
 base case: T(2)=1
by applying smoothness rule
 T(n)=2T(n/2)+2
Instructor: Sadia Arshid
T(n/2)=2T(n/4)+2
T(n/4)=2T(n/8)+2
by backward substitution
T(n)=2(2T(n/4)+2)+2
T(n)=22T(n/4)+22+2
=22(2T(n/8)+2)+22+2
=23T(n/8)+23+22+2
By generalizing
=2iT(n/2i)+2i+2i-1+…+2
=2iT(n/2i)+2(2i-1+2i-2+…….+1)
By applying geometric summation
=2iT(n/2i)+2(2i-1)
By taking 2i=n/2
=n/2T(2)+2(n/2-1)
=n/2+2n-2
=3n/2-2єӨ(n)
Instructor: Sadia Arshid

06 dc2

  • 1.
     Division ofarrays in such a way that  The sorted sub arrays do not need to be later merged.  This can be done by rearranging the elements in A(1:n) such that  A(i) <= A(j) for all i between 1 and m and all j between m+1 and n.  Thus the elements in A(1:m) and A(m+1: n) may be independently sorted. No merge is needed Instructor: Sadia Arshid
  • 2.
     To sortan array S  If the number of elements in S is 0 or 1, then return.  Pick any element V in S. This is called pivot.  Partition S-{V} (the remaining elements in S) into two disjoint groups:  S1= {x  S – {V} | x <= V},  S2 = {x  S – {V} | x >= V}  Return {quickSort(S1) followed by v followed by quickSort(S2) } Instructor: Sadia Arshid
  • 3.
    Rearrange the arraysuch that  Element (pivot) is in its final position  None of elements in A[1] .. A[m] is > pivot  None of elements in A[m+1] .. A[n] is < pivot pivot elements lower than pivot elements higher than pivot  unsorted   unsorted  Instructor: Sadia Arshid
  • 4.
  • 5.
    procedure partition(low,high,pivotpoint) pivotitem=s[low], j=low fori=low+1 to high if s[i]<pivotitm j=j+1 exchange s[i] & s[j] pivotpoint=j exchange s[low] & s[pivotpoint] Instructor: Sadia Arshid
  • 6.
    The pivot isthe smallest element all the time. Then i = 0 and if we ignore T(0) = 0, which is insignificant, the recurrence is w(n)=case complexity of left side array + case complexity of right side of array + complexity of partition in worst case left array would always b zero so right array is of size n-1, n recursive call would be for n-1 elements complexity of partition: n-1 Instructor: Sadia Arshid
  • 7.
    w(n)=w(left subarray)+w(right subarray)+n-1 w(n)=w(0)+w(n-1)+n-1 =w(n-1)+n-1 w(n-1)=w(n-2)+n-2 w(n-2)=w(n-3)+n-3 bybackward subsituation w(n)=w(n-2)+n-2+n-1 =w(n-3)+n-3+n-2+n-1 …….. =w(1)+1+2+……..n-1 Instructor: Sadia Arshid
  • 8.
    summation series offinite numbers =n(n-1)/2 є O (n2 ) Instructor: Sadia Arshid
  • 9.
    The pivot isthe mid element all the time. Then It equally divides array in two halves, the recurrence is T(n)=T(n/2)+T(n/2)+n-1 =2T(n/2)+n-1 …………. =nlgn-(n-1)єO(nlgn) Instructor: Sadia Arshid
  • 10.
    A(n)= 1/n∑ (A(p-1)+A(n-p))+n-1 1/n=probability each element to be pivot item A(p-1)+A(n-p)= average time to sort both left and right sub array if p is pivot point n-1=time to partition array p=1 n Instructor: Sadia Arshid
  • 11.
    If pivot pointp =1 Resultant sub arrays would be A(0), A(n-1) If p=2, A(1), A(n-2) If p=3,A(2), A(n-3) . . . p=(n-2), A(n-3), A(2) p=(n-1), A(n-2),A(1) P=n,A(n-1),A(0) Instructor: Sadia Arshid
  • 12.
    As there issymmetry, and size of subarray is same for p=1 & p=n-1, p=2 & p=n-2 ….. So to simplify equation we can say 2∑ A(p-1)=∑ A(p-1)+A(n-p) Thus our equation would be A(n)=2/n∑ A(p-1)+ n-1 nA(n)=2∑ A(p-1)+n(n-1)………..(1) Similarly (n-1)A(n-1)= 2∑ A(p-1) +(n-1)(n-2)………….(2) p=1 n p= 1 n p= 1 n p= 1 n p= 1 n-1 Instructor: Sadia Arshid
  • 13.
    by subtracting eq(2)from eq(1) n A(n)-(n-1)A(n-1)=2A(n-1)+2(n-1) nA(n)=(n+1)A(n-1)+2(n-1) dividing whole eq with n(n+1) A(n)/(n+1)=A(n-1)/n +2(n-1)/n(n+1) to simplify equation assume a(n)=A(n)/(n+1) so a(n)=a(n-1)+2(n-1)/n(n+1) for larger value of n, (n-1)/(n+1)≈1 a(n)=a(n-1)+2/n for n>0, a(0)=0 a(n-1)=a(n-2)+2/n-1 a(n-2)=a(n-3)+2/n-2 by backward substitution a(n-1)=a(n-3)+2/n-2+2/n-1 a(n)=a(n-1)+2/n-2+2/n-1+2/n Instructor: Sadia Arshid
  • 14.
    by generalizing equation a(n)=a(n-i)+2∑1/k terminatingcondition a(0)=0 by taking i=n a(n)=a(0)+2∑ 1/k =0+2ln(n), ln(n)=0.69 lg(n) =1.38 lgn as a(n)=A(n)/n+1 A(n)=1.38(n+1)lgnє O(nlgn) k=0 i K=1 n Instructor: Sadia Arshid
  • 15.
  • 16.
    procedure maxmin(i,j,fmax,fmin) case: i=j=fmax=fmin=S[i] case: i=j-1;if S[i]<S[j];fmax=S[j], fmin=S[i] else fmax=S[i],fmin=S[j] else mid=floor((i+j)/2) maxmin(i,mid,gmax,gmin) maxmin(mid+1,j,hmax,hmin) fmax=max(gmax,hmax) fmin=min(gmin,hmin) Instructor: Sadia Arshid
  • 17.
     Input size=numberof elements in array  Basic operation: comparison, function call  case complexity: every case  recurrence relation:  T(n)=T(floor(n/2))+T(ceil(n/2))+2  base case: T(2)=1 by applying smoothness rule  T(n)=2T(n/2)+2 Instructor: Sadia Arshid
  • 18.
    T(n/2)=2T(n/4)+2 T(n/4)=2T(n/8)+2 by backward substitution T(n)=2(2T(n/4)+2)+2 T(n)=22T(n/4)+22+2 =22(2T(n/8)+2)+22+2 =23T(n/8)+23+22+2 Bygeneralizing =2iT(n/2i)+2i+2i-1+…+2 =2iT(n/2i)+2(2i-1+2i-2+…….+1) By applying geometric summation =2iT(n/2i)+2(2i-1) By taking 2i=n/2 =n/2T(2)+2(n/2-1) =n/2+2n-2 =3n/2-2єӨ(n) Instructor: Sadia Arshid