SlideShare a Scribd company logo
1 of 13
Presented by:-AREEN GAUR(2020PCP5516)
M .Tech Ist year(CSE,MNIT Jaipur)
Submitted To: Prof. Vijay Laxmi
(CSE dept. MNIT Jaipur)
Points Covered:
 Description of Quick sort
 Algorithm of Quick sort
 Example
 Performance and recurrence relation
 Best case partitioning and time complexity with derivation
 Worst case partitioning and time complexity with derivation
 Balanced partitioning and general equation for best case
 Input for best and worst case
2
Description
 This sorting algorithm uses Divide and Conquer approach , the three-step
divide-and-conquer process for sorting a array A[p….r]:
 DIVIDE-Partition the array A[p…r] into two subarrays A[p…q-1] and
A[q+1..r] such that each element of left subarray is less than or equal to A[q]
and each element of right subarray is greater than or equal to A[q] , for this
partitioning we need to compute the index q.
 Conquer-Sort the two subarrays by recursive calls to quick sort.
 Combine-combine the two sorted subarrays
 Quick sort is inplace and not stable sorting algo.
3
Procedure
Quicksort(A, p, r)
1. if (p < r)
2. q=partition(A, p, r)
3. Quicksort(A, p, q-1)
4. Quicksort(A, q+1, r)///initial call is Quicksort(A,0,n-1) where n is no. of elements in array
partition(A, p, r)
1. x=A[r]
2. i = p-1
3. for j =p to r-1
4. if (A[j]≤x)
5. i = i+1
6. exchange A[i] with A[j]
7. exchange A[i+1] with A[r]
8. return i+1
4
Example
2 8 7 1 3 5 6 4
p , i=0 j=1 2 3 4 5 6 r=7
5
2 8 7 1 3 5 6 4
p , j=0 1 2 3 4 5 6 r=7
2 8 7 1 3 5 6 4
p , i=0 1 j=2 3 4 5 6 r=7
2 8 7 1 3 5 6 4
p , i=0 1 2 j=3 4 5 6 r=7
2 1 7 8 3 5 6 4
0 i=1 2 3 j=4 5 6 r=7
As i=p-1 so i=-1 x=4
2 1 3 8 7 5 6 4
p=0 1 i=2 3 4 5 6 7
2 1 3 4(pivot) 7 5 6 8
p=0 1 i=2 3 4 5 6 7
2 1 3 8 7 5 6 4
p=0 1 i=2 3 4 j=5 6 r=7
2 1 3 8 7 5 6 4
p=0 1 i=2 3 4 5 j=6 7
6
Left sub array right sub array
Performance and Recurrence Relation
1. The running time depends on whether the partition is balanced or unbalanced.
2. If the partition is balanced the algo runs asymptotically fast with time
complexity q(n logn).
3. If the partition is unbalanced , it can run asymptotically slow with time
complexity q(n2).
7
Best-case partitioning
This occurs when array is divided into two equal subarrays, each of size no more
than n/2, since one of size floor(n/2) and one of size ceil(n/2)-1.
The recurrence relation is :
T(n)=2T(n/2)+q(n)
Using master’s theorem we know time complexity will be q(n logn).
Proof: In general aT(n/b)+f(n) where a ≥ 1 and b>1 and f(n) is positive function
On comparing f(n) and nloga/logb
We get f(n) and nloga/logb asymptotically equal and by case 3 of master’s theorem
we know if f(n)=q(n(loga/logb) . (logn)k ) where k ≥ 0 then
T(n)=q(n(loga/logb).(logn)k+1 )
Hence time complexity in best case is q(n logn).
8
Worst Case partitioning
The worst case behaviour for quick sort occurs when the partitioning routine
produces one subproblem with n-1 elements and one with zero elements.
The recurrence relation is :
T(n)=T(n-1)+T(0)+ q(n)
T(n)=T(n-1)+ q(n)///T(1)=1 base case
Proof: By substitution method
T(n)=T(n-2)+n-1+n
T(n)=T(n-3)+n-2+n-1+n , if we do this n-1 times then
T(n)=T(n-(n-1))+(n-(n-2))+(n-(n-3))+………….n-1+n
T(n)=T(1)+2+3+4+……………………+n-1+n//////this is an ap series
9
Worst Case partitioning(continued)
T(n)=(n(n+1))/2 [sum of n natural numbers]
T(n) is approximately equal to q(n2).
Therefore the worst case running time of quicksort is q(n2).
Moreover the q(n2) running time occurs when the input array is already
completely sorted.
10
Balanced Partitioning
The average case running time of quicksort is much closer to the best case.
T(n) = n + T(an) + T((1-a)n)
where 0<a <1
Therefore the running time of quicksort in balanced partitioning will be q(nlogn).
11
Input for best case :- any unsorted array
example:
5 6 7 8 9 89 90 2
12
Input for worst case :- any sorted array
example:
5 5 5 5 5 5 5 5
5 6 7 8 9 10 11 20
THANK YOU
13

More Related Content

What's hot

Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applicationsSaqib Saeed
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureTushar Gonawala
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 

What's hot (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Stack
StackStack
Stack
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
stack & queue
stack & queuestack & queue
stack & queue
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Linked List
Linked ListLinked List
Linked List
 
Merge sort
Merge sortMerge sort
Merge sort
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Stack
StackStack
Stack
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Similar to Quick sort

CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxkassahungebrie
 
Top school in noida
Top school in noidaTop school in noida
Top school in noidaEdhole.com
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmMahbubur Rahman
 

Similar to Quick sort (20)

CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Sorting
SortingSorting
Sorting
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
 
Sorting
SortingSorting
Sorting
 
Merge sort
Merge sortMerge sort
Merge sort
 
Lec10
Lec10Lec10
Lec10
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 

Recently uploaded

UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

Quick sort

  • 1. Presented by:-AREEN GAUR(2020PCP5516) M .Tech Ist year(CSE,MNIT Jaipur) Submitted To: Prof. Vijay Laxmi (CSE dept. MNIT Jaipur)
  • 2. Points Covered:  Description of Quick sort  Algorithm of Quick sort  Example  Performance and recurrence relation  Best case partitioning and time complexity with derivation  Worst case partitioning and time complexity with derivation  Balanced partitioning and general equation for best case  Input for best and worst case 2
  • 3. Description  This sorting algorithm uses Divide and Conquer approach , the three-step divide-and-conquer process for sorting a array A[p….r]:  DIVIDE-Partition the array A[p…r] into two subarrays A[p…q-1] and A[q+1..r] such that each element of left subarray is less than or equal to A[q] and each element of right subarray is greater than or equal to A[q] , for this partitioning we need to compute the index q.  Conquer-Sort the two subarrays by recursive calls to quick sort.  Combine-combine the two sorted subarrays  Quick sort is inplace and not stable sorting algo. 3
  • 4. Procedure Quicksort(A, p, r) 1. if (p < r) 2. q=partition(A, p, r) 3. Quicksort(A, p, q-1) 4. Quicksort(A, q+1, r)///initial call is Quicksort(A,0,n-1) where n is no. of elements in array partition(A, p, r) 1. x=A[r] 2. i = p-1 3. for j =p to r-1 4. if (A[j]≤x) 5. i = i+1 6. exchange A[i] with A[j] 7. exchange A[i+1] with A[r] 8. return i+1 4
  • 5. Example 2 8 7 1 3 5 6 4 p , i=0 j=1 2 3 4 5 6 r=7 5 2 8 7 1 3 5 6 4 p , j=0 1 2 3 4 5 6 r=7 2 8 7 1 3 5 6 4 p , i=0 1 j=2 3 4 5 6 r=7 2 8 7 1 3 5 6 4 p , i=0 1 2 j=3 4 5 6 r=7 2 1 7 8 3 5 6 4 0 i=1 2 3 j=4 5 6 r=7 As i=p-1 so i=-1 x=4
  • 6. 2 1 3 8 7 5 6 4 p=0 1 i=2 3 4 5 6 7 2 1 3 4(pivot) 7 5 6 8 p=0 1 i=2 3 4 5 6 7 2 1 3 8 7 5 6 4 p=0 1 i=2 3 4 j=5 6 r=7 2 1 3 8 7 5 6 4 p=0 1 i=2 3 4 5 j=6 7 6 Left sub array right sub array
  • 7. Performance and Recurrence Relation 1. The running time depends on whether the partition is balanced or unbalanced. 2. If the partition is balanced the algo runs asymptotically fast with time complexity q(n logn). 3. If the partition is unbalanced , it can run asymptotically slow with time complexity q(n2). 7
  • 8. Best-case partitioning This occurs when array is divided into two equal subarrays, each of size no more than n/2, since one of size floor(n/2) and one of size ceil(n/2)-1. The recurrence relation is : T(n)=2T(n/2)+q(n) Using master’s theorem we know time complexity will be q(n logn). Proof: In general aT(n/b)+f(n) where a ≥ 1 and b>1 and f(n) is positive function On comparing f(n) and nloga/logb We get f(n) and nloga/logb asymptotically equal and by case 3 of master’s theorem we know if f(n)=q(n(loga/logb) . (logn)k ) where k ≥ 0 then T(n)=q(n(loga/logb).(logn)k+1 ) Hence time complexity in best case is q(n logn). 8
  • 9. Worst Case partitioning The worst case behaviour for quick sort occurs when the partitioning routine produces one subproblem with n-1 elements and one with zero elements. The recurrence relation is : T(n)=T(n-1)+T(0)+ q(n) T(n)=T(n-1)+ q(n)///T(1)=1 base case Proof: By substitution method T(n)=T(n-2)+n-1+n T(n)=T(n-3)+n-2+n-1+n , if we do this n-1 times then T(n)=T(n-(n-1))+(n-(n-2))+(n-(n-3))+………….n-1+n T(n)=T(1)+2+3+4+……………………+n-1+n//////this is an ap series 9
  • 10. Worst Case partitioning(continued) T(n)=(n(n+1))/2 [sum of n natural numbers] T(n) is approximately equal to q(n2). Therefore the worst case running time of quicksort is q(n2). Moreover the q(n2) running time occurs when the input array is already completely sorted. 10
  • 11. Balanced Partitioning The average case running time of quicksort is much closer to the best case. T(n) = n + T(an) + T((1-a)n) where 0<a <1 Therefore the running time of quicksort in balanced partitioning will be q(nlogn). 11
  • 12. Input for best case :- any unsorted array example: 5 6 7 8 9 89 90 2 12 Input for worst case :- any sorted array example: 5 5 5 5 5 5 5 5 5 6 7 8 9 10 11 20