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

Assignment Presentation

The document describes experiments performed with randomized quicksort and bucket sort algorithms on uniformly and normally distributed datasets. For randomized quicksort, the average complexity was found to be the same as non-randomized quicksort. Bucket sort experiments showed the algorithms' complexity to be constant for large array sizes. Median of medians was also implemented as a selection algorithm, with time complexity shown to be linear based on the divide size. Rearranging elements and using the median of medians as a pivot generally resulted in partitions close to 0.5, demonstrating it finds a good pivot.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment Presentation

The document describes experiments performed with randomized quicksort and bucket sort algorithms on uniformly and normally distributed datasets. For randomized quicksort, the average complexity was found to be the same as non-randomized quicksort. Bucket sort experiments showed the algorithms' complexity to be constant for large array sizes. Median of medians was also implemented as a selection algorithm, with time complexity shown to be linear based on the divide size. Rearranging elements and using the median of medians as a pivot generally resulted in partitions close to 0.5, demonstrating it finds a good pivot.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Question 4

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.
 

• We did Randomized Quicksort 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 50 different arrays of size , taken
from the datasets made in Question 1. Recorded the number of

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
 

• As All Values in our dataset was between 0 and 100, we took


each value one by one, divided each value by 100, and then
saved it to a different files.
Procedure • The no. of data in the dataset was kept same, i.e.
• We Then Plotted the histogram of the datasets to make sure the
conversion was conversion was correct.
Histogram for
Normal Dataset
◦ As Expected, the histogram has the
dumbbell curve and max value of
the dataset goes to 1.
◦ Hence, our conversion was
successful
Histogram for
Uniform Dataset
◦ As Expected, the histogram has the
same number of occurrences for
every numbers range and max
value of the dataset goes to 1.
◦ Hence, our conversion was
successful
Question 6
EXPERIMENT WITH BUCKET SORT TO ARRIVE AT ITS
AVERAGE COMPLEXITY FOR BOTH UNIFORM AND
NORMAL DATASETS AND INFER.
What is Bucket Sort
Bucket sort, or bin sort, is a sorting algorithm that works by
distributing the elements of an array into several buckets. Each
bucket is then sorted individually, either using a different
sorting algorithm, or by recursively applying the bucket sorting
algorithm. It is a distribution sort, a generalization of
pigeonhole sort, and is a cousin of radix sort in the most-to-
least significant digit flavor. Bucket sort can be implemented
with comparisons and therefore can also be considered a
comparison sort algorithm. The computational complexity
depends on the algorithm used to sort each bucket, the
number of buckets to use, and whether the input is uniformly
distributed.
Bucket sort
algorithm….
Bucket sort
implementatio
n on an array
Time Complexity: for best case
The and average case and for the
worst case.
complexity of
the Bucket
Sort Technique
Space Complexity: for worst case
 

• 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.
 

1. Divide elements into groups of element each, and


Median Of elements in the last group.
Median 2. Find median of each group using insertion sort and make
Algorithm an size array
3. Do steps 1 and 2 till we get a single value.
The
complexity of
the Median Of Time Complexity: for worst case
Median
Question 8
TAKE DIFFERENT SIZES FOR EACH TRIVIAL PARTITION
(3/5/7 ...) AND SEE HOW THE TIME TAKEN IS CHANGING.
 

• 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

You might also like