SlideShare a Scribd company logo
 Chapter 13 presents several
common algorithms for
sorting an array of integers.
 Two slow but simple
algorithms are
Selectionsort and
Insertionsort.
 This presentation
demonstrates how the two
algorithms work.
Quadratic Sorting
Data Structures
and Other Objects
Using C++
Sorting an Array of Integers
 The picture
shows an
array of six
integers that
we want to
sort from
smallest to
largest
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 Start by
finding the
smallest
entry.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 Start by
finding the
smallest
entry.
 Swap the
smallest
entry with
the first
entry.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 Start by
finding the
smallest
entry.
 Swap the
smallest
entry with
the first
entry.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 Part of the
array is now
sorted.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 Find the
smallest
element in
the unsorted
side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 Find the
smallest
element in
the unsorted
side.
 Swap with
the front of
the unsorted
side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 We have
increased the
size of the
sorted side
by one
element.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 The process
continues...
Sorted side Unsorted side
Smallest
from
unsorted
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 The process
continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
 The process
continues...
Sorted side Unsorted side
Sorted side
is bigger
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort 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]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort 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
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort 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]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
 The
Insertionsort
algorithm
also views
the array as
having a
sorted side
and an
unsorted
side. [0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
 The sorted
side starts
with just the
first
element,
which is not
necessarily
the smallest
element. [1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
 The sorted
side grows
by taking the
front
element
from the
unsorted
side...
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
 ...and
inserting it
in the place
that keeps
the sorted
side
arranged
from small
to large. [1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
 In this
example, the
new element
goes in front
of the
element that
was already
in the sorted
side. [1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
 Sometimes
we are lucky
and the new
inserted item
doesn't need
to move at
all.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[1] [2] [3] [4] [5] [6]
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.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[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
to a separate
location.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
3] [4] [5] [6]
3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
[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.
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
3] [4] [5] [6]
3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
3] [4] [5] [6]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[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]
3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted sid
[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
3] [4] [5] [6]
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
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
A Quiz
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How many
shifts will
occur before we
copy this
element back
into the array?
3] [4] [5] [6]
3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
A Quiz
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
3] [4] [5] [6]
3] [4] [5] [6]
 Four items
are shifted.
[0] [1] [2] [3] [4] [5]
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
A Quiz
3] [4] [5] [6]
3] [4] [5] [6]
 Four items
are shifted.
And then
the element is
copied back
into the array.
[0] [1] [2] [3] [4] [5]
 Both Selectionsort and Insertionsort have a worst-
case time of O(n2), making them impractical for
large arrays.
 But they are easy to program, easy to debug.
 Insertionsort also has good performance when the
array is nearly sorted to begin with.
 But more sophisticated sorting algorithms are
needed when good performance is needed in all
cases for large arrays.
Timing and Other Issues
THE END
Presentation copyright 2010 Addison Wesley Longman,
For use with Data Structures and Other Objects Using C++
by Michael Main.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright
Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club
Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using Java are welcome
to use this presentation however they see fit, so long as this copyright notice remains
intact.

More Related Content

PPT
Sorting of linked list data through python.ppt
PPTX
Sorting Algorithms
PPTX
Data Structures_Searching and Sorting.pptx
PPT
AA_Sorting.SI.ppt
PPT
Sorting
PDF
Quick sort and binary search PDF
PPTX
Counting Sort
PPT
Merge sort and Quick sort
Sorting of linked list data through python.ppt
Sorting Algorithms
Data Structures_Searching and Sorting.pptx
AA_Sorting.SI.ppt
Sorting
Quick sort and binary search PDF
Counting Sort
Merge sort and Quick sort

Similar to DSSchapt13.ppt (20)

PPT
Quick Sort- Marge Sort.ppt
PPT
Quicksort
PPT
Quicksort and MergeSort Algorithm Analysis
PPT
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
PPT
quicksort (1).ppt
PPT
quicksort.ppt
PPT
Quick & Merge Sort.ppt
PPT
quicksort.ppt
PPT
quicksort.ppt
PPT
3.8 quicksort 04
PPTX
Unit 5 internal sorting & files
PPTX
Hash table
PDF
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
PPT
quick_sort.ppt
PPTX
Sorting_With_Array_Introduction_Presentation.pptx
PDF
第2回 基本演算,データ型の基礎,ベクトルの操作方法
PPTX
Quicksort algorithm
KEY
Take & Drop (MOTM 2010.04)
PPT
Lec-12, 13 Quees - Circular Queues and Implementation with Array
PDF
Algebra Lineal con geogebra.pdf
Quick Sort- Marge Sort.ppt
Quicksort
Quicksort and MergeSort Algorithm Analysis
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
quicksort (1).ppt
quicksort.ppt
Quick & Merge Sort.ppt
quicksort.ppt
quicksort.ppt
3.8 quicksort 04
Unit 5 internal sorting & files
Hash table
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
quick_sort.ppt
Sorting_With_Array_Introduction_Presentation.pptx
第2回 基本演算,データ型の基礎,ベクトルの操作方法
Quicksort algorithm
Take & Drop (MOTM 2010.04)
Lec-12, 13 Quees - Circular Queues and Implementation with Array
Algebra Lineal con geogebra.pdf

Recently uploaded (20)

PPT
Chap8. Product & Service Strategy and branding
PDF
Driving Innovation & Growth, Scalable Startup IT Services That Deliver Result...
PPTX
TimeBee vs. Toggl: Which Time Tracking Tool is Best for You?
PDF
Captivating LED Visuals, Built to Impress Brightlink.pdf
PDF
4. Finance for non-financial managers.08.08.2025.pdf
PDF
Decision trees for high uncertainty decisions
PPT
Organizational Culture and Management.ppt
PDF
Chapter 1 - Introduction to management.pdf
PDF
Investment Risk Assessment Brief: Zacharia Ali and Associated Entities
PPTX
ENTREPRENEURSHIP..PPT.pptx..1234567891011
PPTX
Daily stand up meeting on the various business
PPTX
ELS-07 Lifeskills ToT PPt-Adama (ABE).pptx
PDF
initiate-entrepreneurship-in-healthcare-service-management-in-sierra-leone.pdf
PDF
Business Risk Assessment and Due Diligence Report: Zacharia Ali and Associate...
PDF
Why Has Vertical Farming Recently Become More Economical.pdf
PDF
Chapter 3 - Business environment - Final.pdf
PPT
chap9.New Product Development product lifecycle.ppt
PDF
Why DevOps Teams Are Dropping Spreadsheets for Real-Time Cloud Hygiene.pdf
PPTX
Transforming Finance with Ratiobox – Oracle NetSuite Bookkeeping & Accounting...
PDF
AI Cloud Sprawl Is Real—Here’s How CXOs Can Regain Control Before It Costs Mi...
Chap8. Product & Service Strategy and branding
Driving Innovation & Growth, Scalable Startup IT Services That Deliver Result...
TimeBee vs. Toggl: Which Time Tracking Tool is Best for You?
Captivating LED Visuals, Built to Impress Brightlink.pdf
4. Finance for non-financial managers.08.08.2025.pdf
Decision trees for high uncertainty decisions
Organizational Culture and Management.ppt
Chapter 1 - Introduction to management.pdf
Investment Risk Assessment Brief: Zacharia Ali and Associated Entities
ENTREPRENEURSHIP..PPT.pptx..1234567891011
Daily stand up meeting on the various business
ELS-07 Lifeskills ToT PPt-Adama (ABE).pptx
initiate-entrepreneurship-in-healthcare-service-management-in-sierra-leone.pdf
Business Risk Assessment and Due Diligence Report: Zacharia Ali and Associate...
Why Has Vertical Farming Recently Become More Economical.pdf
Chapter 3 - Business environment - Final.pdf
chap9.New Product Development product lifecycle.ppt
Why DevOps Teams Are Dropping Spreadsheets for Real-Time Cloud Hygiene.pdf
Transforming Finance with Ratiobox – Oracle NetSuite Bookkeeping & Accounting...
AI Cloud Sprawl Is Real—Here’s How CXOs Can Regain Control Before It Costs Mi...

DSSchapt13.ppt

  • 1.  Chapter 13 presents several common algorithms for sorting an array of integers.  Two slow but simple algorithms are Selectionsort and Insertionsort.  This presentation demonstrates how the two algorithms work. Quadratic Sorting Data Structures and Other Objects Using C++
  • 2. Sorting an Array of Integers  The picture shows an array of six integers that we want to sort from smallest to largest [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 3. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  Start by finding the smallest entry. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 4. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  Start by finding the smallest entry.  Swap the smallest entry with the first entry. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 5. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  Start by finding the smallest entry.  Swap the smallest entry with the first entry. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 6. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  Part of the array is now sorted. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 7. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 8. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  Find the smallest element in the unsorted side.  Swap with the front of the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 9. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  We have increased the size of the sorted side by one element. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 10. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  The process continues... Sorted side Unsorted side Smallest from unsorted [0] [1] [2] [3] [4] [5]
  • 11. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5]
  • 12. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort Algorithm  The process continues... Sorted side Unsorted side Sorted side is bigger [0] [1] [2] [3] [4] [5]
  • 13. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort 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]
  • 14. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort 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
  • 15. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Selectionsort 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]
  • 16. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm  The Insertionsort algorithm also views the array as having a sorted side and an unsorted side. [0] [1] [2] [3] [4] [5]
  • 17. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm  The sorted side starts with just the first element, which is not necessarily the smallest element. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 18. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm  The sorted side grows by taking the front element from the unsorted side... [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 19. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm  ...and inserting it in the place that keeps the sorted side arranged from small to large. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 20. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm  In this example, the new element goes in front of the element that was already in the sorted side. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 21. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] The Insertionsort Algorithm  Sometimes we are lucky and the new inserted item doesn't need to move at all. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 22. [1] [2] [3] [4] [5] [6] 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. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 23. [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 to a separate location. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 3] [4] [5] [6] 3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted side
  • 24. [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. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 3] [4] [5] [6] 3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 25. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [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] 3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 26. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [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] 3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 27. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [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] 3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 28. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [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] 3] [4] [5] [6] [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 29. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] [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] 3] [4] [5] [6] [0] [1] [2] [3] [4] [5] Sorted side Unsorted sid
  • 30. [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 3] [4] [5] [6] 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
  • 31. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] A Quiz [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] How many shifts will occur before we copy this element back into the array? 3] [4] [5] [6] 3] [4] [5] [6] [0] [1] [2] [3] [4] [5]
  • 32. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] A Quiz [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] 3] [4] [5] [6] 3] [4] [5] [6]  Four items are shifted. [0] [1] [2] [3] [4] [5]
  • 33. [1] [2] [3] [4] [5] [6] 0 10 20 30 40 50 60 70 [1] [2] [3] [4] [5] [6] A Quiz 3] [4] [5] [6] 3] [4] [5] [6]  Four items are shifted. And then the element is copied back into the array. [0] [1] [2] [3] [4] [5]
  • 34.  Both Selectionsort and Insertionsort have a worst- case time of O(n2), making them impractical for large arrays.  But they are easy to program, easy to debug.  Insertionsort also has good performance when the array is nearly sorted to begin with.  But more sophisticated sorting algorithms are needed when good performance is needed in all cases for large arrays. Timing and Other Issues
  • 35. THE END Presentation copyright 2010 Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using Java are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.