SlideShare a Scribd company logo
Algorithms
Quick Sort
Abdelrahman M. Saleh
List of contents
● Introduction
● Example w/ illustrating figures
● Algorithm
● implementation (Java, C++, Python)
● Performance Runtime
○ Best, Average and worst cases.
● Execution
● Other Notes
Introduction
● Like Merge sort it’s Divide and Conquer algorithm => picks an element as pivot and
partitions the given array around it
● There are many different versions of quickSort that pick pivot in different ways
○ Always pick first element as pivot.
○ Always pick last element as pivot (implemented below)
○ Pick a random element as pivot.
○ Pick median as pivot.
Example
Algorithm .
➔ Array name “arr” , starting index “low”, ending index “high”
● If low is smaller than high
○ pivot is partitioning index, arr[p] is now at right place :
■ pivot = partition(arr, low, high)
○ Recursively sort elements Aefore pivot :
■ Call quickSort(arr, low, pivot - 1)
○ Recursively sort elements After pivot :
■ Call quickSort(arr, pivot + 1, high)
Implementation .
C++
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
C++
Continue ...
int partition (int arr[], int low, int high)
{
int pivot = arr[high], i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
I++; swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
// partition the array around the pivot
JAVA
void sort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
JAVA
Continue ...
int partition(int arr[], int low, int high) {
int pivot = arr[high], i = (low-1);
for (int j=low; j<high; j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; //swap of [i] & [j]
}
}
int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; //swap of [i+1] & [high]
return i+1;
}
// partition the array around the pivot
Python .
def quickSort(arr,low,high):
if low < high:
pi = partition(arr,low,high)
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
Python
Continue ...
def partition(arr,low,high):
i = ( low-1 )
pivot = arr[high]
for j in range(low , high):
if arr[j] <= pivot:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
// partition the array around the pivot
Performance Runtime
● Time complexity F(n) = F(k) + F(n-k-1) +Θ(n) .
● Best case : always picks the middle element as pivot :
○ F(n) = 2F(n/2) + Θ(n) => Θ(nlogn) .
● Worst case : always picks greatest or smallest element as pivot :
○ F(n) = F(0) + F(n-1) +Θ(n) => Θ(n2
) .
● Average case : could be considered when partition puts O(n/9) elements in one set and
O(9n/10) elements in other set :
○ F(n) = F(n/9) + F(9n/10) + Θ(n) .
Execution .
Input array : [4, 6, 3, 2, 1, 9, 7]
Swap Pivot “7” with “9”
=> left part [4, 6, 3, 2, 1] right part[9]
Swap Pivot “1”, 4
Swap 6, 2
=> left part [3, 2] right [4, 6]
Swap Pivot “2”, 3
=> left part [3] right [4, 6]
Output Array: [1, 2, 3, 4, 6, 7, 9]
Other Notes .
● Algorithmic Paradigm: Divide Approach
● Sorting In Place: Yes
● Stable: Yes
● Online: Yes

More Related Content

PDF
Insertion sort
PPTX
Presentation on the topic selection sort
PPTX
Recursion
PPT
Priority queues
PPTX
Quick sort
PPTX
Sorting algorithms
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPT
Chapter 11 - Sorting and Searching
Insertion sort
Presentation on the topic selection sort
Recursion
Priority queues
Quick sort
Sorting algorithms
Analysis of Algorithm (Bubblesort and Quicksort)
Chapter 11 - Sorting and Searching

What's hot (20)

PPT
Insertion sort bubble sort selection sort
PPTX
Different Sorting tecniques in Data Structure
PPT
Counting sort(Non Comparison Sort)
PPTX
Sorting Algorithms
PDF
Binary Search - Design & Analysis of Algorithms
PPT
Data Structure and Algorithms Heaps and Trees
PPTX
Quick sort
PPTX
Queue
PPTX
Java presentation on insertion sort
PPTX
Basic data structures in python
PPTX
Hashing Technique In Data Structures
PDF
UNIT I LINEAR DATA STRUCTURES – LIST
PPT
Selection sort
PPT
Heap Sort || Heapify Method || Build Max Heap Algorithm
PPT
Algorithm: Quick-Sort
PPTX
Data Structure - Elementary Data Organization
PPTX
Merge sort algorithm power point presentation
PDF
Merge sort
PPT
Unit 1 linked list
Insertion sort bubble sort selection sort
Different Sorting tecniques in Data Structure
Counting sort(Non Comparison Sort)
Sorting Algorithms
Binary Search - Design & Analysis of Algorithms
Data Structure and Algorithms Heaps and Trees
Quick sort
Queue
Java presentation on insertion sort
Basic data structures in python
Hashing Technique In Data Structures
UNIT I LINEAR DATA STRUCTURES – LIST
Selection sort
Heap Sort || Heapify Method || Build Max Heap Algorithm
Algorithm: Quick-Sort
Data Structure - Elementary Data Organization
Merge sort algorithm power point presentation
Merge sort
Unit 1 linked list
Ad

Similar to Quick sort (20)

PDF
Selection sort
PDF
Sorting Algorithms and their implementations
PPTX
Quick sort by Sania Nisar
PPTX
quick sort by deepak.pptx
PPTX
Algorithm Complexity and Main Concepts
PPTX
Quick-Sort Algorithm and pivot selection
PDF
binary search
PPT
Unit6 C
PPTX
iPython
PPTX
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
PDF
Quick sort,bubble sort,heap sort and merge sort
PPTX
Intro to super. advance algorithm..pptx
DOCX
DAA Lab Work.docx
PPTX
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
PPTX
CSE680-07QuickSort.pptx
PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PPTX
Stack and Queue.pptx university exam preparation
PPTX
9.Sorting & Searching
PPT
Algorithm: priority queue
PPTX
A Comprehensive Comparative Study and Performance Evaluation
Selection sort
Sorting Algorithms and their implementations
Quick sort by Sania Nisar
quick sort by deepak.pptx
Algorithm Complexity and Main Concepts
Quick-Sort Algorithm and pivot selection
binary search
Unit6 C
iPython
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
Quick sort,bubble sort,heap sort and merge sort
Intro to super. advance algorithm..pptx
DAA Lab Work.docx
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
CSE680-07QuickSort.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Stack and Queue.pptx university exam preparation
9.Sorting & Searching
Algorithm: priority queue
A Comprehensive Comparative Study and Performance Evaluation
Ad

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Computing-Curriculum for Schools in Ghana
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Trump Administration's workforce development strategy
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Yogi Goddess Pres Conference Studio Updates
Computing-Curriculum for Schools in Ghana
Weekly quiz Compilation Jan -July 25.pdf
History, Philosophy and sociology of education (1).pptx
Paper A Mock Exam 9_ Attempt review.pdf.
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Trump Administration's workforce development strategy
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
What if we spent less time fighting change, and more time building what’s rig...

Quick sort

  • 2. List of contents ● Introduction ● Example w/ illustrating figures ● Algorithm ● implementation (Java, C++, Python) ● Performance Runtime ○ Best, Average and worst cases. ● Execution ● Other Notes
  • 3. Introduction ● Like Merge sort it’s Divide and Conquer algorithm => picks an element as pivot and partitions the given array around it ● There are many different versions of quickSort that pick pivot in different ways ○ Always pick first element as pivot. ○ Always pick last element as pivot (implemented below) ○ Pick a random element as pivot. ○ Pick median as pivot.
  • 5. Algorithm . ➔ Array name “arr” , starting index “low”, ending index “high” ● If low is smaller than high ○ pivot is partitioning index, arr[p] is now at right place : ■ pivot = partition(arr, low, high) ○ Recursively sort elements Aefore pivot : ■ Call quickSort(arr, low, pivot - 1) ○ Recursively sort elements After pivot : ■ Call quickSort(arr, pivot + 1, high)
  • 7. C++ void quickSort(int arr[], int low, int high) { if (low < high) { int pivot = partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high); } }
  • 8. C++ Continue ... int partition (int arr[], int low, int high) { int pivot = arr[high], i = (low - 1); for (int j = low; j <= high- 1; j++) { if (arr[j] <= pivot) { I++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); } // partition the array around the pivot
  • 9. JAVA void sort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); sort(arr, low, pi-1); sort(arr, pi+1, high); } }
  • 10. JAVA Continue ... int partition(int arr[], int low, int high) { int pivot = arr[high], i = (low-1); for (int j=low; j<high; j++) { if (arr[j] <= pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; //swap of [i] & [j] } } int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; //swap of [i+1] & [high] return i+1; } // partition the array around the pivot
  • 11. Python . def quickSort(arr,low,high): if low < high: pi = partition(arr,low,high) quickSort(arr, low, pi-1) quickSort(arr, pi+1, high)
  • 12. Python Continue ... def partition(arr,low,high): i = ( low-1 ) pivot = arr[high] for j in range(low , high): if arr[j] <= pivot: i = i+1 arr[i],arr[j] = arr[j],arr[i] arr[i+1],arr[high] = arr[high],arr[i+1] return ( i+1 ) // partition the array around the pivot
  • 13. Performance Runtime ● Time complexity F(n) = F(k) + F(n-k-1) +Θ(n) . ● Best case : always picks the middle element as pivot : ○ F(n) = 2F(n/2) + Θ(n) => Θ(nlogn) . ● Worst case : always picks greatest or smallest element as pivot : ○ F(n) = F(0) + F(n-1) +Θ(n) => Θ(n2 ) . ● Average case : could be considered when partition puts O(n/9) elements in one set and O(9n/10) elements in other set : ○ F(n) = F(n/9) + F(9n/10) + Θ(n) .
  • 14. Execution . Input array : [4, 6, 3, 2, 1, 9, 7] Swap Pivot “7” with “9” => left part [4, 6, 3, 2, 1] right part[9] Swap Pivot “1”, 4 Swap 6, 2 => left part [3, 2] right [4, 6] Swap Pivot “2”, 3 => left part [3] right [4, 6] Output Array: [1, 2, 3, 4, 6, 7, 9]
  • 15. Other Notes . ● Algorithmic Paradigm: Divide Approach ● Sorting In Place: Yes ● Stable: Yes ● Online: Yes