The document describes quicksort algorithm. It partitions an array around a pivot element such that elements smaller than the pivot are moved before it and elements larger than the pivot are moved after it. This partitions the array into two independent sub-arrays that can then each be sorted recursively without requiring a merge. The overall time complexity of quicksort is O(n log n) on average and O(n^2) in the worst case.
UiPath Test Automation using UiPath Test Suite series, part 1
06 dc2
1. 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
2. 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
3. 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
6. 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
8. summation series of finite numbers
=n(n-1)/2 є O (n2 )
Instructor: Sadia Arshid
9. 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
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 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
12. 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
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
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
17. 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