SlideShare a Scribd company logo
1 of 74
Download to read offline
QUICK SORT
This sorting algorithm uses the idea of divide and
conquer.
It finds the element called pivot which divides
the array into two halves in such a way that
elements in the left half are smaller than pivot
and elements in the right half are greater than
pivot.
QUICK SORT
Three steps
Find pivot that divides the array into two halves.
Quick sort the left half.
Quick sort the right half.
QUICK SORT
Example
Consider an array having 6 elements
5 2 6 1 3 4
Arrange the elements in ascending order using
quick sort algorithm
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
Pivot
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
Pivot
Initially pointing to the
First element
0 1 2 3 4 5
5 2 6 1 3 4
Array index
Array element
This is our unsorted array
Left
Initially pointing to the
First element of the array
Right
Initially pointing to the
Last element of the array
Pivot
Initially pointing to the
First element
We will quick sort this array
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
All element to the RIGHT of pivot be GREATER than pivot.
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Remember this rule:
All element to the RIGHT of pivot be GREATER than pivot.
All element to the LEFT of pivot be SMALLER than pivot.
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivot
is pointing at
left
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivot
is pointing at
left
So we will start
from right
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
As the pivot
is pointing at
left
So we will start
from rightAnd move towards left
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
NO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
So we swap pivot and right
NO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Pivot
Pivot = 5
Right =4
So we swap pivot and right
NO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Right =4
So we swap pivot and right
NO
Is Pivot < Right
( 5 < 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot < Left
Now move pivot to right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot < Left
NO
So we swap pivot to the right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivot
is pointing at
right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivot
is pointing at
right
So we will start
from left
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Now the pivot
is pointing at
right
So we will start
from left
And move towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot > Left
( 5 > 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot > Left
YES
( 5 > 4)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =4
Is Pivot > Left
YES
( 5 > 4)
So we move left one position
towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > Left
( 5 > 2)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > Left
( 5 > 2)
YES
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > Left
( 5 > 2)
YES
So we move left one position
towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > Left
( 5 > 6)
NO
So we swap pivot and left
And move the pivot to left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Now the pivot is
pointing at left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Now the pivot is
pointing at left So we will start
from right
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Now the pivot is
pointing at left So we will start
from right
And move towards left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Right =6
Is Pivot < Right
( 5 < 6)
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Right =6
Is Pivot < Right
( 5 < 6)
YES
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Right =6
Is Pivot < Right
( 5 < 6)
YES
So we move right one position
towards left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Right =3
Is Pivot < Right
( 5 < 3)
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Right =3
Is Pivot < Right
( 5 < 3)
NO
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot
Pivot = 5
Right =3
Is Pivot < Right
( 5 < 3)
NO
So we swap pivot and right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5
Right =3
Is Pivot < Right
( 5 < 3)
NO
So we swap pivot and right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5
Right =3
Is Pivot > Right
( 5 > 3)
NO
So we swap pivot and right
And move the pivot to right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5
Left =3
Is Pivot > Left
( 5 > 3)
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5
Left =3YES
Is Pivot > Left
( 5 > 3)
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5
Left =3
Is Pivot > Left
( 5 > 3)
So we move left one position
towards right
YES
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 5
Left = 1
Is Pivot > Left
( 5 > 1)
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
So pivot has divided the array into
two sub array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
Left sub array Right sub array
So pivot has divided the array into
two sub array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Now both left and right are pointing at the same element of the array
This time 5 is the pivot and it is at the
sorted position
4
5
Elements left of pivot are smaller
Elements right of
pivot are greater
Left sub array Right sub array
So pivot has divided the array into
two sub array We will now quick sort
the left sub array
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
0 1 2 3 4 5
4 2 3 1 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
Left Right
0 1 2 3 4 5
4 2 3 1 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Left Right
0 1 2 3 4 5
1 2 3 4 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Left Right
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Now move the pivot to right
0 1 2 3 4 5
1 2 3 4 5 6
Pivot
Pivot = 4
Left =1
Is Pivot < Left
( 4 < 1)
NO
So we swap pivot and right
Left Right
0 1 2 3 4 5
1 2 3 4 5 6
The Array is Sorted
Jeanie Lyn Arnoco

More Related Content

What's hot (20)

linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Segment tree
Segment treeSegment tree
Segment tree
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Data Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithmsData Structures- Part4 basic sorting algorithms
Data Structures- Part4 basic sorting algorithms
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Heap sort
Heap sortHeap sort
Heap sort
 

Recently uploaded

call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...home
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funneljen_giacalone
 
2-tool presenthdbdbdbdbddhdhddation.pptx
2-tool presenthdbdbdbdbddhdhddation.pptx2-tool presenthdbdbdbdbddhdhddation.pptx
2-tool presenthdbdbdbdbddhdhddation.pptxsuhanimunjal27
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja Nehwal
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja Nehwal
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...SUHANI PANDEY
 
Pastel Portfolio _ by Slidesgo.pptx. Xxx
Pastel Portfolio _ by Slidesgo.pptx. XxxPastel Portfolio _ by Slidesgo.pptx. Xxx
Pastel Portfolio _ by Slidesgo.pptx. XxxSegundoManuelFaichin1
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 

Recently uploaded (20)

call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funnel
 
2-tool presenthdbdbdbdbddhdhddation.pptx
2-tool presenthdbdbdbdbddhdhddation.pptx2-tool presenthdbdbdbdbddhdhddation.pptx
2-tool presenthdbdbdbdbddhdhddation.pptx
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
Pastel Portfolio _ by Slidesgo.pptx. Xxx
Pastel Portfolio _ by Slidesgo.pptx. XxxPastel Portfolio _ by Slidesgo.pptx. Xxx
Pastel Portfolio _ by Slidesgo.pptx. Xxx
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 

Quick sort-170316064200

  • 2. This sorting algorithm uses the idea of divide and conquer. It finds the element called pivot which divides the array into two halves in such a way that elements in the left half are smaller than pivot and elements in the right half are greater than pivot. QUICK SORT
  • 3. Three steps Find pivot that divides the array into two halves. Quick sort the left half. Quick sort the right half. QUICK SORT
  • 4. Example Consider an array having 6 elements 5 2 6 1 3 4 Arrange the elements in ascending order using quick sort algorithm
  • 5. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array
  • 6. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left
  • 7. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left Initially pointing to the First element of the array
  • 8. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left Initially pointing to the First element of the array Right
  • 9. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left Initially pointing to the First element of the array Right Initially pointing to the Last element of the array
  • 10. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left Initially pointing to the First element of the array Right Initially pointing to the Last element of the array Pivot
  • 11. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left Initially pointing to the First element of the array Right Initially pointing to the Last element of the array Pivot Initially pointing to the First element
  • 12. 0 1 2 3 4 5 5 2 6 1 3 4 Array index Array element This is our unsorted array Left Initially pointing to the First element of the array Right Initially pointing to the Last element of the array Pivot Initially pointing to the First element We will quick sort this array
  • 13. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot
  • 14. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Remember this rule:
  • 15. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Remember this rule: All element to the RIGHT of pivot be GREATER than pivot.
  • 16. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Remember this rule: All element to the RIGHT of pivot be GREATER than pivot. All element to the LEFT of pivot be SMALLER than pivot.
  • 17. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot As the pivot is pointing at left
  • 18. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot As the pivot is pointing at left So we will start from right
  • 19. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot As the pivot is pointing at left So we will start from rightAnd move towards left
  • 20. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Pivot = 5 Right =4
  • 21. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Pivot = 5 Right =4 Is Pivot < Right ( 5 < 4)
  • 22. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Pivot = 5 Right =4 NO Is Pivot < Right ( 5 < 4)
  • 23. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Pivot = 5 Right =4 So we swap pivot and right NO Is Pivot < Right ( 5 < 4)
  • 24. 0 1 2 3 4 5 5 2 6 1 3 4 Left Right Pivot Pivot = 5 Right =4 So we swap pivot and right NO Is Pivot < Right ( 5 < 4)
  • 25. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Right =4 So we swap pivot and right NO Is Pivot < Right ( 5 < 4)
  • 26. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =4 Is Pivot < Left Now move pivot to right
  • 27. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =4 Is Pivot < Left NO So we swap pivot to the right
  • 28. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Now the pivot is pointing at right
  • 29. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Now the pivot is pointing at right So we will start from left
  • 30. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Now the pivot is pointing at right So we will start from left And move towards right
  • 31. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =4 Is Pivot > Left ( 5 > 4)
  • 32. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =4 Is Pivot > Left YES ( 5 > 4)
  • 33. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =4 Is Pivot > Left YES ( 5 > 4) So we move left one position towards right
  • 34. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =2 Is Pivot > Left ( 5 > 2)
  • 35. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =2 Is Pivot > Left ( 5 > 2) YES
  • 36. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =2 Is Pivot > Left ( 5 > 2) YES So we move left one position towards right
  • 37. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =6 Is Pivot > Left ( 5 > 6)
  • 38. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =6 Is Pivot > Left ( 5 > 6) NO
  • 39. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =6 Is Pivot > Left ( 5 > 6) NO So we swap pivot and left
  • 40. 0 1 2 3 4 5 4 2 6 1 3 5 Left Right Pivot Pivot = 5 Left =6 Is Pivot > Left ( 5 > 6) NO So we swap pivot and left
  • 41. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Left =6 Is Pivot > Left ( 5 > 6) NO So we swap pivot and left
  • 42. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Left =6 Is Pivot > Left ( 5 > 6) NO So we swap pivot and left And move the pivot to left
  • 43. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot
  • 44. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Now the pivot is pointing at left
  • 45. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Now the pivot is pointing at left So we will start from right
  • 46. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Now the pivot is pointing at left So we will start from right And move towards left
  • 47. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Right =6 Is Pivot < Right ( 5 < 6)
  • 48. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Right =6 Is Pivot < Right ( 5 < 6) YES
  • 49. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Right =6 Is Pivot < Right ( 5 < 6) YES So we move right one position towards left
  • 50. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Right =3 Is Pivot < Right ( 5 < 3)
  • 51. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Right =3 Is Pivot < Right ( 5 < 3) NO
  • 52. 0 1 2 3 4 5 4 2 5 1 3 6 Left Right Pivot Pivot = 5 Right =3 Is Pivot < Right ( 5 < 3) NO So we swap pivot and right
  • 53. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 5 Right =3 Is Pivot < Right ( 5 < 3) NO So we swap pivot and right
  • 54. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 5 Right =3 Is Pivot > Right ( 5 > 3) NO So we swap pivot and right And move the pivot to right
  • 55. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 5 Left =3 Is Pivot > Left ( 5 > 3)
  • 56. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 5 Left =3YES Is Pivot > Left ( 5 > 3)
  • 57. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 5 Left =3 Is Pivot > Left ( 5 > 3) So we move left one position towards right YES
  • 58. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 5 Left = 1 Is Pivot > Left ( 5 > 1)
  • 59. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot
  • 60. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array
  • 61. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array This time 5 is the pivot and it is at the sorted position 4 5
  • 62. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array This time 5 is the pivot and it is at the sorted position 4 5 Elements left of pivot are smaller
  • 63. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array This time 5 is the pivot and it is at the sorted position 4 5 Elements left of pivot are smaller Elements right of pivot are greater
  • 64. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array This time 5 is the pivot and it is at the sorted position 4 5 Elements left of pivot are smaller Elements right of pivot are greater So pivot has divided the array into two sub array
  • 65. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array This time 5 is the pivot and it is at the sorted position 4 5 Elements left of pivot are smaller Elements right of pivot are greater Left sub array Right sub array So pivot has divided the array into two sub array
  • 66. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Now both left and right are pointing at the same element of the array This time 5 is the pivot and it is at the sorted position 4 5 Elements left of pivot are smaller Elements right of pivot are greater Left sub array Right sub array So pivot has divided the array into two sub array We will now quick sort the left sub array
  • 67. 0 1 2 3 4 5 4 2 3 1 5 6 Left Right Pivot Pivot = 4 Left =1 Is Pivot < Left ( 4 < 1)
  • 68. 0 1 2 3 4 5 4 2 3 1 5 6 Pivot Pivot = 4 Left =1 Is Pivot < Left ( 4 < 1) NO Left Right
  • 69. 0 1 2 3 4 5 4 2 3 1 5 6 Pivot Pivot = 4 Left =1 Is Pivot < Left ( 4 < 1) NO So we swap pivot and right Left Right
  • 70. 0 1 2 3 4 5 1 2 3 4 5 6 Pivot Pivot = 4 Left =1 Is Pivot < Left ( 4 < 1) NO So we swap pivot and right Left Right
  • 71. 0 1 2 3 4 5 1 2 3 4 5 6 Left Right Pivot Pivot = 4 Left =1 Is Pivot < Left ( 4 < 1) NO So we swap pivot and right Now move the pivot to right
  • 72. 0 1 2 3 4 5 1 2 3 4 5 6 Pivot Pivot = 4 Left =1 Is Pivot < Left ( 4 < 1) NO So we swap pivot and right Left Right
  • 73. 0 1 2 3 4 5 1 2 3 4 5 6 The Array is Sorted