SlideShare a Scribd company logo
Sorting
Introduction
• Common problem: sort a list of values, starting
from lowest to highest.
– List of exam scores
– Words of dictionary in alphabetical order
– Students names listed alphabetically
– Student records sorted by ID#
• Generally, we are given a list of records that have
keys. These keys are used to define an ordering of
the items in the list.
Quadratic Sorting Algorithms
• We are given n records to sort.
• There are a number of simple sorting
algorithms whose worst and average case
performance is quadratic O(n2):
– Selection sort
– Insertion sort
– Bubble sort
Sorting an Array of Integers
• Example: we
are given an
array of six
integers that
we want to
sort from
smallest to
largest
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort Algorithm
• Start by
finding the
smallest
entry.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort Algorithm
• Swap the
smallest
entry with
the first
entry.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort Algorithm
• Swap the
smallest
entry with
the first
entry.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort Algorithm
• Part of the
array is now
sorted.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
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]
The Selection Sort Algorithm
• Find the
smallest
element in
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]
The Selection Sort Algorithm
• 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]
The Selection Sort Algorithm
• We have
increased the
size of the
sorted side
by one
element.
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]
The Selection Sort Algorithm
• The process
continues...
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]
The Selection Sort Algorithm
• The process
continues...
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]
The Selection Sort Algorithm
• The process
continues...
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]
The Selection Sort Algorithm
• The process
keeps adding
one more
number to the
sorted side.
• The sorted side
has the smallest
numbers,
arranged from
small to large.
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]
The Selection Sort Algorithm
• We can stop
when the
unsorted side
has just one
number, since
that number
must be the
largest number.
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted sid
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort Algorithm
• The array is
now sorted.
• We repeatedly
selected the
smallest
element, and
moved this
element to the
front of the
unsorted side. [0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertion Sort Algorithm
• The Insertion
Sort algorithm
also views the
array as having
a sorted side
and an
unsorted side.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertion Sort Algorithm
• The sorted
side starts
with just the
first
element,
which is not
necessarily
the smallest
element.
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]
The Insertion Sort Algorithm
• The sorted
side grows
by taking the
front
element
from the
unsorted
side... 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]
The Insertion Sort Algorithm
• ...and
inserting it
in the place
that keeps
the sorted
side
arranged
from small
to large.
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]
The Insertion Sort Algorithm
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]
The Insertion Sort Algorithm
• Sometimes
we are lucky
and the new
inserted item
doesn't need
to move at
all.
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]
The Insertionsort Algorithm
• Sometimes
we are lucky
twice in a
row.
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]
How to Insert One Element
Copy the
new element
to a separate
location.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
How to Insert One Element
Shift
elements in
the sorted
side,
creating an
open space
for the new
element.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[3] [4] [5] [6] [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]
How to Insert One Element
Shift
elements in
the sorted
side,
creating an
open space
for the new
element.
[3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
How to Insert One Element
Continue
shifting
elements...
[3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
How to Insert One Element
Continue
shifting
elements...
[3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
How to Insert One Element
...until you
reach the
location for
the new
element.
[3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
How to Insert One Element
Copy the
new element
back into the
array, at the
correct
location.
[3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
Sorted side Unsorted sid
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
[2] [3] [4] [5] [6]
• The last
element
must also be
inserted.
Start by
copying it...
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted sid
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted Result
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Swap?
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Yes!
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Swap?
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Swap?
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Swap?
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Yes!
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Swap?
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm
looks at pairs
of entries in
the array, and
swaps their
order if
needed.
[0] [1] [2] [3] [4] [5]
Yes!
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Repeat.
[0] [1] [2] [3] [4] [5]
Swap? No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? Yes.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Loop over
array n-1
times,
swapping pairs
of entries as
needed.
[0] [1] [2] [3] [4] [5]
Swap? No.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Bubble Sort Algorithm
• Continue
looping, until
done.
[0] [1] [2] [3] [4] [5]
Swap? Yes.

More Related Content

Similar to Sorting of linked list data through python.ppt (18)

Merge sort and Quick sort
Merge sort and Quick sortMerge sort and Quick sort
Merge sort and Quick sort
Maitree Patel
 
Hash table
Hash tableHash table
Hash table
Hemant Chetwani
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
p83629918
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
rasheed747195
 
Searching inear Search And Binary Agorithms
Searching inear Search And Binary AgorithmsSearching inear Search And Binary Agorithms
Searching inear Search And Binary Agorithms
PuneetVashistha1
 
Quick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.pptQuick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.ppt
AakritiGoyal7
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & files
DrkhanchanaR
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
Krish_ver2
 
Quicksort
QuicksortQuicksort
Quicksort
maamir farooq
 
Quicksort and MergeSort Algorithm Analysis
Quicksort and MergeSort Algorithm AnalysisQuicksort and MergeSort Algorithm Analysis
Quicksort and MergeSort Algorithm Analysis
lionelmessi593584
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
AmrutaNavale2
 
Quick & Merge Sort.ppt
Quick & Merge Sort.pptQuick & Merge Sort.ppt
Quick & Merge Sort.ppt
MuhammadSheraz836877
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
qasimali7770272
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
DeepakM509554
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
GaganaP13
 
2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 
Merge sort and Quick sort
Merge sort and Quick sortMerge sort and Quick sort
Merge sort and Quick sort
Maitree Patel
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
p83629918
 
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
Searching Algorithms with Binary Search and Hashing Concept with Time and Spa...
mrhabib10
 
Searching inear Search And Binary Agorithms
Searching inear Search And Binary AgorithmsSearching inear Search And Binary Agorithms
Searching inear Search And Binary Agorithms
PuneetVashistha1
 
Quick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.pptQuick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.ppt
AakritiGoyal7
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & files
DrkhanchanaR
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
Krish_ver2
 
Quicksort and MergeSort Algorithm Analysis
Quicksort and MergeSort Algorithm AnalysisQuicksort and MergeSort Algorithm Analysis
Quicksort and MergeSort Algorithm Analysis
lionelmessi593584
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
GaganaP13
 
2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 

Recently uploaded (20)

Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18
Celine George
 
CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...
CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...
CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...
CANSA The Cancer Association of South Africa
 
Maslows Toolbox - Inclusive Classrooms.pptx
Maslows Toolbox - Inclusive Classrooms.pptxMaslows Toolbox - Inclusive Classrooms.pptx
Maslows Toolbox - Inclusive Classrooms.pptx
Pooky Knightsmith
 
Policies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptxPolicies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptx
mansk2
 
Management of head injury in children.pdf
Management of head injury in children.pdfManagement of head injury in children.pdf
Management of head injury in children.pdf
sachin7989
 
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the BoardThe Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
victoriamangiantini1
 
the dynastic history of Kalchuris of Tripuri
the dynastic history of Kalchuris of Tripurithe dynastic history of Kalchuris of Tripuri
the dynastic history of Kalchuris of Tripuri
PrachiSontakke5
 
NS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptxNS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptx
manuelaromero2013
 
Flower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdfFlower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdf
kushallamichhame
 
Leveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdf
Leveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdfLeveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdf
Leveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdf
TechSoup
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
Salinity Resistance in Plants.Rice plant
Salinity Resistance in Plants.Rice plantSalinity Resistance in Plants.Rice plant
Salinity Resistance in Plants.Rice plant
aliabatool11
 
How to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo SlidesHow to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo Slides
Celine George
 
Post Exam Fun(da)- a General under-25 quiz, Prelims and Finals
Post Exam Fun(da)- a General  under-25 quiz, Prelims and FinalsPost Exam Fun(da)- a General  under-25 quiz, Prelims and Finals
Post Exam Fun(da)- a General under-25 quiz, Prelims and Finals
Pragya - UEM Kolkata Quiz Club
 
Combustion in Compression Ignition Engine (CIE)
Combustion in Compression Ignition Engine (CIE)Combustion in Compression Ignition Engine (CIE)
Combustion in Compression Ignition Engine (CIE)
NileshKumbhar21
 
Product in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against YouProduct in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against You
victoriamangiantini1
 
NA FASE REGIONAL DO TL – 1.º CICLO. .
NA FASE REGIONAL DO TL – 1.º CICLO.     .NA FASE REGIONAL DO TL – 1.º CICLO.     .
NA FASE REGIONAL DO TL – 1.º CICLO. .
Colégio Santa Teresinha
 
From Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI StrategyFrom Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI Strategy
victoriamangiantini1
 
NS3 Unit 5 Matter changes presentation.pptx
NS3 Unit 5 Matter changes presentation.pptxNS3 Unit 5 Matter changes presentation.pptx
NS3 Unit 5 Matter changes presentation.pptx
manuelaromero2013
 
From Building Products to Owning the Business
From Building Products to Owning the BusinessFrom Building Products to Owning the Business
From Building Products to Owning the Business
victoriamangiantini1
 
Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18
Celine George
 
Maslows Toolbox - Inclusive Classrooms.pptx
Maslows Toolbox - Inclusive Classrooms.pptxMaslows Toolbox - Inclusive Classrooms.pptx
Maslows Toolbox - Inclusive Classrooms.pptx
Pooky Knightsmith
 
Policies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptxPolicies, procedures, subject selection and QTAC.pptx
Policies, procedures, subject selection and QTAC.pptx
mansk2
 
Management of head injury in children.pdf
Management of head injury in children.pdfManagement of head injury in children.pdf
Management of head injury in children.pdf
sachin7989
 
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the BoardThe Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
victoriamangiantini1
 
the dynastic history of Kalchuris of Tripuri
the dynastic history of Kalchuris of Tripurithe dynastic history of Kalchuris of Tripuri
the dynastic history of Kalchuris of Tripuri
PrachiSontakke5
 
NS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptxNS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptx
manuelaromero2013
 
Flower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdfFlower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdf
kushallamichhame
 
Leveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdf
Leveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdfLeveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdf
Leveraging AI to Streamline Operations for Nonprofits [05.20.2025].pdf
TechSoup
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
Salinity Resistance in Plants.Rice plant
Salinity Resistance in Plants.Rice plantSalinity Resistance in Plants.Rice plant
Salinity Resistance in Plants.Rice plant
aliabatool11
 
How to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo SlidesHow to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo Slides
Celine George
 
Post Exam Fun(da)- a General under-25 quiz, Prelims and Finals
Post Exam Fun(da)- a General  under-25 quiz, Prelims and FinalsPost Exam Fun(da)- a General  under-25 quiz, Prelims and Finals
Post Exam Fun(da)- a General under-25 quiz, Prelims and Finals
Pragya - UEM Kolkata Quiz Club
 
Combustion in Compression Ignition Engine (CIE)
Combustion in Compression Ignition Engine (CIE)Combustion in Compression Ignition Engine (CIE)
Combustion in Compression Ignition Engine (CIE)
NileshKumbhar21
 
Product in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against YouProduct in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against You
victoriamangiantini1
 
From Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI StrategyFrom Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI Strategy
victoriamangiantini1
 
NS3 Unit 5 Matter changes presentation.pptx
NS3 Unit 5 Matter changes presentation.pptxNS3 Unit 5 Matter changes presentation.pptx
NS3 Unit 5 Matter changes presentation.pptx
manuelaromero2013
 
From Building Products to Owning the Business
From Building Products to Owning the BusinessFrom Building Products to Owning the Business
From Building Products to Owning the Business
victoriamangiantini1
 

Sorting of linked list data through python.ppt

  • 2. Introduction • Common problem: sort a list of values, starting from lowest to highest. – List of exam scores – Words of dictionary in alphabetical order – Students names listed alphabetically – Student records sorted by ID# • Generally, we are given a list of records that have keys. These keys are used to define an ordering of the items in the list.
  • 3. Quadratic Sorting Algorithms • We are given n records to sort. • There are a number of simple sorting algorithms whose worst and average case performance is quadratic O(n2): – Selection sort – Insertion sort – Bubble sort
  • 4. Sorting an Array of Integers • Example: we are given an array of six integers that we want to sort from smallest to largest 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 5. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selection Sort Algorithm • Start by finding the smallest entry. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 6. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selection Sort Algorithm • Swap the smallest entry with the first entry. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 7. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selection Sort Algorithm • Swap the smallest entry with the first entry. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 8. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selection Sort Algorithm • Part of the array is now sorted. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 9. 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] The Selection Sort Algorithm • Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 10. 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] The Selection Sort Algorithm • Swap with the front of the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 11. 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] The Selection Sort Algorithm • We have increased the size of the sorted side by one element. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 12. 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] The Selection Sort Algorithm • The process continues... Sorted side Unsorted side Smallest from unsorted [0] [1] [2] [3] [4] [5]
  • 13. 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] The Selection Sort Algorithm • The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 14. 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] The Selection Sort Algorithm • The process continues... Sorted side Unsorted side Sorted side is bigger [0] [1] [2] [3] [4] [5]
  • 15. 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] The Selection Sort Algorithm • The process keeps adding one more number to the sorted side. • The sorted side has the smallest numbers, arranged from small to large. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 16. 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] The Selection Sort Algorithm • We can stop when the unsorted side has just one number, since that number must be the largest number. [0] [1] [2] [3] [4] [5] Sorted side Unsorted sid
  • 17. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selection Sort Algorithm • The array is now sorted. • We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0] [1] [2] [3] [4] [5]
  • 18. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertion Sort Algorithm • The Insertion Sort algorithm also views the array as having a sorted side and an unsorted side. [0] [1] [2] [3] [4] [5]
  • 19. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertion Sort Algorithm • The sorted side starts with just the first element, which is not necessarily the smallest element. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 20. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertion Sort Algorithm • The sorted side grows by taking the front element from the unsorted side... 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 21. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertion Sort Algorithm • ...and inserting it in the place that keeps the sorted side arranged from small to large. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 22. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertion Sort Algorithm 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 23. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertion Sort Algorithm • Sometimes we are lucky and the new inserted item doesn't need to move at all. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 24. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm • Sometimes we are lucky twice in a row. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 25. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] How to Insert One Element Copy the new element to a separate location. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 26. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] How to Insert One Element Shift elements in the sorted side, creating an open space for the new element. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 27. 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] How to Insert One Element Shift elements in the sorted side, creating an open space for the new element. [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 28. 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] How to Insert One Element Continue shifting elements... [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 29. 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] How to Insert One Element Continue shifting elements... [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 30. 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] How to Insert One Element ...until you reach the location for the new element. [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 31. 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] How to Insert One Element Copy the new element back into the array, at the correct location. [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted sid
  • 32. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] How to Insert One Element [2] [3] [4] [5] [6] • The last element must also be inserted. Start by copying it... [0] [1] [2] [3] [4] [5] Sorted side Unsorted sid
  • 33. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted Result [0] [1] [2] [3] [4] [5]
  • 34. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]
  • 35. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?
  • 36. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Yes!
  • 37. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?
  • 38. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] No.
  • 39. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?
  • 40. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] No.
  • 41. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?
  • 42. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Yes!
  • 43. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?
  • 44. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Yes!
  • 45. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? No.
  • 46. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? No.
  • 47. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 48. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 49. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 50. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 51. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Repeat. [0] [1] [2] [3] [4] [5] Swap? No.
  • 52. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? No.
  • 53. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 54. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 55. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 56. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.
  • 57. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? No.
  • 58. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? No.
  • 59. 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Bubble Sort Algorithm • Continue looping, until done. [0] [1] [2] [3] [4] [5] Swap? Yes.

Editor's Notes

  • #5: The picture shows a graphical representation of an array which we will sort so that the smallest element ends up at the front, and the other elements increase to the largest at the end. The bar graph indicates the values which are in the array before sorting--for example the first element of the array contains the integer 45.
  • #6: The first sorting algorithm that we'll examine is called Selectionsort. It begins by going through the entire array and finding the smallest element. In this example, the smallest element is the number 8 at location [4] of the array.
  • #7: Once we have found the smallest element, that element is swapped with the first element of the array...
  • #8: ...like this. The smallest element is now at the front of the array, and we have taken one small step toward producing a sorted array.
  • #9: At this point, we can view the array as being split into two sides: To the left of the dotted line is the "sorted side", and to the right of the dotted line is the "unsorted side". Our goal is to push the dotted line forward, increasing the number of elements in the sorted side, until the entire array is sorted.
  • #10: Each step of the Selectionsort works by finding the smallest element in the unsorted side. At this point, we would find the number 15 at location [5] in the unsorted side.
  • #11: This small element is swapped with the number at the front of the unsorted side, as shown here...
  • #12: ...and the effect is to increase the size of the sorted side by one element. As you can see, the sorted side always contains the smallest numbers, and those numbers are sorted from small to large. The unsorted side contains the rest of the numbers, and those numbers are in no particular order.
  • #13: Again, we find the smallest entry in the unsorted side...
  • #14: ...and swap this element with the front of the unsorted side.
  • #15: The sorted side now contains the three smallest elements of the array.
  • #16: Here is the array after increasing the sorted side to four elements.
  • #17: And now the sorted side has five elements. In fact, once the unsorted side is down to a single element, the sort is completed. At this point the 5 smallest elements are in the sorted side, and so the the one largest element is left in the unsorted side. We are done...
  • #18: ...The array is sorted. The basic algorithm is easy to state and also easy to program.
  • #19: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #20: ...like this. However, in the Selectionsort, the sorted side always contained the smallest elements of the array. In the Insertionsort, the sorted side will be sorted from small to large, but the elements in the sorted side will not necessarily be the smallest entries of the array. Because the sorted side does not need to have the smallest entries, we can start by placing one element in the sorted side--we don't need to worry about sorting just one element. But we do need to worry about how to increase the number of elements that are in the sorted side.
  • #21: The basic approach is to take the front element from the unsorted side...
  • #22: ...and insert this element at the correct spot of the sorted side. In this example, the front element of the unsorted side is 20. So the 20 must be inserted before the number 45 which is already in the sorted side.
  • #23: After the insertion, the sorted side contains two elements. These two elements are in order from small to large, although they are not the smallest elements in the array.
  • #24: Sometimes we are lucky and the newly inserted element is already in the right spot. This happens if the new element is larger than anything that's already in the array.
  • #25: Sometimes we are lucky twice in a row.
  • #26: The actual insertion process requires a bit of work that is shown here. The first step of the insertion is to make a copy of the new element. Usually this copy is stored in a local variable. It just sits off to the side, ready for us to use whenever we need it.
  • #27: After we have safely made a copy of the new element, we start shifting elements from the end of the sorted side. These elements are shifted rightward, to create an "empty spot" for our new element to be placed. In this example we take the last element of the sorted side and shift it rightward one spot...
  • #28: ...like this. Is this the correct spot for the new element? No, because the new element is smaller than the next element in the sorted section. So we continue shifting elements rightward...
  • #29: This is still not the correct spot for our new element, so we shift again...
  • #30: ...and shift one more time...
  • #31: Finally, this is the correct location for the new element. In general there are two situations that indicate the "correct location" has been found: 1. We reach the front of the array (as happened here), or 2. We reached an element that is less than or equal to the new element.
  • #32: Once the correct spot is found, we copy the new element back into the array. The number of elements in the sorted side has increased by one.
  • #33: The last element of the array also needs to be inserted. Start by copying it to a safe location.
  • #34: The new element is inserted into the array.
  • #35: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #36: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #37: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #38: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #39: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #40: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #41: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #42: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #43: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #44: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #45: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #46: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #47: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #48: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #49: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #50: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #51: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #52: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #53: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #54: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #55: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #56: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #57: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #58: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #59: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...
  • #60: Now we'll look at another sorting method called Insertionsort. The end result will be the same: The array will be sorted from smallest to largest. But the sorting method is different. However, there are some common features. As with the Selectionsort, the Insertionsort algorithm also views the array as having a sorted side and an unsorted side, ...