SlideShare ist ein Scribd-Unternehmen logo
1 von 35
 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.

Weitere ähnliche Inhalte

Ähnlich wie DSSchapt13.ppt

quicksort.ppt
quicksort.pptquicksort.ppt
quicksort.pptGaganaP13
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04Krish_ver2
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & filesDrkhanchanaR
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)Wataru Shito
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法Wataru Shito
 
Quicksort algorithm
Quicksort algorithmQuicksort algorithm
Quicksort algorithmBapan Maity
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Kevin Munc
 
Algebra Lineal con geogebra.pdf
Algebra Lineal con geogebra.pdfAlgebra Lineal con geogebra.pdf
Algebra Lineal con geogebra.pdfJesus Fuentes
 
Taller final matematicas ii
Taller final matematicas iiTaller final matematicas ii
Taller final matematicas iiDaniellejo123
 

Ähnlich wie DSSchapt13.ppt (16)

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
 
3.8 quicksort 04
3.8 quicksort 043.8 quicksort 04
3.8 quicksort 04
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & files
 
Hash table
Hash tableHash table
Hash table
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
第2回 基本演算,データ型の基礎,ベクトルの操作方法(解答付き)
 
quick_sort.ppt
quick_sort.pptquick_sort.ppt
quick_sort.ppt
 
第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法第2回 基本演算,データ型の基礎,ベクトルの操作方法
第2回 基本演算,データ型の基礎,ベクトルの操作方法
 
Quicksort algorithm
Quicksort algorithmQuicksort algorithm
Quicksort algorithm
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)
 
Algebra Lineal con geogebra.pdf
Algebra Lineal con geogebra.pdfAlgebra Lineal con geogebra.pdf
Algebra Lineal con geogebra.pdf
 
Taller final matematicas ii
Taller final matematicas iiTaller final matematicas ii
Taller final matematicas ii
 

Kürzlich hochgeladen

(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...
(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...
(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...Hot Call Girls In Sector 58 (Noida)
 
Cheap Rate ➥8448380779 ▻Call Girls In Sector 54 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 54 GurgaonCheap Rate ➥8448380779 ▻Call Girls In Sector 54 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 54 GurgaonDelhi Call girls
 
Cheap Rate ➥8448380779 ▻Call Girls In Sector 55 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 55 GurgaonCheap Rate ➥8448380779 ▻Call Girls In Sector 55 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 55 GurgaonDelhi Call girls
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...aditipandeya
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...aditipandeya
 
Cheap Rate ➥8448380779 ▻Call Girls In Sector 56 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 56 GurgaonCheap Rate ➥8448380779 ▻Call Girls In Sector 56 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 56 GurgaonDelhi Call girls
 
A STUDY ON EMPLOYEE MORALE AT ELGI EQUIPMENT ELIMITED
A STUDY ON EMPLOYEE MORALE AT ELGI  EQUIPMENT ELIMITEDA STUDY ON EMPLOYEE MORALE AT ELGI  EQUIPMENT ELIMITED
A STUDY ON EMPLOYEE MORALE AT ELGI EQUIPMENT ELIMITEDksanjai333
 
Call girls in Andheri with phone number 9892124323
Call girls in Andheri with phone number 9892124323Call girls in Andheri with phone number 9892124323
Call girls in Andheri with phone number 9892124323Pooja Nehwal
 
Top Call Girls In Indira Nagar Lucknow ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
Top Call Girls In Indira Nagar Lucknow ( Lucknow  ) 🔝 8923113531 🔝  Cash PaymentTop Call Girls In Indira Nagar Lucknow ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment
Top Call Girls In Indira Nagar Lucknow ( Lucknow ) 🔝 8923113531 🔝 Cash Paymentanilsa9823
 
Mumbai Call Girls Colaba Pooja WhatsApp 7738631006 💞 Full Night Enjoy
Mumbai Call Girls Colaba Pooja WhatsApp  7738631006  💞 Full Night EnjoyMumbai Call Girls Colaba Pooja WhatsApp  7738631006  💞 Full Night Enjoy
Mumbai Call Girls Colaba Pooja WhatsApp 7738631006 💞 Full Night EnjoyPooja Nehwal
 
Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝soniya singh
 
High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...
High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...
High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...gurkirankumar98700
 
CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...
CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...
CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...anilsa9823
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...aditipandeya
 
Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...
Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...
Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...anilsa9823
 
Product Catalog Bandung Home Decor Design Furniture
Product Catalog Bandung Home Decor Design FurnitureProduct Catalog Bandung Home Decor Design Furniture
Product Catalog Bandung Home Decor Design Furniturem3resolve
 
EMPLOYEES JOB SATISFACTION ( With special reference to selected Sundaram Ind...
EMPLOYEES JOB SATISFACTION  ( With special reference to selected Sundaram Ind...EMPLOYEES JOB SATISFACTION  ( With special reference to selected Sundaram Ind...
EMPLOYEES JOB SATISFACTION ( With special reference to selected Sundaram Ind...ksanjai333
 
VIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our Escorts
VIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our EscortsVIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our Escorts
VIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our Escortssonatiwari757
 
Top Call Girls In Arjunganj ( Lucknow ) ✨ 8923113531 ✨ Cash Payment
Top Call Girls In Arjunganj ( Lucknow  ) ✨ 8923113531 ✨  Cash PaymentTop Call Girls In Arjunganj ( Lucknow  ) ✨ 8923113531 ✨  Cash Payment
Top Call Girls In Arjunganj ( Lucknow ) ✨ 8923113531 ✨ Cash Paymentanilsa9823
 

Kürzlich hochgeladen (20)

(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...
(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...
(COD) ̄Young Call Girls In Defence Colony , New Delhi꧁❤ 7042364481❤꧂ Escorts S...
 
Cheap Rate ➥8448380779 ▻Call Girls In Sector 54 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 54 GurgaonCheap Rate ➥8448380779 ▻Call Girls In Sector 54 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 54 Gurgaon
 
Cheap Rate ➥8448380779 ▻Call Girls In Sector 55 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 55 GurgaonCheap Rate ➥8448380779 ▻Call Girls In Sector 55 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 55 Gurgaon
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Secunderabad high-profile Cal...
 
Pakistani Jumeirah Call Girls # +971559085003 # Pakistani Call Girls In Jumei...
Pakistani Jumeirah Call Girls # +971559085003 # Pakistani Call Girls In Jumei...Pakistani Jumeirah Call Girls # +971559085003 # Pakistani Call Girls In Jumei...
Pakistani Jumeirah Call Girls # +971559085003 # Pakistani Call Girls In Jumei...
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...
VIP 7001035870 Find & Meet Hyderabad Call Girls Jubilee Hills high-profile Ca...
 
Cheap Rate ➥8448380779 ▻Call Girls In Sector 56 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 56 GurgaonCheap Rate ➥8448380779 ▻Call Girls In Sector 56 Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Sector 56 Gurgaon
 
A STUDY ON EMPLOYEE MORALE AT ELGI EQUIPMENT ELIMITED
A STUDY ON EMPLOYEE MORALE AT ELGI  EQUIPMENT ELIMITEDA STUDY ON EMPLOYEE MORALE AT ELGI  EQUIPMENT ELIMITED
A STUDY ON EMPLOYEE MORALE AT ELGI EQUIPMENT ELIMITED
 
Call girls in Andheri with phone number 9892124323
Call girls in Andheri with phone number 9892124323Call girls in Andheri with phone number 9892124323
Call girls in Andheri with phone number 9892124323
 
Top Call Girls In Indira Nagar Lucknow ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
Top Call Girls In Indira Nagar Lucknow ( Lucknow  ) 🔝 8923113531 🔝  Cash PaymentTop Call Girls In Indira Nagar Lucknow ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment
Top Call Girls In Indira Nagar Lucknow ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
 
Mumbai Call Girls Colaba Pooja WhatsApp 7738631006 💞 Full Night Enjoy
Mumbai Call Girls Colaba Pooja WhatsApp  7738631006  💞 Full Night EnjoyMumbai Call Girls Colaba Pooja WhatsApp  7738631006  💞 Full Night Enjoy
Mumbai Call Girls Colaba Pooja WhatsApp 7738631006 💞 Full Night Enjoy
 
Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Bawana Delhi reach out to us at 🔝8264348440🔝
 
High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...
High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...
High Profile Call Girls in Lucknow | Whatsapp No 🧑🏼‍❤️‍💋‍🧑🏽 8923113531 𓀇 VIP ...
 
CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...
CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...
CALL ON ➥8923113531 🔝Call Girls Sushant Golf City Lucknow best sexual service...
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...
VIP 7001035870 Find & Meet Hyderabad Call Girls Gachibowli high-profile Call ...
 
Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...
Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...
Lucknow 💋 Escort Service in Lucknow ₹7.5k Pick Up & Drop With Cash Payment 89...
 
Product Catalog Bandung Home Decor Design Furniture
Product Catalog Bandung Home Decor Design FurnitureProduct Catalog Bandung Home Decor Design Furniture
Product Catalog Bandung Home Decor Design Furniture
 
EMPLOYEES JOB SATISFACTION ( With special reference to selected Sundaram Ind...
EMPLOYEES JOB SATISFACTION  ( With special reference to selected Sundaram Ind...EMPLOYEES JOB SATISFACTION  ( With special reference to selected Sundaram Ind...
EMPLOYEES JOB SATISFACTION ( With special reference to selected Sundaram Ind...
 
VIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our Escorts
VIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our EscortsVIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our Escorts
VIP Chandigarh Call Girls 7001035870 Enjoy Call Girls With Our Escorts
 
Top Call Girls In Arjunganj ( Lucknow ) ✨ 8923113531 ✨ Cash Payment
Top Call Girls In Arjunganj ( Lucknow  ) ✨ 8923113531 ✨  Cash PaymentTop Call Girls In Arjunganj ( Lucknow  ) ✨ 8923113531 ✨  Cash Payment
Top Call Girls In Arjunganj ( Lucknow ) ✨ 8923113531 ✨ Cash Payment
 

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.