SlideShare a Scribd company logo
1 of 24
Sorting
Common sorting algorithms
How do you sort?
Assuming we want to sort an array of integers in ascending order
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
5 1 7 4 2
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
5 1 7 4 2
5 1 4 7 2
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
5 1 7 4 2
5 1 4 7 2
5 1 4 2 7
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
5 1 7 4 2
 Question: How many passes do we need?
5 1 4 7 2
5 1 4 2 7
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
5 1 7 4 2
 Question: How many passes do we need?
5 1 4 7 2
5 1 4 2 7
Bubble Sort – Concept
 Let’s scan from left to right and swap conflicting neighboring integers!
5 7 1 4 2
5 1 7 4 2
 Question: How many passes do we need?
5 1 4 7 2
5 1 4 2 7
(N – 1)
Bubble Sort – Python
Selection Sort – Concept
 Let’s find the smallest integer, then the 2nd-smallest, 3rd-smallest and so on!
smallest 2nd-smallest 3rd-smallest 4th-smallest 5th-smallest
 This is selection sort!
Selection Sort – Python
Insertion Sort – Concept
 Suppose we have a sorted array of integers already, and we want to insert
a new integer
1 3 7 2 4
sorted insert this
Insertion Sort – Concept
 Suppose we have a sorted array of integers already, and we want to insert
a new integer
1 3 7 2 4
sorted insert this
 Find the position, then shift integers right for space
1 2 3 7 4
shifted rightinserted
Insertion Sort – Concept
 Suppose we have a sorted array of integers already, and we want to insert
a new integer
1 3 7 2 4
sorted insert this
 Find the position, then shift integers right for space
1 2 3 7 4
shifted rightinserted
 We do this for all integers
Insertion Sort – Python
Merge Sort – Concept
 Divide and Conquer
 Divide an array into equal halves, sort them and combine the 2 sorted
arrays into 1
Merge Sort – Concept
 Combining 2 sorted arrays
 2 finger approach
2 3 5 7 1 4 6 9
1
Merge Sort – Concept
 Combining 2 sorted arrays
 2 finger approach
2 3 5 7 1 4 6 9
1
2 3 5 7 1 4 6 9
1 2
Merge Sort – Python
Divide
Conquer
Quick Sort – Concept
 Divide and Conquer – again
 Choose a “pivot” and divide the array into 2 halves where the left half is
less than (or equal to) the pivot and the right half is greater than the pivot.
Conquering is simply returning the array.
5 7 3 6 2 8 4
Quick Sort – Concept
 Divide and Conquer – again
 Choose a “pivot” and divide the array into 2 halves where the left half is
less than (or equal to) the pivot and the right half is greater than the pivot.
Conquering is simply returning the array.
5 7 3 6 2 8 4
3 2 4 5 7 6 8
Quick Sort – Concept
 Divide and Conquer – again
 Choose a “pivot” and divide the array into 2 halves where the left half is
less than (or equal to) the pivot and the right half is greater than the pivot.
Conquering is simply returning the array.
5 7 3 6 2 8 4
3 2 4 5 7 6 8
3 2 4 7 6 8
Quick Sort – Python

More Related Content

What's hot

I.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AII.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AIvikas dhakane
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sortUmmar Hayat
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Sorting in python
Sorting in python Sorting in python
Sorting in python Simplilearn
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 

What's hot (20)

I.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AII.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AI
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Sorting
SortingSorting
Sorting
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Sorting in python
Sorting in python Sorting in python
Sorting in python
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 

Similar to Sorting

Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 
Sorting algorithms in Data Structure
Sorting algorithms in Data StructureSorting algorithms in Data Structure
Sorting algorithms in Data StructureBalamurugan M
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notesmailund
 
Merge sort implementation
Merge sort implementationMerge sort implementation
Merge sort implementationsadikulhaque1
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityMotaleb Hossen Manik
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
K-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptxK-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptxchacha987301
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Comparing and ordering whole numbers and decimals
Comparing and ordering whole numbers and decimals Comparing and ordering whole numbers and decimals
Comparing and ordering whole numbers and decimals Joemarie Araneta
 

Similar to Sorting (20)

Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
Sorting algorithms in Data Structure
Sorting algorithms in Data StructureSorting algorithms in Data Structure
Sorting algorithms in Data Structure
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notes
 
Merge sort implementation
Merge sort implementationMerge sort implementation
Merge sort implementation
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Quick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili SaghafiQuick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili Saghafi
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
K-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptxK-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptx
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Sorting
SortingSorting
Sorting
 
Machine learning
Machine learningMachine learning
Machine learning
 
Comparing and ordering whole numbers and decimals
Comparing and ordering whole numbers and decimals Comparing and ordering whole numbers and decimals
Comparing and ordering whole numbers and decimals
 

Recently uploaded

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Recently uploaded (20)

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Sorting

  • 2. How do you sort? Assuming we want to sort an array of integers in ascending order
  • 3. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2
  • 4. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2 5 1 7 4 2
  • 5. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2 5 1 7 4 2 5 1 4 7 2
  • 6. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2 5 1 7 4 2 5 1 4 7 2 5 1 4 2 7
  • 7. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2 5 1 7 4 2  Question: How many passes do we need? 5 1 4 7 2 5 1 4 2 7
  • 8. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2 5 1 7 4 2  Question: How many passes do we need? 5 1 4 7 2 5 1 4 2 7
  • 9. Bubble Sort – Concept  Let’s scan from left to right and swap conflicting neighboring integers! 5 7 1 4 2 5 1 7 4 2  Question: How many passes do we need? 5 1 4 7 2 5 1 4 2 7 (N – 1)
  • 10. Bubble Sort – Python
  • 11. Selection Sort – Concept  Let’s find the smallest integer, then the 2nd-smallest, 3rd-smallest and so on! smallest 2nd-smallest 3rd-smallest 4th-smallest 5th-smallest  This is selection sort!
  • 13. Insertion Sort – Concept  Suppose we have a sorted array of integers already, and we want to insert a new integer 1 3 7 2 4 sorted insert this
  • 14. Insertion Sort – Concept  Suppose we have a sorted array of integers already, and we want to insert a new integer 1 3 7 2 4 sorted insert this  Find the position, then shift integers right for space 1 2 3 7 4 shifted rightinserted
  • 15. Insertion Sort – Concept  Suppose we have a sorted array of integers already, and we want to insert a new integer 1 3 7 2 4 sorted insert this  Find the position, then shift integers right for space 1 2 3 7 4 shifted rightinserted  We do this for all integers
  • 17. Merge Sort – Concept  Divide and Conquer  Divide an array into equal halves, sort them and combine the 2 sorted arrays into 1
  • 18. Merge Sort – Concept  Combining 2 sorted arrays  2 finger approach 2 3 5 7 1 4 6 9 1
  • 19. Merge Sort – Concept  Combining 2 sorted arrays  2 finger approach 2 3 5 7 1 4 6 9 1 2 3 5 7 1 4 6 9 1 2
  • 20. Merge Sort – Python Divide Conquer
  • 21. Quick Sort – Concept  Divide and Conquer – again  Choose a “pivot” and divide the array into 2 halves where the left half is less than (or equal to) the pivot and the right half is greater than the pivot. Conquering is simply returning the array. 5 7 3 6 2 8 4
  • 22. Quick Sort – Concept  Divide and Conquer – again  Choose a “pivot” and divide the array into 2 halves where the left half is less than (or equal to) the pivot and the right half is greater than the pivot. Conquering is simply returning the array. 5 7 3 6 2 8 4 3 2 4 5 7 6 8
  • 23. Quick Sort – Concept  Divide and Conquer – again  Choose a “pivot” and divide the array into 2 halves where the left half is less than (or equal to) the pivot and the right half is greater than the pivot. Conquering is simply returning the array. 5 7 3 6 2 8 4 3 2 4 5 7 6 8 3 2 4 7 6 8
  • 24. Quick Sort – Python