SlideShare a Scribd company logo
Quick Sort
Using
Divide And Conquer
Algorithm
Name : Mrunal Sanjay Patil
Roll No : FYMCA2064
What is Quick Sort ?
 Ranking manner Quick Sort is the most widely used of all the other sorting
algorithms because they are faster and consume less memory.
 Fastest known sorting algorithm in practice
 Quick sort is a highly efficient sorting algorithm and is based on partitioning of
array of data into smaller arrays.
 A large array is partitioned into two arrays one of which holds
values smaller than the specified value, say pivot, based on which the
partition is made and another array holds values greater than
the pivot value.
Divide and Conquer
Divide :
Divide the problem into a number of sub-problems
Similar sub-problems of smaller size
Conquer :
Conquer the sub-problems Solve the sub-problems
recursively.
Sub-problem size small enough  solve the problems in
straightforward manner
Combine :
Combine the solutions of the sub-problems
Obtain the solution for the original problem
How we use divide and conquer algorithm for
Quicksort
 Sort an array A[p…r]
 Divide
Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of
A[p..q] is smaller than or equal to each element in A[q+1..r]
Need to find index q to partition the array
≤
 Conquer
Recursively sort A[p..q] and A[q+1..r] using Quicksort.
First sort A[p..q] and then sort A[q+1..r] individually.
 Combine
Trivial: the arrays are sorted in place
No additional work is required to combine them.
The entire array is now sorted.
≤
Basic idea (The first step towards sorting the
elements)
 Pick some number as pivot from the array
 Move all numbers less than pivot to the beginning of the array
 Move all numbers greater than (or equal to) pivot to the end of the array
 Quicksort the numbers less than pivot
 Quicksort the numbers greater than or equal to pivot
pivot
pivot
numbers less than pivot numbers greater than or equal to
Example
41 62 13 84 35 96
Pivot = 41 i j
35
84
13
62
41
If i > j then swap i and j Else j--
96
35
84
13
62
41
i j
41 > 35 = true
35
84
13
62
41
i j
swap 41 and 35
96
96
Unsorted array of 6 elements
41 > 96 = false then j--
i j
41
84
13
62
35 96
Pivot = j = 41 do i++
i j
62 > 41 = true
j
41
84
13
i
62
swap 41 and 62
j
62
84
13
i
41
Pivot = i = 41 do j--
35 35 96
41
84
13
62 84
62
35 96
96
41 > 84 = false do j-- 41 > 13 = True
j
13
i
41 62
84
35
j
84
13
i
41 62
35 96
swap 41 and 13 Pivot = j = 41 do
96
j
41
i
13 62
84
35 96
j
13
i
41 62
84
35 96
i = j
41
13
35
a[0] a[1] a[2] a[3] a[4] a[5]
numbers less than
pivot
numbers greater than or
equal to pivot
Now we sort numbers less than pivot
13
35
41 62
84 96
62
84 96
j
41
i
13 62
84
35 96
13 41 62
84 96
Pivot = 35
j
i
35
35 > 13 = true
Left side Array sorted
Now, we sort numbers greater than
35 41 62
84
13 96
13 41 62
84
j
i
35 96
swap 35 and 13
35 41
13
Pivot = 84
84 > 62 = True
35 41
13
swap 84 and 62
Both Sub-Arrays are
sorted
62
35 41
13
j
i
84
84 > 96 = false
do j--
96 62
j
i
84 96
62
84 96
35 41 84
13 62 96
 Algorithm
 Program
Quick Sort Space Complexity
 Basic approach : O(n)
 Modified approach : O(logn)
Quick Sort Time Complexity
 Best Time Complexity : O(nlogn)
 Average Time Complexity : O(nlogn)
 Worst Time Complexity : O(n^2)
The amount of computer time needs to run to completion.
The amount of computer space (memory) needs to
run to completion.
Divide-and-conquer

More Related Content

PPT
Data Structures- Part4 basic sorting algorithms
PPTX
10 merge sort
PPTX
Insertion Sort, Quick Sort And Their complexity
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPTX
Advanced Sorting Algorithms
PPT
Systems of linear equations; matrices
PPTX
Solving Systems of Equations and Inequalities by Graphing
Data Structures- Part4 basic sorting algorithms
10 merge sort
Insertion Sort, Quick Sort And Their complexity
358 33 powerpoint-slides_14-sorting_chapter-14
Advanced Sorting Algorithms
Systems of linear equations; matrices
Solving Systems of Equations and Inequalities by Graphing

What's hot (20)

PPT
Data Structure Sorting
PPT
Systems of linear equations and augmented matrices
PPT
Gauss Jordan
PDF
Sorting
PPT
(Data Structure) Chapter11 searching & sorting
PPTX
Quick sort
PDF
Lecture 6.1 6.2 bt
PPTX
System of linear equations
PDF
Ma3bfet par 10.6 5 aug 2014
PPTX
Sorting algorithms
PPT
3.8 quick sort
PPTX
Crout s method for solving system of linear equations
PPT
Quicksort
PDF
9.4 Cramer's Rule
DOCX
BSC_COMPUTER _SCIENCE_UNIT-4_DISCRETE MATHEMATICS
PPTX
Merge Sort
PPTX
Gauss jordan
PPTX
Sorting algorithms
PPT
Mergesort
PPTX
Matrix by suman sir
Data Structure Sorting
Systems of linear equations and augmented matrices
Gauss Jordan
Sorting
(Data Structure) Chapter11 searching & sorting
Quick sort
Lecture 6.1 6.2 bt
System of linear equations
Ma3bfet par 10.6 5 aug 2014
Sorting algorithms
3.8 quick sort
Crout s method for solving system of linear equations
Quicksort
9.4 Cramer's Rule
BSC_COMPUTER _SCIENCE_UNIT-4_DISCRETE MATHEMATICS
Merge Sort
Gauss jordan
Sorting algorithms
Mergesort
Matrix by suman sir
Ad

Similar to Divide-and-conquer (20)

PPTX
Divide and conquer - Quick sort
PPTX
Unit 2 - Quick Sort.pptx
PPTX
Quicksort algorithm and implantation process
PPTX
Quick sort
PPTX
Quick sort
DOCX
Quick sort
PPTX
Quick Sort algorithm for sorting an array of elements
PDF
Quick sort
PPTX
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
PPTX
09 QUICK SORT Design and Analysis of algorithms
PDF
Quick sort
PPTX
Quick-Sort Algorithm and pivot selection
PPTX
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
PPTX
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
PPTX
Quick sort by Sania Nisar
PDF
Class13_Quicksort_Algorithm.pdf
PPT
quick_sort_with_explanationandImplmentation.ppt
PPT
quicksort (1).ppt
PPTX
jyothi(22D21A0547)DAA.pptx in DAA computer
PPTX
QUICK SORT_DAA.pptx
Divide and conquer - Quick sort
Unit 2 - Quick Sort.pptx
Quicksort algorithm and implantation process
Quick sort
Quick sort
Quick sort
Quick Sort algorithm for sorting an array of elements
Quick sort
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
09 QUICK SORT Design and Analysis of algorithms
Quick sort
Quick-Sort Algorithm and pivot selection
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
Quick sort by Sania Nisar
Class13_Quicksort_Algorithm.pdf
quick_sort_with_explanationandImplmentation.ppt
quicksort (1).ppt
jyothi(22D21A0547)DAA.pptx in DAA computer
QUICK SORT_DAA.pptx
Ad

Recently uploaded (20)

PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Current and future trends in Computer Vision.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
573137875-Attendance-Management-System-original
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Sustainable Sites - Green Building Construction
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Well-logging-methods_new................
PDF
737-MAX_SRG.pdf student reference guides
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
introduction to datamining and warehousing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Fundamentals of Mechanical Engineering.pptx
Geodesy 1.pptx...............................................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Current and future trends in Computer Vision.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
573137875-Attendance-Management-System-original
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Sustainable Sites - Green Building Construction
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Categorization of Factors Affecting Classification Algorithms Selection
Well-logging-methods_new................
737-MAX_SRG.pdf student reference guides
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
introduction to datamining and warehousing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

Divide-and-conquer

  • 1. Quick Sort Using Divide And Conquer Algorithm Name : Mrunal Sanjay Patil Roll No : FYMCA2064
  • 2. What is Quick Sort ?  Ranking manner Quick Sort is the most widely used of all the other sorting algorithms because they are faster and consume less memory.  Fastest known sorting algorithm in practice  Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays.  A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.
  • 3. Divide and Conquer Divide : Divide the problem into a number of sub-problems Similar sub-problems of smaller size Conquer : Conquer the sub-problems Solve the sub-problems recursively. Sub-problem size small enough  solve the problems in straightforward manner Combine : Combine the solutions of the sub-problems Obtain the solution for the original problem
  • 4. How we use divide and conquer algorithm for Quicksort  Sort an array A[p…r]  Divide Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r] Need to find index q to partition the array ≤
  • 5.  Conquer Recursively sort A[p..q] and A[q+1..r] using Quicksort. First sort A[p..q] and then sort A[q+1..r] individually.  Combine Trivial: the arrays are sorted in place No additional work is required to combine them. The entire array is now sorted. ≤
  • 6. Basic idea (The first step towards sorting the elements)  Pick some number as pivot from the array  Move all numbers less than pivot to the beginning of the array  Move all numbers greater than (or equal to) pivot to the end of the array  Quicksort the numbers less than pivot  Quicksort the numbers greater than or equal to pivot pivot pivot numbers less than pivot numbers greater than or equal to
  • 7. Example 41 62 13 84 35 96 Pivot = 41 i j 35 84 13 62 41 If i > j then swap i and j Else j-- 96 35 84 13 62 41 i j 41 > 35 = true 35 84 13 62 41 i j swap 41 and 35 96 96 Unsorted array of 6 elements 41 > 96 = false then j--
  • 8. i j 41 84 13 62 35 96 Pivot = j = 41 do i++ i j 62 > 41 = true j 41 84 13 i 62 swap 41 and 62 j 62 84 13 i 41 Pivot = i = 41 do j-- 35 35 96 41 84 13 62 84 62 35 96 96
  • 9. 41 > 84 = false do j-- 41 > 13 = True j 13 i 41 62 84 35 j 84 13 i 41 62 35 96 swap 41 and 13 Pivot = j = 41 do 96 j 41 i 13 62 84 35 96 j 13 i 41 62 84 35 96
  • 10. i = j 41 13 35 a[0] a[1] a[2] a[3] a[4] a[5] numbers less than pivot numbers greater than or equal to pivot Now we sort numbers less than pivot 13 35 41 62 84 96 62 84 96 j 41 i 13 62 84 35 96
  • 11. 13 41 62 84 96 Pivot = 35 j i 35 35 > 13 = true Left side Array sorted Now, we sort numbers greater than 35 41 62 84 13 96 13 41 62 84 j i 35 96 swap 35 and 13
  • 12. 35 41 13 Pivot = 84 84 > 62 = True 35 41 13 swap 84 and 62 Both Sub-Arrays are sorted 62 35 41 13 j i 84 84 > 96 = false do j-- 96 62 j i 84 96 62 84 96 35 41 84 13 62 96
  • 15. Quick Sort Space Complexity  Basic approach : O(n)  Modified approach : O(logn) Quick Sort Time Complexity  Best Time Complexity : O(nlogn)  Average Time Complexity : O(nlogn)  Worst Time Complexity : O(n^2) The amount of computer time needs to run to completion. The amount of computer space (memory) needs to run to completion.