SlideShare a Scribd company logo
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)

PPTX
Quick sort
Dhruv Sabalpara
 
PPT
Heap sort
Mohd Arif
 
PDF
Array data structure
maamir farooq
 
PPTX
Quick Sort
Shweta Sahu
 
PPTX
Bubble sort | Data structure |
MdSaiful14
 
PPTX
Deque and its applications
Jsaddam Hussain
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPTX
Insertion sort
Monalisa Patel
 
PPT
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
PPTX
linked list in data structure
shameen khan
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PPTX
heap Sort Algorithm
Lemia Algmri
 
PPTX
Quicksort Presentation
irdginfo
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPT
Merge sort
Vidushi Pathak
 
PDF
Stack
Zaid Shabbir
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Quick sort
Dhruv Sabalpara
 
Heap sort
Mohd Arif
 
Array data structure
maamir farooq
 
Quick Sort
Shweta Sahu
 
Bubble sort | Data structure |
MdSaiful14
 
Deque and its applications
Jsaddam Hussain
 
Priority Queue in Data Structure
Meghaj Mallick
 
Insertion sort
Monalisa Patel
 
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
linked list in data structure
shameen khan
 
Sparse matrix and its representation data structure
Vardhil Patel
 
heap Sort Algorithm
Lemia Algmri
 
Quicksort Presentation
irdginfo
 
linear search and binary search
Zia Ush Shamszaman
 
Merge sort
Vidushi Pathak
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 

Viewers also liked (20)

PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPTX
Quick sort
Jehat Hassan
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PDF
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
PPT
Quick Sort
priyankanaidu6
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PPTX
Philip Crosby Principles
Jeanie Arnoco
 
PPT
Introduction to programming using Visual Basic 6
Jeanie Arnoco
 
PPT
Quick sort
Rajendran
 
PPT
Sorting algos
Omair Imtiaz Ansari
 
DOCX
25 java tough interview questions
Arun Banotra
 
PDF
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
PPTX
Java Sorting Algorithms
Brendan Campbell
 
PPT
Longest common subsequences in Algorithm Analysis
Rajendran
 
PPT
Meaghan technology report
Marq2014
 
DOC
Wie zit er écht achter de site mvslim.com?
Thierry Debels
 
PDF
Access may newsletter
enrique santoyo quezada
 
PPT
The Dream Act
juliakoenig
 
DOCX
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Sunil Kumar Gunasekaran
 
Algorithm: Quick-Sort
Tareq Hasan
 
Quick sort
Jehat Hassan
 
Divide and conquer - Quick sort
Madhu Bala
 
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Quick Sort
priyankanaidu6
 
Merge sort and quick sort
Shakila Mahjabin
 
Philip Crosby Principles
Jeanie Arnoco
 
Introduction to programming using Visual Basic 6
Jeanie Arnoco
 
Quick sort
Rajendran
 
Sorting algos
Omair Imtiaz Ansari
 
25 java tough interview questions
Arun Banotra
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
Java Sorting Algorithms
Brendan Campbell
 
Longest common subsequences in Algorithm Analysis
Rajendran
 
Meaghan technology report
Marq2014
 
Wie zit er écht achter de site mvslim.com?
Thierry Debels
 
Access may newsletter
enrique santoyo quezada
 
The Dream Act
juliakoenig
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Sunil Kumar Gunasekaran
 
Ad

More from Jeanie Arnoco (20)

PPT
The Database Environment Chapter 15
Jeanie Arnoco
 
PPT
The Database Environment Chapter 14
Jeanie Arnoco
 
PPT
The Database Environment Chapter 13
Jeanie Arnoco
 
PPT
The Database Environment Chapter 12
Jeanie Arnoco
 
PPT
The Database Environment Chapter 11
Jeanie Arnoco
 
PPT
The Database Environment Chapter 10
Jeanie Arnoco
 
PPT
The Database Environment Chapter 9
Jeanie Arnoco
 
PPT
The Database Environment Chapter 8
Jeanie Arnoco
 
PPT
The Database Environment Chapter 7
Jeanie Arnoco
 
PPT
The Database Environment Chapter 6
Jeanie Arnoco
 
PPT
The Database Environment Chapter 5
Jeanie Arnoco
 
PPT
The Database Environment Chapter 4
Jeanie Arnoco
 
PPT
The Database Environment Chapter 3
Jeanie Arnoco
 
PPT
The Database Environment Chapter 2
Jeanie Arnoco
 
PPT
The Database Environment Chapter 1
Jeanie Arnoco
 
PPT
Introduction to BOOTSTRAP
Jeanie Arnoco
 
PPTX
Hacking and Online Security
Jeanie Arnoco
 
PPTX
(CAR)Cordillera Administrative Region
Jeanie Arnoco
 
PPTX
Quality Gurus Student
Jeanie Arnoco
 
PPT
QUALITY STANDARDS
Jeanie Arnoco
 
The Database Environment Chapter 15
Jeanie Arnoco
 
The Database Environment Chapter 14
Jeanie Arnoco
 
The Database Environment Chapter 13
Jeanie Arnoco
 
The Database Environment Chapter 12
Jeanie Arnoco
 
The Database Environment Chapter 11
Jeanie Arnoco
 
The Database Environment Chapter 10
Jeanie Arnoco
 
The Database Environment Chapter 9
Jeanie Arnoco
 
The Database Environment Chapter 8
Jeanie Arnoco
 
The Database Environment Chapter 7
Jeanie Arnoco
 
The Database Environment Chapter 6
Jeanie Arnoco
 
The Database Environment Chapter 5
Jeanie Arnoco
 
The Database Environment Chapter 4
Jeanie Arnoco
 
The Database Environment Chapter 3
Jeanie Arnoco
 
The Database Environment Chapter 2
Jeanie Arnoco
 
The Database Environment Chapter 1
Jeanie Arnoco
 
Introduction to BOOTSTRAP
Jeanie Arnoco
 
Hacking and Online Security
Jeanie Arnoco
 
(CAR)Cordillera Administrative Region
Jeanie Arnoco
 
Quality Gurus Student
Jeanie Arnoco
 
QUALITY STANDARDS
Jeanie Arnoco
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 

Quick sort-Data Structure

  • 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