quockSort irtuallab ass
quockSort irtuallab ass
quockSort irtuallab ass
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No: 22258 Submission date:
Demonstrate Quick Sort Virtual lab
Title:
Problem
Working of Quick sort using virtual lab
Statement
Refer lab manual for below
Prerequisites:
Objectives:
Theory:
Write algorithm and besides it example for each, attach both side ruled pages
in case required
Page 1 of 9
elements in right side won't cross the position of pivot after sorting
as they are greater than pivot) thus reducing the no. of elements we
have to check in further steps.
Steps to Sort an Unsorted Array with Quick Sort Algorithm
Page 2 of 9
The key process in Quick Sort is partition. Targets of partitions is,
given an array and an element x of array as pivot, put x at its correct
position in sorted array and put all smaller and equal elements
Page 3 of 9
(smaller than or equal to x) before x, and put all greater elements
(greater than x) after x.
Partitioning array around the pivot means we divide the array into
three parts
Partition-1 : Contains all elements of that array less than or
equal to pivot
Partition-2 : Contains only pivot
Page 4 of 9
What is Recursion?
On the given array we have to perform partition on that array and
recursively on partition-1 and partition-3. But we have to stop at
some point i.e., we will stop when the partition is already sorted.
Remember that array containing one element is always sorted. So,
we proceed till the partition is of length one.
What is Concatenation?
Page 5 of 9
Running Time of Quick Sort
Page 6 of 9
The time taken by quick sort to sort an array either in ascending or
descending order generally depends upon the input array and
partition strategy.
Following are three cases which we come across while sorting an
array with quick sort algorithm:
Best Case
Worst Case
Average Case
Best Case Time Complexity Analysis
Best case time complexity comes when the no. of levels of
recursion is minimum.As the partition divides it into two sub-
arrays, if there is a symmetry then both takes same time(both have
same no. of elements). Taking the pivot to be the middle element of
the sorted array(at each step of recursion), after partition one of the
subarray has n/2-1 elemenets while the other has n/2. The divide
continues till a single element is left in the array. So, n/(2^i)=1,
i=logn(base 2). The depth comes out to be logn. This partition goes
on for n elements of the array, hence the best time complexity
comes out to be T(n)=nlogn.
Worst Case Time Complexity Analysis
Page 7 of 9
factor of log(4/3)(base 2), which is a constant. So the average case
is taken to be O(nlogn) as well.
Space Complexity Analysis
So, the space complexity for one level of recursion will be O(logn)
if median of medians is used for pivot selection and O(1) otherwise.
But the no of function calls can go upto O(n) in worst case. So, the
call stack memory used can goes upto O(n) because of function
call. The space used by median of medians in current step is not
needed for further steps. So, overall space complexity of Quicksort
is O(n) due to function calls in call stack.
Graph : Time Complexities of Sorting Algorithms
Page 8 of 9
CONCLUSION:
Page 9 of 9