Assignment Presentation
Assignment Presentation
E XP E R IM E N T WI T H R A N D O M IZ E D Q U I CK S O RT W IT H B O T H U NI F O R M
A N D N O R M AL D IS T R I B U T IO N A S IN P U T D ATA TO A R R IV E AT T H E
AV E R A G E C O M P L E X I T Y ( C O U N T O F O P E R AT IO N S P E R F O R ME D ) WI T H
B O T H IN P U T D ATA S E T S .
What is a Randomized
Algorithm?
An algorithm that uses random numbers to decide what to
do next anywhere in its logic is called a Randomized
Algorithm. For example, in Randomized Quick Sort, we use a
random number to pick the next pivot (or we randomly
shuffle the array). And in Karger’s algorithm, we randomly
pick an edge.
Randomized
Quicksort
Algorithm
Working of
Randomized
Quicksort
Time
Complexity of
RQ..
Randomized
quicksort has expected
time complexity as ,but worst-case
time complexity remains same. In
worst case the randomized function
can pick the index of corner element
every time.
Procedure
comparison and Time Taken per sort, then took their mean.
• We then Plotted the following graphs for both datasets
◦ vs
• vs
• We also plotted the usual Quick Sort Observations for
comparisons too.
Observation
Following Inference can be made
by the Observed Graph:
◦ As we did 50 sorts per n, and took
its mean, the time complexity
comes out to be same as Non-
Random Quick Sort, i.e.,
• As array size keep increasing, we
see similar observation, i.e., the
comparison ratio and time ratio
converges.
• The Divergence of Time ratio for Big array
size might be linked to low system RAM.
which asks for more Secondary Memory
Calls
Question 5
NOW NORMALIZE BOTH THE DATASETS IN THE RANGE
FROM 0 TO 1 AND IMPLEMENT BUCKET SORT (BS)
ALGORITHM AND CHECK FOR CORRECTNESS
• We did Bucket Sort for different array size, which starts from 2
and increments in powers of 2 till our system gives Error due to
Huge Size
• We were able to record observation from array size to array size
• For each array size , we sorted 100 different arrays of size , taken
Procedure from the datasets made in Question 1. Recorded the number of
comparison and Time Taken per sort, then took their mean.
• We then Plotted the following graphs for both datasets
◦ vs
• vs
Observation
◦ For big array size, the both plots for
both datasets converges to a
constant, which means that the
algorithm implemented is 𝑂
◦ There are deviations for small n,
this could be due to more
overhead of file read and write
compared to actual sort.
◦ Theoretically for Normal dataset,
the complexity should be more
towards , but as we averaged it
out, it became
Question 7
IMPLEMENT THE WORST CASE LINEAR MEDIAN SELECTION
ALGORITHM BY TAKING THE MEDIAN OF MEDIANS (MOM) AS
THE PIVOTAL ELEMENT AND CHECK FOR CORRECTNESS.
What is Median Of
Median (MoM)
The Median of Medians is an approximate (median) selection
algorithm, frequently used to supply a good pivot for an exact
selection algorithm, mainly the quicksort, that selects the
largest element of an initially unsorted array.
Median of medians finds an approximate median in linear time
only, which is limited but an additional overhead for quicksort.
When this approximate median is used as an improved pivot,
the worst-case complexity of quicksort reduces significantly
from to , which is also the asymptotically optimal worst-case
complexity of any sorting algorithm.
• We did Median of Median for fixed array size , but varied the
divide size in
Procedure • For each divide size, we computed MoM for 10 different arrays,
generated randomly (Uniform) with number in range . Recorded
Time Taken per Pass, then took their mean.
• We then Plotted the graph of time vs divide size
Observation
◦ From the graph we can infer that time complexity
is Linear with Divide Size
Question 9
PERFORM EXPERIMENTS BY REARRANGING THE ELEMENTS OF THE
D ATA S E T S ( B O T H U D A N D N D ) A N D C O M M E N T O N T H E PA RT IT IO N O R
S P L IT O B TA IN E D U S I N G T H E P I VO TA L E L E M E N T C HO S E N A S M O M .
• We did Median of Median for divide size 5, but varied the size of
array from 100 to , with increments of 100
• For each array size, we computed MoM for 10 different arrays,
Procedure chosen randomly form the datasets generated at question 1.
used Partition() with MoM as pivot element, and recorded the
output position, then took their mean.
• We then Plotted the graph of vs for both datasets.
Observation
◦
As we can see the varies in range
with majority of cases around 0.5.
◦ This shows that what MoM finds is
very close to actual median, and
hence MoM can mostly guarantee
a good pivot of Quicksort
Algorithm.
The End