SlideShare a Scribd company logo
Chapter 3
BASIC SORTING
ALGORITHMS
2 0 2 4 - 2 0 2 5 S p r i n g S e m e s t e r
2
Contents
0 0
• Introduction
• Why Studying Sorting
• Sorting Categories
• Types of Sorting
 Bubble Sort
 Insertion Sort
 Selection Sort
• Linear Searching
3
Introduction
0 1
• Sorting means arranging the elements of an array so that they are
placed in some relevant order which may be either ascending or
descending.
• sorting is also used to represent data in a more readable format. Some
real-life examples of sorting are:-
 Contact List in Your Mobile Phone also contains all contacts arranged
alphabetically (lexicographically). So if you look for contact then you don’t have
to look randomly and can be searched easily and many others like Apps on your
phone.
 Keywords in Your book are also in a lexicographical manner and you can find it
according to Chapter.
4
Why Study Sorting?
0 2
• When you perform sorting on an array/elements, many problems
become easy (e.g. min/max, kth smallest/largest)
• Performing Sorting also givesnumber of algorithmic solutions that
contain many other ideas such as:
o Iterative
o Divide-and-conquer
o Comparison vs non-comparison based
o Recursive
5
Why Study Sorting?
0 2
 The main advantage of sorting is time complexity and that’s the most
important thing when you solve a problem because it’s not enough
you’re able to solve a problem but you should be able to solve it in the
minimum time possible.
 Sometimes problems can be solved easily and quickly based on sorting
which can prevent you from every Coder’s Nightmare i.e. TLE (Time Limit
Exceeded).
6
Sorting Categories
0 3
There are two different categories in sorting:
1. Internal sorting: If the input data is such that it can be
adjusted in the main memory at once, it is called internal
sorting.
2. External sorting: If the input data is such that it cannot be
adjusted in the memory entirely at once, it needs to be
stored in a hard disk, floppy disk, or any other storage
device. This is called external sorting.
7
Types of Sorting algorithms
0 4
Although there is number of sorting algorithms best algorithm
is which can solve a problem in the minimum
time and minimum space required to do so. Some types of the
Sorting algorithm are:-
1. Bubble sort
2. Insertion Sort
3. Select Sort
8
Types of Sorting algorithms
0 4
 It’s one of the simplest algorithms which repeatedly iterates through
the list, compares the elements next to each other, and swaps
according to the condition passed to them.
 This sorting algorithm is comparison-based algorithm in which each
pair of adjacent elements is compared and the elements are
swapped if they are not in order.
 This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2
) where n is the number of items.
Bubble Sort
9
Types of Sorting algorithms
0 4
Bubble Sort
10
Types of Sorting algorithms
0 4
Repeat “bubble up” how many times?
• If we have N elements
• And if each time we bubble an element, we place it in its correct
location…..
• Then we repeat the “bubble up” process N-1 times.
• This guarantees we’ll correctly place all N elements.
Bubble Sort
11
Types of Sorting algorithms
0 4
Example 1 : 5, 12, 3, 9, 16
Pass 1
● 5, 12, 3, 9, 16
○ The list stays the same because 5 is less than 12.
● 5, 3, 12, 9, 16
○ 3 and 12 are switched because 3 is less than 12
● 5, 3, 9, 12, 16
○ 9 and 12 are switched since 9 is less than 12
● 5, 3, 9, 12, 16
○ 12 and 16 do not switch because 12 is less than 16
Bubble Sort
12
Types of Sorting algorithms
0 4
Example 1 : 5, 12, 3, 9, 16
Pass 2
● 3, 5, 9, 12, 16
○ 3 is less than 5, so they switch
● 3, 5, 9, 12, 16
○ 5 is less than 9 so they remain in the same places
● 3, 5, 9, 12, 16
○ 12 is greater than 9 so they do not switch places
● 3, 5, 9, 12, 16
○ 12 and 16 are in numerical order so they don't switch
Bubble Sort
13
Types of Sorting algorithms
0 4
Example 2
Bubble Sort
13
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
0 1 2 3 4 5
14
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
12
35
42
77 101
0 1 2 3 4 5
77 42 35 12 101 5
15
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
12
35
42
77 101
0 1 2 3 4 5
Swap
42 77
77 42 35 12 101 5
16
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
12
35
77
42 101
0 1 2 3 4 5
Swap
35 77
77 42 35 12 101 5
17
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
0 1 2 3 4 5
5
12
77
35
42 101
12 77
77 42 35 12 101 5
18
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
77
12
35
42 101
No need to swap
77 42 35 12 101 5
19
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
77
12
35
42 101
0 1 2 3 4 5
5 101
77 42 35 12 101 5
20
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
0 1 2 3 4 5
77
12
35
42 5 101
Largest value correctly placed
77 42 35 12 101 5
21
Types of Sorting algorithms
0 4
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
Bubble Sort
22
Types of Sorting algorithms
0 4
Bubble Sort
a = [35, 10, 31, 11, 26]
print("Before sorting array elements are - ")
for i in a:
print(i, end = " ")
for i in range(0,len(a)):
for j in range(i+1,len(a)):
if a[j]<a[i]:
temp = a[j]
a[j]=a[i]
a[i]=temp
print("nAfter sorting array elements are - ")
for i in a:
print(i, end = " ")
23
Types of Sorting algorithms
0 4
No it is your turn.
Bubble Sort
9 7 3 0 1 7 10
Sort these elements using bubble
sort algorithm
24
Types of Sorting algorithms
0 4
 Insertion sort is the sorting mechanism where the sorted array is built
having one item at a time.
 The array elements are compared with each other sequentially
and then arranged simultaneously in some particular order.
 The idea of the insertion sort is similar to the idea of sorting the
playing cards.
Insertion Sort
25
Types of Sorting algorithms
0 4
• The array of values to be sorted is divided into two sets. One that stores sorted
values and another that contains unsorted values.
• The sorting algorithm will proceed until there are elements in the unsorted set.
• Suppose there are n elements in the array. Initially, the element with index 0
(assuming LB = 0) is in the sorted set. Rest of the elements are in the unsorted set.
• The first element of the unsorted partition has array index 1 (if LB = 0).
• During each iteration of the algorithm, the first element in the unsorted set is picked
up and inserted into the correct position in the sorted set.
Insertion Sort
26
Types of Sorting algorithms
0 4
• Insertion sort algorithm divides the list into two parts, sorted and unsorted.
• initially sorted list contain only one element.
• in each pass, one element from the unsorted list is inserted at its correct position in
sorted list.
• Consider an unsorted list in an array.
Insertion Sort
27
Types of Sorting algorithms
0 4
Insertion Sort
28
Types of Sorting algorithms
0 4
Insertion Sort
29
Types of Sorting algorithms
0 4
Insertion Sort
Example 2
30
Types of Sorting algorithms
0 4
Insertion Sort
Example 3
30
31
Types of Sorting algorithms
0 4
Insertion Sort
Example 4
32
Types of Sorting algorithms
0 4
Insertion Sort INSERTION-SORT(A)
for i = 1 to n
key A [i]
←
j i
← – 1
while j > = 0 and A[j] > key
A[j+1] A[j]
←
j j
← – 1
End while
A[j+1] key
←
End for
33
Types of Sorting algorithms
0 4
Insertion Sort #Function to do insertion sort
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-1], that are greater than key, to one
position ahead of their current position
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Driver code to test above
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print(arr[i])
Python program for implementation of Insertion Sort
34
Types of Sorting algorithms
0 4
Insertion Sort
12 31 3 5 1 7 25 8 17 2
Sort these elements using insertion sort
algorithm
35
Types of Sorting algorithms
0 4
• Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which the list is divided into two parts, the
sorted part at the left end and the unsorted part at the right end. Initially, the
sorted part is empty and the unsorted part is the entire list.
• The smallest element is selected from the unsorted array and swapped with
the leftmost element, and that element becomes a part of the sorted array.
• This process continues moving unsorted array boundary by one element to
the right.
Selection sort
36
Types of Sorting algorithms
0 4
1. Find the smallest element in the array and swap it with the first element
of the array i.e. a[0].
2. The elements left for sorting are n-1 so far. Find the smallest element in
the array from index 1 to n-1 i.e. a[1] to a[n-1] and swap it with a[1].
3. Continue this process for all the elements in the array until we get a
sorted list.
Selection sort
37
Types of Sorting algorithms
0 4
Selection sort
 The picture shows an array of
six integers that we want to
sort from smallest to largest.
 Start by finding the smallest
entry.
37
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
38
Types of Sorting algorithms
0 4
Selection sort
 Start by finding the smallest entry.
 Swap the smallest entry with the first
entry.
38
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
39
Types of Sorting algorithms
0 4
Selection sort
 Start by finding the smallest entry.
 Swap the smallest entry with the first
entry.
39
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
40
Types of Sorting algorithms
0 4
Selection sort
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
 Find the smallest element in the unsorted side.
 Swap with the front of the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
41
Types of Sorting algorithms
0 4
Selection sort
 The process continues.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
Smallest
from
unsorted
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
42
Types of Sorting algorithms
0 4
Selection sort
 The process continues.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
Sorted side
is bigger
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
43
Types of Sorting algorithms
0 4
Selection sort
 We can stop when the unsorted side has just one number, since that number
must be the largest number.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
The array is now sorted.
We repeatedly
selected the smallest
element, and moved
this element to the
front of the unsorted
side.
44
Types of Sorting algorithms
0 4
Selection sort
1
3
2
9
6
4
8
8
3
2
9
6
4
1
8
3
4
9
6
2
1
8
6
4
9
3
2
1
8
9
6
4
3
2
1
8
6
9
4
3
2
1
9
8
6
4
3
2
1
9
8
6
4
3
2
1
Example
45
Types of Sorting algorithms
0 4
Selection sort
for (i = 0; i < n-1; i++)
{
small = i; //minimum element in unsorted array
for (j = i+1; j < n; j++) {
if (arr[j] < arr[small])
small = j;
// Swap the minimum element with the first element
temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
46
Types of Sorting algorithms
0 4
Selection sort Python program for implementation of selection Sort
def selectionSort(array, size):
for step in range(size):
min_idx = step
for i in range(step + 1, size):
# to sort in descending order, change > to < in this line
# select the minimum element in each loop
if array[i] < array[min_idx]:
min_idx = i
# put min at the correct position
(array[step], array[min_idx]) = (array[min_idx], array[step])
data = [64, 25, 12, 22, 11]
size = len(data)
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)
47
Types of Sorting algorithms
0 4
Selection sort
12 31 3 5 1 7 25 8 17 2
Sort these elements using selection sort
algorithm
48
Linear Search
0 5
• The simplest solution to the sequence search problem is the
sequential or linear
search algorithm.
• This technique iterates over the sequence, one item at a
time, until the specific item is found or all items have been
examined.
49
Linear Search
0 5
Linear Search Algorithm
procedure LINEAR_SEARCH (array, key)
for each item in the array
if match element == key
return element's index
end if
end for
end procedure
50
Linear Search
0 5
Best Case Analysis:
• In the best case analysis, we calculate the lower bound of the execution
time of an algorithm. It is necessary to know the case which causes the
execution of the minimum number of operations. In the linear search
problem, the best case occurs when x is present at the first location.
• Best Case − Minimum time required for program execution.
Case Time Complexity
51
Linear Search
0 5
Average Case Analysis:
• In the average case analysis, we take all possible inputs and calculate
the computation time for all inputs. Add up all the calculated values ​
​
and
divide the sum by the total number of entries.
• Average Case − Average time required for program execution.
Case Time Complexity
52
Linear Search
0 5
Worst Case Analysis:
• In the worst-case analysis, we calculate the upper limit of the execution
time of an algorithm. It is necessary to know the case which causes the
execution of the maximum number of operations.
• Worst Case − Maximum time required for program execution.
Case Time Complexity
53
Comparing the Simple Sorts
0 5
• Bubble Sort – simplest.
• Use only if you don’t have other algorithms available and ‘n’ is small.
• Selection Sort
• Minimizes number of swaps, but number of comparisons still high.
• Useful when amount of data is small and swapping is very time consuming – like when sorting
records in tables – internal sorts.
• Insertion Sort
• The most versatile, and is usually best bet in most situations, if amount of data is small or data is
almost sorted. For large n, other sorts are better. We will cover advanced sorts later.
• All require very little space and they sort in place.
• Can’t see the real efficiencies / differences unless you apply the different sources to large
amounts of data.
54
END

More Related Content

PPTX
Unit 7 sorting
PPTX
Unit vii sorting
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPT
Chapter 11 - Sorting and Searching
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
PPTX
Algorithm & data structures lec4&5
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
PPTX
Searching and sorting Techniques in Data structures
Unit 7 sorting
Unit vii sorting
Searching Sorting-SELECTION ,BUBBBLE.ppt
Chapter 11 - Sorting and Searching
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
Algorithm & data structures lec4&5
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Searching and sorting Techniques in Data structures

Similar to DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx (20)

PPTX
1.4 Sorting.pptx
PPTX
Chapter 2. data structure and algorithm
PPTX
Rahat &amp; juhith
PDF
advanced searching and sorting.pdf
PPTX
sorting-160810203705.pptx
PPTX
data_structure_Chapter two_computer.pptx
PPTX
sorting-160810203705.pptx
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
PPTX
Insertion Sorting
PPT
Sorting
PPTX
Searching searching in in arrays arrays.pptx
PPTX
Searching Algorithms - Foundations of Algorithms
PPTX
Lecture_Oct26.pptx
PPTX
SORTING techniques.pptx
PPTX
PPTX
Data structure.pptx
PPTX
Data Structures Unit 2 FINAL presentation.pptx
PPTX
what is sorting algorithm and implementation.pptx
PPT
Sorting algorithums > Data Structures & Algorithums
1.4 Sorting.pptx
Chapter 2. data structure and algorithm
Rahat &amp; juhith
advanced searching and sorting.pdf
sorting-160810203705.pptx
data_structure_Chapter two_computer.pptx
sorting-160810203705.pptx
Chapter 2 Sorting and Searching .pptx.soft
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Insertion Sorting
Sorting
Searching searching in in arrays arrays.pptx
Searching Algorithms - Foundations of Algorithms
Lecture_Oct26.pptx
SORTING techniques.pptx
Data structure.pptx
Data Structures Unit 2 FINAL presentation.pptx
what is sorting algorithm and implementation.pptx
Sorting algorithums > Data Structures & Algorithums
Ad

Recently uploaded (20)

PPTX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
PPTX
LIFE ORIENTATION SLIDES 2025 Grade 11.pptx
PDF
esg-supply-chain-webinar-nov2018hkhkkh.pdf
PPTX
internship presentation of bsnl in colllege
PPTX
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PDF
Prostaglandin E2.pdf orthoodontics op kharbanda
PPTX
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PDF
RIBOSOMES.12.pdf kerala msc botany degree
PDF
HR Jobs in Jaipur: 2025 Trends, Banking Careers & Smart Hiring Tools
PPTX
jinsha and arif-2.pptx blood factors and diseases
DOCX
How to Become a Criminal Profiler or Behavioural Analyst.docx
PPTX
arif og 2.pptx defence mechanism of gingiva
PPTX
OCCULAR MANIFESTATIONS IN LEPROSY.pptx bbb
PPTX
engineeringAlumni Meet 2025 -Principal.pptx
PPTX
Public_Health_Informghiufdrgatics_PPT.pptx
PPT
Leadership essentials to build your carrier
PPTX
Life Skills Stress_Management_Presentation.pptx
PPTX
CORE 1 HOUSEKEEPING TOURISM SECTOR POWERPOINT
PPTX
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
LIFE ORIENTATION SLIDES 2025 Grade 11.pptx
esg-supply-chain-webinar-nov2018hkhkkh.pdf
internship presentation of bsnl in colllege
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
L-0018048598visual cloud book for PCa-pdf.pdf
Prostaglandin E2.pdf orthoodontics op kharbanda
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
RIBOSOMES.12.pdf kerala msc botany degree
HR Jobs in Jaipur: 2025 Trends, Banking Careers & Smart Hiring Tools
jinsha and arif-2.pptx blood factors and diseases
How to Become a Criminal Profiler or Behavioural Analyst.docx
arif og 2.pptx defence mechanism of gingiva
OCCULAR MANIFESTATIONS IN LEPROSY.pptx bbb
engineeringAlumni Meet 2025 -Principal.pptx
Public_Health_Informghiufdrgatics_PPT.pptx
Leadership essentials to build your carrier
Life Skills Stress_Management_Presentation.pptx
CORE 1 HOUSEKEEPING TOURISM SECTOR POWERPOINT
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
Ad

DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx

  • 1. Chapter 3 BASIC SORTING ALGORITHMS 2 0 2 4 - 2 0 2 5 S p r i n g S e m e s t e r
  • 2. 2 Contents 0 0 • Introduction • Why Studying Sorting • Sorting Categories • Types of Sorting  Bubble Sort  Insertion Sort  Selection Sort • Linear Searching
  • 3. 3 Introduction 0 1 • Sorting means arranging the elements of an array so that they are placed in some relevant order which may be either ascending or descending. • sorting is also used to represent data in a more readable format. Some real-life examples of sorting are:-  Contact List in Your Mobile Phone also contains all contacts arranged alphabetically (lexicographically). So if you look for contact then you don’t have to look randomly and can be searched easily and many others like Apps on your phone.  Keywords in Your book are also in a lexicographical manner and you can find it according to Chapter.
  • 4. 4 Why Study Sorting? 0 2 • When you perform sorting on an array/elements, many problems become easy (e.g. min/max, kth smallest/largest) • Performing Sorting also givesnumber of algorithmic solutions that contain many other ideas such as: o Iterative o Divide-and-conquer o Comparison vs non-comparison based o Recursive
  • 5. 5 Why Study Sorting? 0 2  The main advantage of sorting is time complexity and that’s the most important thing when you solve a problem because it’s not enough you’re able to solve a problem but you should be able to solve it in the minimum time possible.  Sometimes problems can be solved easily and quickly based on sorting which can prevent you from every Coder’s Nightmare i.e. TLE (Time Limit Exceeded).
  • 6. 6 Sorting Categories 0 3 There are two different categories in sorting: 1. Internal sorting: If the input data is such that it can be adjusted in the main memory at once, it is called internal sorting. 2. External sorting: If the input data is such that it cannot be adjusted in the memory entirely at once, it needs to be stored in a hard disk, floppy disk, or any other storage device. This is called external sorting.
  • 7. 7 Types of Sorting algorithms 0 4 Although there is number of sorting algorithms best algorithm is which can solve a problem in the minimum time and minimum space required to do so. Some types of the Sorting algorithm are:- 1. Bubble sort 2. Insertion Sort 3. Select Sort
  • 8. 8 Types of Sorting algorithms 0 4  It’s one of the simplest algorithms which repeatedly iterates through the list, compares the elements next to each other, and swaps according to the condition passed to them.  This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order.  This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2 ) where n is the number of items. Bubble Sort
  • 9. 9 Types of Sorting algorithms 0 4 Bubble Sort
  • 10. 10 Types of Sorting algorithms 0 4 Repeat “bubble up” how many times? • If we have N elements • And if each time we bubble an element, we place it in its correct location….. • Then we repeat the “bubble up” process N-1 times. • This guarantees we’ll correctly place all N elements. Bubble Sort
  • 11. 11 Types of Sorting algorithms 0 4 Example 1 : 5, 12, 3, 9, 16 Pass 1 ● 5, 12, 3, 9, 16 ○ The list stays the same because 5 is less than 12. ● 5, 3, 12, 9, 16 ○ 3 and 12 are switched because 3 is less than 12 ● 5, 3, 9, 12, 16 ○ 9 and 12 are switched since 9 is less than 12 ● 5, 3, 9, 12, 16 ○ 12 and 16 do not switch because 12 is less than 16 Bubble Sort
  • 12. 12 Types of Sorting algorithms 0 4 Example 1 : 5, 12, 3, 9, 16 Pass 2 ● 3, 5, 9, 12, 16 ○ 3 is less than 5, so they switch ● 3, 5, 9, 12, 16 ○ 5 is less than 9 so they remain in the same places ● 3, 5, 9, 12, 16 ○ 12 is greater than 9 so they do not switch places ● 3, 5, 9, 12, 16 ○ 12 and 16 are in numerical order so they don't switch Bubble Sort
  • 13. 13 Types of Sorting algorithms 0 4 Example 2 Bubble Sort 13 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 0 1 2 3 4 5
  • 14. 14 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 5 12 35 42 77 101 0 1 2 3 4 5 77 42 35 12 101 5
  • 15. 15 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 5 12 35 42 77 101 0 1 2 3 4 5 Swap 42 77 77 42 35 12 101 5
  • 16. 16 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 5 12 35 77 42 101 0 1 2 3 4 5 Swap 35 77 77 42 35 12 101 5
  • 17. 17 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 0 1 2 3 4 5 5 12 77 35 42 101 12 77 77 42 35 12 101 5
  • 18. 18 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 5 77 12 35 42 101 No need to swap 77 42 35 12 101 5
  • 19. 19 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 5 77 12 35 42 101 0 1 2 3 4 5 5 101 77 42 35 12 101 5
  • 20. 20 Types of Sorting algorithms 0 4 Example 2 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Bubble Sort 0 1 2 3 4 5 77 12 35 42 5 101 Largest value correctly placed 77 42 35 12 101 5
  • 21. 21 Types of Sorting algorithms 0 4 for (i = 0; i < n; i++) { for (j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } Bubble Sort
  • 22. 22 Types of Sorting algorithms 0 4 Bubble Sort a = [35, 10, 31, 11, 26] print("Before sorting array elements are - ") for i in a: print(i, end = " ") for i in range(0,len(a)): for j in range(i+1,len(a)): if a[j]<a[i]: temp = a[j] a[j]=a[i] a[i]=temp print("nAfter sorting array elements are - ") for i in a: print(i, end = " ")
  • 23. 23 Types of Sorting algorithms 0 4 No it is your turn. Bubble Sort 9 7 3 0 1 7 10 Sort these elements using bubble sort algorithm
  • 24. 24 Types of Sorting algorithms 0 4  Insertion sort is the sorting mechanism where the sorted array is built having one item at a time.  The array elements are compared with each other sequentially and then arranged simultaneously in some particular order.  The idea of the insertion sort is similar to the idea of sorting the playing cards. Insertion Sort
  • 25. 25 Types of Sorting algorithms 0 4 • The array of values to be sorted is divided into two sets. One that stores sorted values and another that contains unsorted values. • The sorting algorithm will proceed until there are elements in the unsorted set. • Suppose there are n elements in the array. Initially, the element with index 0 (assuming LB = 0) is in the sorted set. Rest of the elements are in the unsorted set. • The first element of the unsorted partition has array index 1 (if LB = 0). • During each iteration of the algorithm, the first element in the unsorted set is picked up and inserted into the correct position in the sorted set. Insertion Sort
  • 26. 26 Types of Sorting algorithms 0 4 • Insertion sort algorithm divides the list into two parts, sorted and unsorted. • initially sorted list contain only one element. • in each pass, one element from the unsorted list is inserted at its correct position in sorted list. • Consider an unsorted list in an array. Insertion Sort
  • 27. 27 Types of Sorting algorithms 0 4 Insertion Sort
  • 28. 28 Types of Sorting algorithms 0 4 Insertion Sort
  • 29. 29 Types of Sorting algorithms 0 4 Insertion Sort Example 2
  • 30. 30 Types of Sorting algorithms 0 4 Insertion Sort Example 3 30
  • 31. 31 Types of Sorting algorithms 0 4 Insertion Sort Example 4
  • 32. 32 Types of Sorting algorithms 0 4 Insertion Sort INSERTION-SORT(A) for i = 1 to n key A [i] ← j i ← – 1 while j > = 0 and A[j] > key A[j+1] A[j] ← j j ← – 1 End while A[j+1] key ← End for
  • 33. 33 Types of Sorting algorithms 0 4 Insertion Sort #Function to do insertion sort def insertionSort(arr): # Traverse through 1 to len(arr) for i in range(1, len(arr)): key = arr[i] # Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position j = i - 1 while j >= 0 and key < arr[j]: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key # Driver code to test above arr = [12, 11, 13, 5, 6] insertionSort(arr) print ("Sorted array is:") for i in range(len(arr)): print(arr[i]) Python program for implementation of Insertion Sort
  • 34. 34 Types of Sorting algorithms 0 4 Insertion Sort 12 31 3 5 1 7 25 8 17 2 Sort these elements using insertion sort algorithm
  • 35. 35 Types of Sorting algorithms 0 4 • Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. • The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. • This process continues moving unsorted array boundary by one element to the right. Selection sort
  • 36. 36 Types of Sorting algorithms 0 4 1. Find the smallest element in the array and swap it with the first element of the array i.e. a[0]. 2. The elements left for sorting are n-1 so far. Find the smallest element in the array from index 1 to n-1 i.e. a[1] to a[n-1] and swap it with a[1]. 3. Continue this process for all the elements in the array until we get a sorted list. Selection sort
  • 37. 37 Types of Sorting algorithms 0 4 Selection sort  The picture shows an array of six integers that we want to sort from smallest to largest.  Start by finding the smallest entry. 37 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 38. 38 Types of Sorting algorithms 0 4 Selection sort  Start by finding the smallest entry.  Swap the smallest entry with the first entry. 38 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 39. 39 Types of Sorting algorithms 0 4 Selection sort  Start by finding the smallest entry.  Swap the smallest entry with the first entry. 39 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 40. 40 Types of Sorting algorithms 0 4 Selection sort 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6]  Find the smallest element in the unsorted side.  Swap with the front of the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 41. 41 Types of Sorting algorithms 0 4 Selection sort  The process continues. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side Smallest from unsorted [0] [1] [2] [3] [4] [5] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side [0] [1] [2] [3] [4] [5] Swap with front
  • 42. 42 Types of Sorting algorithms 0 4 Selection sort  The process continues. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side Sorted side is bigger [0] [1] [2] [3] [4] [5] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 43. 43 Types of Sorting algorithms 0 4 Selection sort  We can stop when the unsorted side has just one number, since that number must be the largest number. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] The array is now sorted. We repeatedly selected the smallest element, and moved this element to the front of the unsorted side.
  • 44. 44 Types of Sorting algorithms 0 4 Selection sort 1 3 2 9 6 4 8 8 3 2 9 6 4 1 8 3 4 9 6 2 1 8 6 4 9 3 2 1 8 9 6 4 3 2 1 8 6 9 4 3 2 1 9 8 6 4 3 2 1 9 8 6 4 3 2 1 Example
  • 45. 45 Types of Sorting algorithms 0 4 Selection sort for (i = 0; i < n-1; i++) { small = i; //minimum element in unsorted array for (j = i+1; j < n; j++) { if (arr[j] < arr[small]) small = j; // Swap the minimum element with the first element temp = arr[small]; arr[small] = arr[i]; arr[i] = temp; }
  • 46. 46 Types of Sorting algorithms 0 4 Selection sort Python program for implementation of selection Sort def selectionSort(array, size): for step in range(size): min_idx = step for i in range(step + 1, size): # to sort in descending order, change > to < in this line # select the minimum element in each loop if array[i] < array[min_idx]: min_idx = i # put min at the correct position (array[step], array[min_idx]) = (array[min_idx], array[step]) data = [64, 25, 12, 22, 11] size = len(data) selectionSort(data, size) print('Sorted Array in Ascending Order:') print(data)
  • 47. 47 Types of Sorting algorithms 0 4 Selection sort 12 31 3 5 1 7 25 8 17 2 Sort these elements using selection sort algorithm
  • 48. 48 Linear Search 0 5 • The simplest solution to the sequence search problem is the sequential or linear search algorithm. • This technique iterates over the sequence, one item at a time, until the specific item is found or all items have been examined.
  • 49. 49 Linear Search 0 5 Linear Search Algorithm procedure LINEAR_SEARCH (array, key) for each item in the array if match element == key return element's index end if end for end procedure
  • 50. 50 Linear Search 0 5 Best Case Analysis: • In the best case analysis, we calculate the lower bound of the execution time of an algorithm. It is necessary to know the case which causes the execution of the minimum number of operations. In the linear search problem, the best case occurs when x is present at the first location. • Best Case − Minimum time required for program execution. Case Time Complexity
  • 51. 51 Linear Search 0 5 Average Case Analysis: • In the average case analysis, we take all possible inputs and calculate the computation time for all inputs. Add up all the calculated values ​ ​ and divide the sum by the total number of entries. • Average Case − Average time required for program execution. Case Time Complexity
  • 52. 52 Linear Search 0 5 Worst Case Analysis: • In the worst-case analysis, we calculate the upper limit of the execution time of an algorithm. It is necessary to know the case which causes the execution of the maximum number of operations. • Worst Case − Maximum time required for program execution. Case Time Complexity
  • 53. 53 Comparing the Simple Sorts 0 5 • Bubble Sort – simplest. • Use only if you don’t have other algorithms available and ‘n’ is small. • Selection Sort • Minimizes number of swaps, but number of comparisons still high. • Useful when amount of data is small and swapping is very time consuming – like when sorting records in tables – internal sorts. • Insertion Sort • The most versatile, and is usually best bet in most situations, if amount of data is small or data is almost sorted. For large n, other sorts are better. We will cover advanced sorts later. • All require very little space and they sort in place. • Can’t see the real efficiencies / differences unless you apply the different sources to large amounts of data.

Editor's Notes

  • #4: iterative definition: 1. doing something again and again, usually to improve A divide and conquer algorithm is a strategy of solving a large problem by breaking the problem it into smaller sub-problems,  involving doing or saying the same thing several times in order to produce a particular result… The process in which a function calls itself directly or indirectly is called recursion