SlideShare a Scribd company logo
1 of 59
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

Searching.ppt
Searching.pptSearching.ppt
Searching.pptp83629918
 
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
 
Quick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.pptQuick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.pptAakritiGoyal7
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & filesDrkhanchanaR
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04Krish_ver2
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.pptGaganaP13
 
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.pdfssuser034ce1
 

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

Hash table
Hash tableHash table
Hash table
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
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...
 
Quick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.pptQuick Sort- Marge Sort.ppt
Quick Sort- Marge Sort.ppt
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & files
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
 
Quicksort
QuicksortQuicksort
Quicksort
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
 
quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.ppt
 
Quick & Merge Sort.ppt
Quick & Merge Sort.pptQuick & Merge Sort.ppt
Quick & Merge Sort.ppt
 
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
 

Recently uploaded

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

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

  1. 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.
  2. 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.
  3. Once we have found the smallest element, that element is swapped with the first element of the array...
  4. ...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.
  5. 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.
  6. 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.
  7. This small element is swapped with the number at the front of the unsorted side, as shown here...
  8. ...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.
  9. Again, we find the smallest entry in the unsorted side...
  10. ...and swap this element with the front of the unsorted side.
  11. The sorted side now contains the three smallest elements of the array.
  12. Here is the array after increasing the sorted side to four elements.
  13. 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...
  14. ...The array is sorted. The basic algorithm is easy to state and also easy to program.
  15. 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, ...
  16. ...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.
  17. The basic approach is to take the front element from the unsorted side...
  18. ...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.
  19. 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.
  20. 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.
  21. Sometimes we are lucky twice in a row.
  22. 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.
  23. 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...
  24. ...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...
  25. This is still not the correct spot for our new element, so we shift again...
  26. ...and shift one more time...
  27. 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.
  28. 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.
  29. The last element of the array also needs to be inserted. Start by copying it to a safe location.
  30. The new element is inserted into the array.
  31. 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, ...
  32. 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, ...
  33. 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, ...
  34. 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, ...
  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, ...