Sorting Algorithms
Technical Writing(CS1305) Assignment
Shivam Singh
20148025
MNNIT
August 2015
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 1 / 20
What Is A Sorting Algorithm ?
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 2 / 20
Bubble Sort
Compare each element (except the last one) with its neighbor to the
right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20
Bubble Sort
Compare each element (except the last one) with its neighbor to the
right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its neighbor to the
right
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20
Bubble Sort
Compare each element (except the last one) with its neighbor to the
right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its neighbor to the
right
Compare each element (except the last three) with its neighbor to the
right
Continue as above until you have no unsorted elements on the left
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20
Bubble Sort
Explanation
Figure: Graphical illustration of Bubble Sort
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 4 / 20
Bubble Sort
Pseudo-Code
for i ← 0toN − 2 do
for j ← 0toN − 2 do
if A[j] ≥ A[j + 1] then
Swap(A[j],A[j+1])
end if
end for
end for
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 5 / 20
Selection Sort
Given an array of length n :
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
Selection Sort
Given an array of length n :
Search elements 0 through n-1 and select the smallest
Swap it with the element in location 0
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
Selection Sort
Given an array of length n :
Search elements 0 through n-1 and select the smallest
Swap it with the element in location 0
Search elements 1 through n-1 and select the smallest
Swap it with the element in location 1
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
Selection Sort
Given an array of length n :
Search elements 0 through n-1 and select the smallest
Swap it with the element in location 0
Search elements 1 through n-1 and select the smallest
Swap it with the element in location 1
Continue in this fashion until theres nothing left to search
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
Selection Sort
Graphical Illustration
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 7 / 20
Selection Sort
Pseudo-Code
for i ← 0toN − 1 do
Min ← i
for j ← 0toN − 1 do
if A[j] ≤ A[Min] then
Min ← j
end if
end for
if i = Min then
Swap(A[i],A[Min])
end if
end for
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 8 / 20
Insertion Sort
Outer Loop
The Outer Loop i goes from 1 → N
all the elements to the left of i are sorted with respect to one another
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 9 / 20
Insertion Sort
Outer Loop
The Outer Loop i goes from 1 → N
all the elements to the left of i are sorted with respect to one another
Inner Loop
In every iteration of the Inner Loop we are :
1 Finding the elements proper place
2 Making room for the inserted element (by shifting over other elements)
3 Inserting the element
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 9 / 20
Insertion Sort
Graphical Illustration
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 10 / 20
Insertion Sort
Pseudo-Code
for i ← 1toN − 1 do
Temp ← A[i]
j ← i
while A[j] > Tempandj > 0 do
A[j] ← A[j − 1]
j ← j − 1
end while
A[j] ← Temp
end for
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 11 / 20
Merge Sort
A Divide and Conquer Algorithm
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20
Merge Sort
A Divide and Conquer Algorithm
Divide the unsorted array into 2 halves until the sub-arrays only
contain one element
Merge the sub-problem solutions together
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20
Merge Sort
A Divide and Conquer Algorithm
Divide the unsorted array into 2 halves until the sub-arrays only
contain one element
Merge the sub-problem solutions together
1 Compare the sub-arrays first elements
2 Remove the smallest element and put it into the result array
3 Continue the process until all elements have been put into the result
array
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20
Merge Sort-Explanation
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 13 / 20
Merge Sort
Pseudo-Code
Divide And Conquer
Sort(A, N)
if N ≤ 1 then
return
end if
nL ← N/2 , nR ← N − N/2
for i ← 0toMid − 1 do
Left[i] ← A[i]
end for
for i ← MidtoN − 1 do
Right[i] ← A[i]
end for
Sort(Left, nL)
Sort(Right, nR)
Merge(Left, Right, A)
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 14 / 20
Merge Sort
Pseudo-Code
Merging The SubArrays
Merge(Left, Right, A, N)
nL ← N/2 , nR ← N − N/2 , i ← j ← k ← 0
while i < nLandj < nR do
if Left[i] ≤ Right[j] then
A[k] ← Left[i]
i ← i + 1
else
A[k] ← Right[j]
j ← j + 1
end if
k ← k + 1
end while
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 15 / 20
Merge Sort
Pseudo-Code
Merging The SubArrays Cont...
while i < nL do
A[k] ← Left[i]
i ← i + 1
k ← k + 1
end while
while j < nR do
A[k] ← Right[j]
j ← j + 1
k ← k + 1
end while
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 16 / 20
Which Is The Best ?
Algorithm Time Complexity
Bubble Sort O(n2)
Selection Sort O(n2)
Insertion Sort O(n2)
Merge Sort O(n ∗ (log2n))
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 17 / 20
Which Is The Best ?
Algorithm Time Complexity
Bubble Sort O(n2)
Selection Sort O(n2)
Insertion Sort O(n2)
Merge Sort O(n ∗ (log2n))
Answer :
As we can observe from the Time Complexities, Merge Sort will be the
fastest Sorting Algorithm !
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 17 / 20
Special Mention
QuickSort
Recursive Quick Sort
QuickSort(A, Start, End)
if Start ≥ End then
return
end if
pIndex ←Partition(A, Start, End)
QuickSort(A, Start, pIndex-1)
QuickSort(A, pIndex+1, End)
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 18 / 20
Special Mention
QuickSort
Partitioning Around The Pivot
Partition(A, Start, End)
Pivot ← A[End]
PIndex ← Start
for i ← StarttoEnd − 1 do
if A[i] ≤ Pivot then
Swap(A[i],A[PIndex])
PIndex ← PIndex + 1
end if
end for
Swap(A[i],A[PIndex])
return PIndex
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 19 / 20
End
Thank you
Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 20 / 20

More Related Content

PPTX
Skill34 linear quadratic multiple representation of functions
ODP
Sorting Algorithm
PPT
Sorting algorithms v01
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
Sorting algorithms
PPTX
Parallel sorting algorithm
PPT
Sorting Algorithms
PPTX
Sorting algorithms
Skill34 linear quadratic multiple representation of functions
Sorting Algorithm
Sorting algorithms v01
Data Structures - Lecture 8 [Sorting Algorithms]
Sorting algorithms
Parallel sorting algorithm
Sorting Algorithms
Sorting algorithms

Viewers also liked (20)

PPT
Data Structures - Searching & sorting
PPT
COMPUTER PROGRAMMING UNIT 1 Lecture 5
PPTX
Sorting Algorithms
PPT
Bucket sort
PPT
Counting Sort and Radix Sort Algorithms
PPT
PPTX
Metodos de ordenacion radix sort
PPTX
Radix sorting
PPTX
Merge sort and quick sort
PPT
Dc parent 14 2
PPTX
CSS 3, Style and Beyond
PPT
Sorting algorithms
PPS
Ds 4
PPTX
Was There A Darwinian Revolution
KEY
The Scientific Revolution
PPT
Chap04alg
PPTX
Chapter one
KEY
The Chemical Revolution
PPT
Google at a glance 1998 - 2008
Data Structures - Searching & sorting
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Sorting Algorithms
Bucket sort
Counting Sort and Radix Sort Algorithms
Metodos de ordenacion radix sort
Radix sorting
Merge sort and quick sort
Dc parent 14 2
CSS 3, Style and Beyond
Sorting algorithms
Ds 4
Was There A Darwinian Revolution
The Scientific Revolution
Chap04alg
Chapter one
The Chemical Revolution
Google at a glance 1998 - 2008
Ad

Similar to Sorting Algorithms (20)

PPTX
Sorting Algorithms to arrange data in particular format
PPTX
Different Searching and Sorting Methods.pptx
PPT
Quicksort
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
PDF
Quick sort,bubble sort,heap sort and merge sort
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPTX
9.Sorting & Searching
ODP
Data operatons & searching and sorting algorithms
PPT
Sorting of linked list data through python.ppt
PPTX
Lecture3a sorting
DOCX
Sorting
PPTX
All Searching and Sorting Techniques in Data Structures
PPTX
Sorting Algorithm
PPTX
Unit 5 dsuc
PDF
Data structures and algorithms - sorting algorithms
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
PDF
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
PPTX
Sorting Algorithms
PPTX
Data structure using c module 3
PPT
3.8 quick sort
Sorting Algorithms to arrange data in particular format
Different Searching and Sorting Methods.pptx
Quicksort
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Quick sort,bubble sort,heap sort and merge sort
Analysis of Algorithm (Bubblesort and Quicksort)
9.Sorting & Searching
Data operatons & searching and sorting algorithms
Sorting of linked list data through python.ppt
Lecture3a sorting
Sorting
All Searching and Sorting Techniques in Data Structures
Sorting Algorithm
Unit 5 dsuc
Data structures and algorithms - sorting algorithms
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
Sorting Algorithms
Data structure using c module 3
3.8 quick sort
Ad

More from Shivam Singh (7)

PDF
Stacks
PDF
PPTX
What If Microsoft sold diapers!? MS Diapers!
PDF
Search Algprithms
PDF
Binary Search Tree
PDF
Graph Theory
PDF
Linked List
Stacks
What If Microsoft sold diapers!? MS Diapers!
Search Algprithms
Binary Search Tree
Graph Theory
Linked List

Recently uploaded (20)

PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
PDF
Design Guidelines and solutions for Plastics parts
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PDF
ChapteR012372321DFGDSFGDFGDFSGDFGDFGDFGSDFGDFGFD
PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PPTX
Module 8- Technological and Communication Skills.pptx
PPTX
communication and presentation skills 01
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Exploratory_Data_Analysis_Fundamentals.pdf
Abrasive, erosive and cavitation wear.pdf
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
Design Guidelines and solutions for Plastics parts
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
ChapteR012372321DFGDSFGDFGDFSGDFGDFGDFGSDFGDFGFD
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
August -2025_Top10 Read_Articles_ijait.pdf
Module 8- Technological and Communication Skills.pptx
communication and presentation skills 01

Sorting Algorithms

  • 1. Sorting Algorithms Technical Writing(CS1305) Assignment Shivam Singh 20148025 MNNIT August 2015 Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 1 / 20
  • 2. What Is A Sorting Algorithm ? Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 2 / 20
  • 3. Bubble Sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20
  • 4. Bubble Sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place Compare each element (except the last two) with its neighbor to the right Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20
  • 5. Bubble Sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place Compare each element (except the last two) with its neighbor to the right Compare each element (except the last three) with its neighbor to the right Continue as above until you have no unsorted elements on the left Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20
  • 6. Bubble Sort Explanation Figure: Graphical illustration of Bubble Sort Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 4 / 20
  • 7. Bubble Sort Pseudo-Code for i ← 0toN − 2 do for j ← 0toN − 2 do if A[j] ≥ A[j + 1] then Swap(A[j],A[j+1]) end if end for end for Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 5 / 20
  • 8. Selection Sort Given an array of length n : Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
  • 9. Selection Sort Given an array of length n : Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
  • 10. Selection Sort Given an array of length n : Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
  • 11. Selection Sort Given an array of length n : Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Continue in this fashion until theres nothing left to search Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20
  • 12. Selection Sort Graphical Illustration Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 7 / 20
  • 13. Selection Sort Pseudo-Code for i ← 0toN − 1 do Min ← i for j ← 0toN − 1 do if A[j] ≤ A[Min] then Min ← j end if end for if i = Min then Swap(A[i],A[Min]) end if end for Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 8 / 20
  • 14. Insertion Sort Outer Loop The Outer Loop i goes from 1 → N all the elements to the left of i are sorted with respect to one another Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 9 / 20
  • 15. Insertion Sort Outer Loop The Outer Loop i goes from 1 → N all the elements to the left of i are sorted with respect to one another Inner Loop In every iteration of the Inner Loop we are : 1 Finding the elements proper place 2 Making room for the inserted element (by shifting over other elements) 3 Inserting the element Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 9 / 20
  • 16. Insertion Sort Graphical Illustration Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 10 / 20
  • 17. Insertion Sort Pseudo-Code for i ← 1toN − 1 do Temp ← A[i] j ← i while A[j] > Tempandj > 0 do A[j] ← A[j − 1] j ← j − 1 end while A[j] ← Temp end for Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 11 / 20
  • 18. Merge Sort A Divide and Conquer Algorithm Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20
  • 19. Merge Sort A Divide and Conquer Algorithm Divide the unsorted array into 2 halves until the sub-arrays only contain one element Merge the sub-problem solutions together Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20
  • 20. Merge Sort A Divide and Conquer Algorithm Divide the unsorted array into 2 halves until the sub-arrays only contain one element Merge the sub-problem solutions together 1 Compare the sub-arrays first elements 2 Remove the smallest element and put it into the result array 3 Continue the process until all elements have been put into the result array Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20
  • 21. Merge Sort-Explanation Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 13 / 20
  • 22. Merge Sort Pseudo-Code Divide And Conquer Sort(A, N) if N ≤ 1 then return end if nL ← N/2 , nR ← N − N/2 for i ← 0toMid − 1 do Left[i] ← A[i] end for for i ← MidtoN − 1 do Right[i] ← A[i] end for Sort(Left, nL) Sort(Right, nR) Merge(Left, Right, A) Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 14 / 20
  • 23. Merge Sort Pseudo-Code Merging The SubArrays Merge(Left, Right, A, N) nL ← N/2 , nR ← N − N/2 , i ← j ← k ← 0 while i < nLandj < nR do if Left[i] ≤ Right[j] then A[k] ← Left[i] i ← i + 1 else A[k] ← Right[j] j ← j + 1 end if k ← k + 1 end while Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 15 / 20
  • 24. Merge Sort Pseudo-Code Merging The SubArrays Cont... while i < nL do A[k] ← Left[i] i ← i + 1 k ← k + 1 end while while j < nR do A[k] ← Right[j] j ← j + 1 k ← k + 1 end while Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 16 / 20
  • 25. Which Is The Best ? Algorithm Time Complexity Bubble Sort O(n2) Selection Sort O(n2) Insertion Sort O(n2) Merge Sort O(n ∗ (log2n)) Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 17 / 20
  • 26. Which Is The Best ? Algorithm Time Complexity Bubble Sort O(n2) Selection Sort O(n2) Insertion Sort O(n2) Merge Sort O(n ∗ (log2n)) Answer : As we can observe from the Time Complexities, Merge Sort will be the fastest Sorting Algorithm ! Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 17 / 20
  • 27. Special Mention QuickSort Recursive Quick Sort QuickSort(A, Start, End) if Start ≥ End then return end if pIndex ←Partition(A, Start, End) QuickSort(A, Start, pIndex-1) QuickSort(A, pIndex+1, End) Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 18 / 20
  • 28. Special Mention QuickSort Partitioning Around The Pivot Partition(A, Start, End) Pivot ← A[End] PIndex ← Start for i ← StarttoEnd − 1 do if A[i] ≤ Pivot then Swap(A[i],A[PIndex]) PIndex ← PIndex + 1 end if end for Swap(A[i],A[PIndex]) return PIndex Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 19 / 20
  • 29. End Thank you Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 20 / 20