SlideShare a Scribd company logo
Quick Sort Algorithm
Dr J P Patra
Associate Professor
Computer Science & Engineering
UTD, CSVTU, Bhilai
1
Outline:
• What is Quick Sort in Data Structure?
• When to Use Quick Sort?
• Explanation of Quick Sort Algorithm
• Example of Quick Sort
• Time Complexity of Quick Sort
• Advantages and Disadvantages of Quick Sort
• Applications of Quick Sort
2
What is Quick Sort in Data Structure?
•Quick Sort is a sorting algorithm based on the
Divide and Conquer approach that picks an
element as a pivot and partitions the given
array around the picked pivot by placing the
pivot in its correct position in the sorted array.
3
What is Divide and Conquer approach?
4
This technique can be divided into the following 3 parts:
• Divide: This involves dividing the problem into smaller
sub-problems.
• Conquer: Solve sub-problems by calling recursively until
solved.
• Combine: Combine the sub-problems to get the final
solution of the whole problem.
5
Here is the three-steps divide-and-conquer process for
sorting a typical array A[p….r].
Divide: Partition (rearrange) the array A[p.....r] into two
(possibly empty) subarrays A[p.....q - 1] and A[q+1.....r] such
that each element of A[p.....q - 1] is less than to A[q], and
each element of A[q+1......r] is greater than or equal to A[q]
Compute the index q as part of this partitioning procedure.
6
Conquer: Sort the two subarrays A[p....q - 1] and
A[q+1......r] by recursive calls to quicksort.
Combine: Since the subarrays are sorted in place, now
work is needed to combine them
So we can define that the entire array A[p....r] is now
sorted.
Quick Sort Algorithm
7
Quick Sort Partition Algorithm
8
9
• Here the value of p=1 and r=8
• As per the Partition Algorithm
the value of x=4 (As A[r]=A[8]=4)
• The value of i=0 (i.e p-1= 1-1=0)
• As per step no. 3 for loop will
start from (p=1) and it will
continue up to (r-1=8-1=7).
Example : A=[2, 8, 7, 1, 3, 5, 6, 4]
10
• A=[2, 8, 7, 1, 3, 5, 6, 4]
• Here the value of
x= 4, r=8, i=0, j=1, A[1]=2
do if (2≤ 4) Ture
• Then the value of iwill be 1 (i. e i=1)
and we have to swap :A[1] ↔ A[1]
• Output :
Process for j=1
11
• A=[2, 8, 7, 1, 3, 5, 6, 4]
• Here the value of
x= 4, r=8, i=1, j=2, A[2]=8
do if (8≤ 4) False
• Then we will skip the steps 5 and
6 and the output after
processing j=2 will be define as
• Output :
Process for j=2
12
• A=[2, 8, 7, 1, 3, 5, 6, 4]
• Here the value of
x= 4, r=8, i=1, j=3, A[3]=7
do if (7≤ 4) False
• Then we will skip the steps 5 and
6 and the output after
processing j=3 will be define as
• Output :
Process for j=3
13
• A=[2, 8, 7, 1, 3, 5, 6, 4]
• Here the value of
x= 4, r=8, i=1, j=4, A[4]=1
do if (1≤ 4) True
• Then the value of i will be 2 (i. e i=2)
and we have to swap :A[2] ↔ A[4]
• Output :
Process for j=4
14
• A=[2, 1, 7, 8, 3, 5, 6, 4]
• Here the value of
x= 4, r=8, i=2, j=5, A[5]=3
do if (3≤ 4) True
• Then the value of i will be 3 (i. e i =3)
and we have to swap :A[3] ↔ A[5]
• Output :
Process for j=5
15
• A=[2, 1, 3, 8, 7, 5, 6, 4]
• Here the value of
x= 4, r=8, i=3, j=6, A[6]=5
do if (5≤ 4) False
• Then we will skip the steps 5 and 6
and the output after processing j=6
will be define as
• Output :
Process for j=6
16
• A=[2, 1, 3, 8, 7, 5, 6, 4]
• Here the value of
x= 4, r=8, i=3, j=7, A[7]=6
do if (6 ≤ 4) False
• Then we will skip the steps 5 and 6
and the output after processing j=7
will be define as
• Output :
Process for j=7
17
• A=[2, 1, 3, 8, 7, 5, 6, 4]
or
Here the value of
x= 4, r=8, i=3, j=7, A[7]=6
Now using step 7 we have to exchange
A[i+1] ↔ A[r] i.e A[4] ↔ A[8]
Output:
Step 8 will return value 4
Process Step 7 and 8
• Using Quick Sort Partition algorithm return the value 4 and
that value will be assign to variable q.
• Now we divide the array in two sub arrays A[1…3] and
A[5….8] and the position of the Pivot element is 4.
18
Sub Array2 A[5….8]
Sub Array1 A[1….3] Position of Pivot
element
19
• After getting the value
of q we will call
Quicksort(A, p, q-1)
and Quicksort(A, q+1,
r)
• This process will
continue until we
satisfy the condition.
Best-case running time
20
• In the most even possible split, PARTITION produces two
subproblems, each of size no more than (n/2), since one
is of size ⌊n/2⌋ and one of size ⌊(n/2)-1⌋.In this case,
quicksort runs much faster.
• The recurrence for the running time is then:
T(n) =2T(n/2) + (n)
21
Here T(n) =2T(n/2) + (n)
So we can solve the above recurrence equation using
Master Method
Master Method Standard equation is T(n) =aT(n/b) + f(n)
If we compare the two equations we will obtained the
value of a=2, b=2 and f(n)=n
22
Now we have to calculate n
loga
b
=> n
log2
2
=> n
If we compare the value of f(n) and n
loga
b both values are
equal.
So, T(n) =0(f(n).logn)
=>T(n)=0(nlogn)
Best Case running time of Quick sort is 0(nlogn)
Worst-case running time
23
• The efficiency of the Quicksort algorithm very much
depends on the selection of the pivot element.
• Let’s assume the input of the Quicksort is a sorted array and
we choose the leftmost element as a pivot element.
• In this case, we’ll have two extremely unbalanced arrays.
One array will have one element and the other one will
have (N - 1) elements.
Worst-case running time
24
• Similarly, when the given input array is sorted reversely and
we choose the rightmost element as the pivot element, the
worst case occurs. Again, in this case, the pivot elements
will split the input array into two unbalanced arrays.
Worst Case Time Complexity Analysis
25
Worst Case Time Complexity Analysis
26
• All the numbers in the box denote the size of the
arrays or the subarrays.
• Let’s consider an input array of size N. The first
partition call takes N times to perform the partition
step on the input array.
• Each partition step is invoked recursively from the
previous one. Given that, we can take the complexity
of each partition call and sum them up to get our
total complexity of the Quicksort algorithm
27
Alternatively, we can create a recurrence relation for
computing it.
Or
28
• In the worst case, after the first partition, one array will
have 1 element and the other one will have (N-1)
elements.
• Let’s say T(N) denotes the time complexity to sort N
elements in the worst case:
• T(N) = Time needed to partition N elements + Time
needed to sort (N - 1) elements recursively = N + T(N - 1)
• Again for the base case when N = 0 and 1, we don’t need
to sort anything. Hence, the sorting time is 0 and T(0)=
T(1) = 0
29
Now, we’re ready to solve the recurrence relation
T(N) = N + T(N- 1),
T(N - 1) = (N - 1) + T(N- 2),
T(N - 2) = (N - 2) + T(N- 3),
T(N - 3) = (N - 3) + T(N- 4),
......
......
......
T(3) = 3 + T(2),
T(2) = 2 + T(1),
T(1) = 0
30
As a result, T(N) = N + (N-1) + (N-2) ... + 3 + 2
= [
𝑵(𝑵+𝟏)
𝟐
- 1] =O(N2)
Advantages of Quick Sort
31
• Fast and efficient : Quick sort is the most favored users’
choice to perform sorting functions quickly and
efficiently. It allows users to achieve the same result
much quicker than other sorting algorithms
• Cache friendly :The most talked-about feature of
quicksort is its in-place sorting. This means, at the time
of sorting, the algorithm doesn’t need additional storage
space. Thus, the sorted list occupies the same storage as
the unsorted list, and the sorting takes place in the given
area
Advantages of Quick Sort
32
• Good for parallelization: Quick sort can be parallelized
easily, taking advantage of multiple processors or
computing resources. By splitting the input array into
sub-arrays and sorting them concurrently, it can achieve
faster sorting times on systems with parallel processing
capabilities.
• Simple implementation: Quick sort's algorithmic logic is
relatively straightforward, making it easier to
understand and implement compared to some other
complex sorting algorithms
Advantages of Quick Sort
33
• Time complexity: Time complexity is the time taken by
the algorithm to run until its completion. Compared to
other sorting algorithms, it can be said that the time
complexity of quick sort is the best
• Space complexity: Quick sort has a space complexity of
O(logn), making it a suitable algorithm when you have
restrictions in space availability
Disadvantages of Quick Sort
34
• Unstable: Quick sort is undoubtedly a fast and efficient
sorting algorithm, but when it comes to stability, you
might want to reconsider your options. This sorting
algorithm is regarded unstable as it does not retain the
original order of the key-value pair.
• Worst-case time complexity: This is actually a drawback
of quick sort. It usually occurs when the element
selected as the pivot is the largest or smallest element,
or when all the elements are the same. These worst
cases drastically affect the performance of the quick
sort.
Disadvantages of Quick Sort
35
• Dependency on Pivot Selection: The choice of pivot
significantly affects the performance of quick sort. If a poorly
chosen pivot divides the array into highly imbalanced
partitions, the algorithm's efficiency may degrade. This issue
is particularly prominent when dealing with already sorted or
nearly sorted arrays.
• Recursive Overhead: Quick sort heavily relies on recursion to
divide the array into subarrays. Recursive function calls
consume additional memory and can lead to stack overflow
errors when dealing with large data sets. This makes quick
sort less suitable for sorting extremely large arrays.
Disadvantages of Quick Sort
36
• Inefficient for Small Data Sets: Quick sort has additional
overhead in comparison to some simpler sorting algorithms,
especially when dealing with small data sets. The recursive
nature of quick sort and the constant splitting of the array can
be inefficient for sorting small arrays or arrays with a small
number of unique elements.
• Not Adaptive: Quick sort's performance is not adaptive to the
initial order of the elements. It does not take advantage of any
pre-existing order or partially sorted input. This means that
even if a portion of the array is already sorted, the quick sort
will still perform partitioning operations on the entire array.
Applications of Quick Sort
37
• Commercial Computing is used in various government and
private organizations for the purpose of sorting various
data like sorting files by name/date/price, sorting of
students by their roll no., sorting of account profile by given
id, etc.
• The sorting algorithm is used for information searching and
as Quicksort is the fastest algorithm so it is widely used as a
better way of searching.
• It is used everywhere where a stable sort is not needed.
Applications of Quick Sort
38
• Quicksort is a cache-friendly algorithm as it has a good
locality of reference when used for arrays.
• It is tail -recursive and hence all the call optimization can be
done.
• It is used to implement primitive type methods.
• It is used in operational research and event-driven
simulation.
39
Conclusion:
Overall, quick sort is a powerful data structure algorithm
with advantages such as efficiency and versatility. It is an
essential tool for developers to have in their belt when
dealing with large amounts of data. Quicksort is an
efficient, unstable sorting algorithm with time
complexity of O(n log n) in the best and average case
and O(n²) in the worst case.
40
Ad

More Related Content

What's hot (20)

Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi Algorithm
Jorge Jasso
 
Space Invaders Tokyo 2017
Space Invaders Tokyo 2017Space Invaders Tokyo 2017
Space Invaders Tokyo 2017
Prénom RVLEB
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
Monalisa Patel
 
Sorting Methods.pptx
Sorting Methods.pptxSorting Methods.pptx
Sorting Methods.pptx
Bharati Vidyapeeth COE, Navi Mumbai
 
手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門
Yen-lung Tsai
 
VCE - UNIT-II.pptx
VCE - UNIT-II.pptxVCE - UNIT-II.pptx
VCE - UNIT-II.pptx
skilljiolms
 
Queue
QueueQueue
Queue
Allana Institute of management sciences
 
C#次世代非同期処理概観 - Task vs Reactive Extensions
C#次世代非同期処理概観 - Task vs Reactive ExtensionsC#次世代非同期処理概観 - Task vs Reactive Extensions
C#次世代非同期処理概観 - Task vs Reactive Extensions
Yoshifumi Kawai
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Vicente García Díaz
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
skilljiolms
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
Kumar
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
zzzubair
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
linked list
linked list linked list
linked list
Mohaimin Rahat
 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
Fazlullah28
 
Merge sort
Merge sortMerge sort
Merge sort
Srikrishnan Suresh
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 
Lockfree Queue
Lockfree QueueLockfree Queue
Lockfree Queue
Kumazaki Hiroki
 

Similar to Class13_Quicksort_Algorithm.pdf (20)

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List RankingParallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SukhrobAtoev2
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
Afaq Mansoor Khan
 
Data Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdfData Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
03_sorting123456789454545454545444543.ppt
03_sorting123456789454545454545444543.ppt03_sorting123456789454545454545444543.ppt
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
03_sorting and it's types with example .ppt
03_sorting and it's types with example .ppt03_sorting and it's types with example .ppt
03_sorting and it's types with example .ppt
vanshii9976
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
Mandeep Singh
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
Krish_ver2
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
Vivek Bhargav
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
Gelo Maribbay
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer ScienceDesign and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
Lecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & AlgorithmsLecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
iqbalphy1
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptxSORT AND SEARCH ARRAY WITH WITH C++.pptx
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List RankingParallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SukhrobAtoev2
 
Data Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdfData Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
Sumita Das
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
jehan1987
 
03_sorting123456789454545454545444543.ppt
03_sorting123456789454545454545444543.ppt03_sorting123456789454545454545444543.ppt
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
03_sorting and it's types with example .ppt
03_sorting and it's types with example .ppt03_sorting and it's types with example .ppt
03_sorting and it's types with example .ppt
vanshii9976
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
Mandeep Singh
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
Krish_ver2
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
Vivek Bhargav
 
Radix and Merge Sort
Radix and Merge SortRadix and Merge Sort
Radix and Merge Sort
Gelo Maribbay
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer ScienceDesign and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
Lecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & AlgorithmsLecture 1 and 2 of Data Structures & Algorithms
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
iqbalphy1
 
Ad

Recently uploaded (20)

Learn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdfLearn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdf
KlaraJericaFrancisco
 
A Sneak Peek into Communication Design by Ayon
A Sneak Peek into Communication Design by AyonA Sneak Peek into Communication Design by Ayon
A Sneak Peek into Communication Design by Ayon
aonbanerjee
 
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
INKPPT
 
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & InsightsDeloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
INKPPT
 
CORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRAND
CORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRANDCORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRAND
CORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRAND
aonbanerjee
 
COLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERS
COLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERSCOLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERS
COLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERS
alainyanda99
 
Morgenbooster - Systems and Transition. 14.05.2025.pdf
Morgenbooster - Systems and Transition. 14.05.2025.pdfMorgenbooster - Systems and Transition. 14.05.2025.pdf
Morgenbooster - Systems and Transition. 14.05.2025.pdf
1508 A/S
 
EY – The Future of Assurance | How Technology is Transforming the Audit
EY – The Future of Assurance | How Technology is Transforming the AuditEY – The Future of Assurance | How Technology is Transforming the Audit
EY – The Future of Assurance | How Technology is Transforming the Audit
INKPPT
 
Beautiful Motherhood (Kal-el's Shows Slideshow)
Beautiful Motherhood (Kal-el's Shows Slideshow)Beautiful Motherhood (Kal-el's Shows Slideshow)
Beautiful Motherhood (Kal-el's Shows Slideshow)
Kal-el's Shows
 
Recycled Materials and Eco-Design for design students.pptx
Recycled Materials and Eco-Design for design students.pptxRecycled Materials and Eco-Design for design students.pptx
Recycled Materials and Eco-Design for design students.pptx
Prof. Hany El-Said
 
Mars.pptx we known about the mars using this ppt
Mars.pptx we known about the mars using this pptMars.pptx we known about the mars using this ppt
Mars.pptx we known about the mars using this ppt
shameer200479
 
Unit 5 visual merchandiseing trend analysis. pdf
Unit 5 visual merchandiseing  trend analysis. pdfUnit 5 visual merchandiseing  trend analysis. pdf
Unit 5 visual merchandiseing trend analysis. pdf
NaziaFarheen13
 
Eeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docx
Eeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docxEeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docx
Eeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docx
PlfiGergely
 
PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...
PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...
PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...
officialpino35
 
Furniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdfFurniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdf
AjayBhonge1
 
Carte d'indentité1 a model for a nes country
Carte d'indentité1 a model for a nes countryCarte d'indentité1 a model for a nes country
Carte d'indentité1 a model for a nes country
stephaniethomas940921
 
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic WorldBCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
INKPPT
 
PWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's Work
PWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's WorkPWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's Work
PWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's Work
INKPPT
 
Using AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map CreationUsing AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map Creation
Kyle Soucy
 
The Role of Structure and Materials in Design.pptx
The Role of Structure and Materials in Design.pptxThe Role of Structure and Materials in Design.pptx
The Role of Structure and Materials in Design.pptx
Prof. Hany El-Said
 
Learn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdfLearn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdf
KlaraJericaFrancisco
 
A Sneak Peek into Communication Design by Ayon
A Sneak Peek into Communication Design by AyonA Sneak Peek into Communication Design by Ayon
A Sneak Peek into Communication Design by Ayon
aonbanerjee
 
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
INKPPT
 
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & InsightsDeloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
INKPPT
 
CORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRAND
CORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRANDCORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRAND
CORPORATE OFFICE INTERNAL BRANDING OF A LEADING INDO-JAPANESE AUTOMOTIVE BRAND
aonbanerjee
 
COLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERS
COLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERSCOLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERS
COLOR THEROY IN GRAPHIC DESIGN HANDBOOK FOR BEGINNERS
alainyanda99
 
Morgenbooster - Systems and Transition. 14.05.2025.pdf
Morgenbooster - Systems and Transition. 14.05.2025.pdfMorgenbooster - Systems and Transition. 14.05.2025.pdf
Morgenbooster - Systems and Transition. 14.05.2025.pdf
1508 A/S
 
EY – The Future of Assurance | How Technology is Transforming the Audit
EY – The Future of Assurance | How Technology is Transforming the AuditEY – The Future of Assurance | How Technology is Transforming the Audit
EY – The Future of Assurance | How Technology is Transforming the Audit
INKPPT
 
Beautiful Motherhood (Kal-el's Shows Slideshow)
Beautiful Motherhood (Kal-el's Shows Slideshow)Beautiful Motherhood (Kal-el's Shows Slideshow)
Beautiful Motherhood (Kal-el's Shows Slideshow)
Kal-el's Shows
 
Recycled Materials and Eco-Design for design students.pptx
Recycled Materials and Eco-Design for design students.pptxRecycled Materials and Eco-Design for design students.pptx
Recycled Materials and Eco-Design for design students.pptx
Prof. Hany El-Said
 
Mars.pptx we known about the mars using this ppt
Mars.pptx we known about the mars using this pptMars.pptx we known about the mars using this ppt
Mars.pptx we known about the mars using this ppt
shameer200479
 
Unit 5 visual merchandiseing trend analysis. pdf
Unit 5 visual merchandiseing  trend analysis. pdfUnit 5 visual merchandiseing  trend analysis. pdf
Unit 5 visual merchandiseing trend analysis. pdf
NaziaFarheen13
 
Eeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docx
Eeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docxEeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docx
Eeeeeeezfhedjdjdjrjrnrnrkddjdjdjdrnrnnn.docx
PlfiGergely
 
PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...
PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...
PINOQQ SITUS MUDAH MERAIH KEMENANGAN SEGERA DAFTAR DAN RAIH KEMENANGAN NYA HA...
officialpino35
 
Furniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdfFurniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdf
AjayBhonge1
 
Carte d'indentité1 a model for a nes country
Carte d'indentité1 a model for a nes countryCarte d'indentité1 a model for a nes country
Carte d'indentité1 a model for a nes country
stephaniethomas940921
 
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic WorldBCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
INKPPT
 
PWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's Work
PWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's WorkPWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's Work
PWC – Workforce of the Future 2030 | Four Scenarios Shaping Tomorrow's Work
INKPPT
 
Using AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map CreationUsing AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map Creation
Kyle Soucy
 
The Role of Structure and Materials in Design.pptx
The Role of Structure and Materials in Design.pptxThe Role of Structure and Materials in Design.pptx
The Role of Structure and Materials in Design.pptx
Prof. Hany El-Said
 
Ad

Class13_Quicksort_Algorithm.pdf

  • 1. Quick Sort Algorithm Dr J P Patra Associate Professor Computer Science & Engineering UTD, CSVTU, Bhilai 1
  • 2. Outline: • What is Quick Sort in Data Structure? • When to Use Quick Sort? • Explanation of Quick Sort Algorithm • Example of Quick Sort • Time Complexity of Quick Sort • Advantages and Disadvantages of Quick Sort • Applications of Quick Sort 2
  • 3. What is Quick Sort in Data Structure? •Quick Sort is a sorting algorithm based on the Divide and Conquer approach that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. 3
  • 4. What is Divide and Conquer approach? 4 This technique can be divided into the following 3 parts: • Divide: This involves dividing the problem into smaller sub-problems. • Conquer: Solve sub-problems by calling recursively until solved. • Combine: Combine the sub-problems to get the final solution of the whole problem.
  • 5. 5 Here is the three-steps divide-and-conquer process for sorting a typical array A[p….r]. Divide: Partition (rearrange) the array A[p.....r] into two (possibly empty) subarrays A[p.....q - 1] and A[q+1.....r] such that each element of A[p.....q - 1] is less than to A[q], and each element of A[q+1......r] is greater than or equal to A[q] Compute the index q as part of this partitioning procedure.
  • 6. 6 Conquer: Sort the two subarrays A[p....q - 1] and A[q+1......r] by recursive calls to quicksort. Combine: Since the subarrays are sorted in place, now work is needed to combine them So we can define that the entire array A[p....r] is now sorted.
  • 8. Quick Sort Partition Algorithm 8
  • 9. 9 • Here the value of p=1 and r=8 • As per the Partition Algorithm the value of x=4 (As A[r]=A[8]=4) • The value of i=0 (i.e p-1= 1-1=0) • As per step no. 3 for loop will start from (p=1) and it will continue up to (r-1=8-1=7). Example : A=[2, 8, 7, 1, 3, 5, 6, 4]
  • 10. 10 • A=[2, 8, 7, 1, 3, 5, 6, 4] • Here the value of x= 4, r=8, i=0, j=1, A[1]=2 do if (2≤ 4) Ture • Then the value of iwill be 1 (i. e i=1) and we have to swap :A[1] ↔ A[1] • Output : Process for j=1
  • 11. 11 • A=[2, 8, 7, 1, 3, 5, 6, 4] • Here the value of x= 4, r=8, i=1, j=2, A[2]=8 do if (8≤ 4) False • Then we will skip the steps 5 and 6 and the output after processing j=2 will be define as • Output : Process for j=2
  • 12. 12 • A=[2, 8, 7, 1, 3, 5, 6, 4] • Here the value of x= 4, r=8, i=1, j=3, A[3]=7 do if (7≤ 4) False • Then we will skip the steps 5 and 6 and the output after processing j=3 will be define as • Output : Process for j=3
  • 13. 13 • A=[2, 8, 7, 1, 3, 5, 6, 4] • Here the value of x= 4, r=8, i=1, j=4, A[4]=1 do if (1≤ 4) True • Then the value of i will be 2 (i. e i=2) and we have to swap :A[2] ↔ A[4] • Output : Process for j=4
  • 14. 14 • A=[2, 1, 7, 8, 3, 5, 6, 4] • Here the value of x= 4, r=8, i=2, j=5, A[5]=3 do if (3≤ 4) True • Then the value of i will be 3 (i. e i =3) and we have to swap :A[3] ↔ A[5] • Output : Process for j=5
  • 15. 15 • A=[2, 1, 3, 8, 7, 5, 6, 4] • Here the value of x= 4, r=8, i=3, j=6, A[6]=5 do if (5≤ 4) False • Then we will skip the steps 5 and 6 and the output after processing j=6 will be define as • Output : Process for j=6
  • 16. 16 • A=[2, 1, 3, 8, 7, 5, 6, 4] • Here the value of x= 4, r=8, i=3, j=7, A[7]=6 do if (6 ≤ 4) False • Then we will skip the steps 5 and 6 and the output after processing j=7 will be define as • Output : Process for j=7
  • 17. 17 • A=[2, 1, 3, 8, 7, 5, 6, 4] or Here the value of x= 4, r=8, i=3, j=7, A[7]=6 Now using step 7 we have to exchange A[i+1] ↔ A[r] i.e A[4] ↔ A[8] Output: Step 8 will return value 4 Process Step 7 and 8
  • 18. • Using Quick Sort Partition algorithm return the value 4 and that value will be assign to variable q. • Now we divide the array in two sub arrays A[1…3] and A[5….8] and the position of the Pivot element is 4. 18 Sub Array2 A[5….8] Sub Array1 A[1….3] Position of Pivot element
  • 19. 19 • After getting the value of q we will call Quicksort(A, p, q-1) and Quicksort(A, q+1, r) • This process will continue until we satisfy the condition.
  • 20. Best-case running time 20 • In the most even possible split, PARTITION produces two subproblems, each of size no more than (n/2), since one is of size ⌊n/2⌋ and one of size ⌊(n/2)-1⌋.In this case, quicksort runs much faster. • The recurrence for the running time is then: T(n) =2T(n/2) + (n)
  • 21. 21 Here T(n) =2T(n/2) + (n) So we can solve the above recurrence equation using Master Method Master Method Standard equation is T(n) =aT(n/b) + f(n) If we compare the two equations we will obtained the value of a=2, b=2 and f(n)=n
  • 22. 22 Now we have to calculate n loga b => n log2 2 => n If we compare the value of f(n) and n loga b both values are equal. So, T(n) =0(f(n).logn) =>T(n)=0(nlogn) Best Case running time of Quick sort is 0(nlogn)
  • 23. Worst-case running time 23 • The efficiency of the Quicksort algorithm very much depends on the selection of the pivot element. • Let’s assume the input of the Quicksort is a sorted array and we choose the leftmost element as a pivot element. • In this case, we’ll have two extremely unbalanced arrays. One array will have one element and the other one will have (N - 1) elements.
  • 24. Worst-case running time 24 • Similarly, when the given input array is sorted reversely and we choose the rightmost element as the pivot element, the worst case occurs. Again, in this case, the pivot elements will split the input array into two unbalanced arrays.
  • 25. Worst Case Time Complexity Analysis 25
  • 26. Worst Case Time Complexity Analysis 26 • All the numbers in the box denote the size of the arrays or the subarrays. • Let’s consider an input array of size N. The first partition call takes N times to perform the partition step on the input array. • Each partition step is invoked recursively from the previous one. Given that, we can take the complexity of each partition call and sum them up to get our total complexity of the Quicksort algorithm
  • 27. 27 Alternatively, we can create a recurrence relation for computing it. Or
  • 28. 28 • In the worst case, after the first partition, one array will have 1 element and the other one will have (N-1) elements. • Let’s say T(N) denotes the time complexity to sort N elements in the worst case: • T(N) = Time needed to partition N elements + Time needed to sort (N - 1) elements recursively = N + T(N - 1) • Again for the base case when N = 0 and 1, we don’t need to sort anything. Hence, the sorting time is 0 and T(0)= T(1) = 0
  • 29. 29 Now, we’re ready to solve the recurrence relation T(N) = N + T(N- 1), T(N - 1) = (N - 1) + T(N- 2), T(N - 2) = (N - 2) + T(N- 3), T(N - 3) = (N - 3) + T(N- 4), ...... ...... ...... T(3) = 3 + T(2), T(2) = 2 + T(1), T(1) = 0
  • 30. 30 As a result, T(N) = N + (N-1) + (N-2) ... + 3 + 2 = [ 𝑵(𝑵+𝟏) 𝟐 - 1] =O(N2)
  • 31. Advantages of Quick Sort 31 • Fast and efficient : Quick sort is the most favored users’ choice to perform sorting functions quickly and efficiently. It allows users to achieve the same result much quicker than other sorting algorithms • Cache friendly :The most talked-about feature of quicksort is its in-place sorting. This means, at the time of sorting, the algorithm doesn’t need additional storage space. Thus, the sorted list occupies the same storage as the unsorted list, and the sorting takes place in the given area
  • 32. Advantages of Quick Sort 32 • Good for parallelization: Quick sort can be parallelized easily, taking advantage of multiple processors or computing resources. By splitting the input array into sub-arrays and sorting them concurrently, it can achieve faster sorting times on systems with parallel processing capabilities. • Simple implementation: Quick sort's algorithmic logic is relatively straightforward, making it easier to understand and implement compared to some other complex sorting algorithms
  • 33. Advantages of Quick Sort 33 • Time complexity: Time complexity is the time taken by the algorithm to run until its completion. Compared to other sorting algorithms, it can be said that the time complexity of quick sort is the best • Space complexity: Quick sort has a space complexity of O(logn), making it a suitable algorithm when you have restrictions in space availability
  • 34. Disadvantages of Quick Sort 34 • Unstable: Quick sort is undoubtedly a fast and efficient sorting algorithm, but when it comes to stability, you might want to reconsider your options. This sorting algorithm is regarded unstable as it does not retain the original order of the key-value pair. • Worst-case time complexity: This is actually a drawback of quick sort. It usually occurs when the element selected as the pivot is the largest or smallest element, or when all the elements are the same. These worst cases drastically affect the performance of the quick sort.
  • 35. Disadvantages of Quick Sort 35 • Dependency on Pivot Selection: The choice of pivot significantly affects the performance of quick sort. If a poorly chosen pivot divides the array into highly imbalanced partitions, the algorithm's efficiency may degrade. This issue is particularly prominent when dealing with already sorted or nearly sorted arrays. • Recursive Overhead: Quick sort heavily relies on recursion to divide the array into subarrays. Recursive function calls consume additional memory and can lead to stack overflow errors when dealing with large data sets. This makes quick sort less suitable for sorting extremely large arrays.
  • 36. Disadvantages of Quick Sort 36 • Inefficient for Small Data Sets: Quick sort has additional overhead in comparison to some simpler sorting algorithms, especially when dealing with small data sets. The recursive nature of quick sort and the constant splitting of the array can be inefficient for sorting small arrays or arrays with a small number of unique elements. • Not Adaptive: Quick sort's performance is not adaptive to the initial order of the elements. It does not take advantage of any pre-existing order or partially sorted input. This means that even if a portion of the array is already sorted, the quick sort will still perform partitioning operations on the entire array.
  • 37. Applications of Quick Sort 37 • Commercial Computing is used in various government and private organizations for the purpose of sorting various data like sorting files by name/date/price, sorting of students by their roll no., sorting of account profile by given id, etc. • The sorting algorithm is used for information searching and as Quicksort is the fastest algorithm so it is widely used as a better way of searching. • It is used everywhere where a stable sort is not needed.
  • 38. Applications of Quick Sort 38 • Quicksort is a cache-friendly algorithm as it has a good locality of reference when used for arrays. • It is tail -recursive and hence all the call optimization can be done. • It is used to implement primitive type methods. • It is used in operational research and event-driven simulation.
  • 39. 39 Conclusion: Overall, quick sort is a powerful data structure algorithm with advantages such as efficiency and versatility. It is an essential tool for developers to have in their belt when dealing with large amounts of data. Quicksort is an efficient, unstable sorting algorithm with time complexity of O(n log n) in the best and average case and O(n²) in the worst case.
  • 40. 40
  翻译: