Docsity

Sorting Algorithms: Bubble Sort and Selection Sort, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
Sorting
B bbl t l ith
B
u
bbl
e sor
t
a
l
gor
ith
m
1. Array consists of two parts: sorted and
u
n
so
r
ted.
Ini
t
i
a
ll
y
t
h
e
a
rr
ay
i
s
u
n
so
r
ted.
usoted. taytea aysusoted.
2. Consider consecutively each two adjacent
ltfth tdtd th
e
l
emen
t
s o
f
th
e unsor
t
e
d
par
t
an
d
swap
th
em
if necessary. The minimal (“lighter”)
l t t t b bbl
e
l
emen
t
comes ou
t
on
t
op as a
b
u
bbl
e.
3. The “li
g
hter” element leaves the unsorted
g
part and joins to the sorted one.
4
R t t 2 d 3 til i t d
4
.
R
epea
t
s
t
eps
2
an
d
3
un
til
array
i
s sor
t
e
d
.
The Bubble Sort AlgorithmThe Bubble Sort Algorithm
• The Bubble
Slih
60
70
S
ort a
l
gor
i
t
h
m
looks at pairs
40
50
o
entr
es
n
the array, and
hi
20
30
swaps t
h
e
i
r
order if
dd
0
10
nee
d
e
d
.
0
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
The Bubble Sort AlgorithmThe Bubble Sort Algorithm
• The Bubble
Slih
60
70
S
ort a
l
gor
i
t
h
m
looks at pairs
40
50
o
entr
es
n
the array, and
hi
20
30
swaps t
h
e
i
r
order if
dd
0
10
nee
d
e
d
.
Swap?
0
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Swap?
The Bubble Sort AlgorithmThe Bubble Sort Algorithm
• The Bubble
Slih
60
70
S
ort a
l
gor
i
t
h
m
looks at pairs
40
50
o
entr
es
n
the array, and
hi
20
30
swaps t
h
e
i
r
order if
dd
0
10
nee
d
e
d
.
Yes !
0
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Yes !
The Bubble Sort AlgorithmThe Bubble Sort Algorithm
• The Bubble
Slih
60
70
S
ort a
l
gor
i
t
h
m
looks at pairs
40
50
o
entr
es
n
the array, and
hi
20
30
swaps t
h
e
i
r
order if
dd
0
10
nee
d
e
d
.
Swap?
0
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Swap?
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Sorting Algorithms: Bubble Sort and Selection Sort and more Slides Data Structures and Algorithms in PDF only on Docsity!

Sorting

B bblBubble sort algorithm t l ith

1. Array consists of two parts: sorted andunsorted. Initially the array is unsorted.u so ted. t a y t e a ay s u so ted.

2. Consider consecutively each two adjacentelements of the unsorted part and swap theml t f th t d t d th

if necessary. The minimal (“lighter”)element comes out on top as a bubble.l t t t b bbl

3. The “lighter” element leaves the unsortedpart and joins to the sorted one.g

4 4. Repeat steps 2 and 3 until array is sorted. R t t 2 d 3 til i t d

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsfort algorithm i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsort algorithmf i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Swap? Swap? 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsfort algorithm i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Yes! Yes! 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsort algorithmf i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Swap? Swap? 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsfort algorithm i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.No No. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsort algorithmf i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Swap? Swap? 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsfort algorithm i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.No No. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsort algorithmf i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Swap? Swap? 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsfort algorithm i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Yes! Yes! 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • The BubbleS l i h

Slooks at pairsort algorithmf i i 6070

of entries inthe array, andh i 4050

swaps theirorder ifd d 2030

needed.Swap? Swap? 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Repeat. 6070 4050 2030

Swap? Yes Swap? Yes. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Loop over 1

array n-1times,i i 6070

swapping pairsof entries asd d 4050

needed. 2030

Swap? No Swap? No. 1000 [1] [0] [1] [2] [2] [3] [3] [4] [4] [5] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Loop over 1

array n-1times,i i 6070

swapping pairsof entries asd d 4050

needed. 2030

Swap? Yes Swap? Yes. 1000 [1] [0] [1] [2] [3] [2] [3] [4] [4] [5] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Loop over 1

array n-1times,i i 6070

swapping pairsof entries asd d 4050

needed. 2030

Swap? Yes Swap? Yes. 1000 [1] [0] [1] [2] [2] [3] [3] [4] [4] [5] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Loop over 1

array n-1times,i i 6070

swapping pairsof entries asd d 4050

needed. 2030

Swap? Yes Swap? Yes. 1000 [1] [0] [1] [2] [3] [2] [3] [4] [4] [5] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Loop over 1

array n-1times,i i 6070

swapping pairsof entries asd d 4050

needed. 2030

Swap? Yes Swap? Yes. 1000 [1] [0] [1] [2] [2] [3] [3] [4] [4] [5] [5] [6]

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

  • Continuel i il

ldone.ooping, until 6070

Swap? Yes Swap? Yes. 1000 [1] [0] [1] [2] [3] [2] [3] [4] [4] [5] [5] [6]

Bubble Sort Pseudocode

current = 0sorted = falsesorted false

loop (current < last AND sorted false)walker = last

sorted = trueloop (walker > current )dif( list[walker] < list[walker-1] )

(sorted = falseswap(list, walker, walker-1) [ ] [ ] ) 1

end if

end loop^ end ifwalker = walker – 1

end loopcurrent = current + 1

Selection sort

  • Given as input a sequence of unorderedintegers, produce as output a sequence of the
  • same integers in ascending order.Algorithm:Algorithm:

1. Array consists of two parts: sorted (left) andunsorted (right) Initially the array is unsortedunsorted (right). Initially the array is unsorted.

2. Swap the first item of the unsorted part with thesmallest item of the unsorted partsmallest item of the unsorted part.

3. First item of the unsorted part leaves it and joinsas last item of the sorted part.as last item of the sorted part.

4. Repeat steps 2 and 3 until array is sorted.

Sorting an Array of IntegersSorting an Array of Integers

  • Example: weare given ani

array of sixi i ntegers thath 6070

we want to sort fromf 4050

smallest tol l argest 2030

1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

  • Start byfi di h

fi smallestnding the 6070

entry. 4050

1000 [1] [0] [2] [1] [3] [2] [4] [3] [5][5] [4] [6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

  • Swap thell

smallestentry withh fi 6070

theentry first. 4050

1000 [1] [0] [2] [1] [3] [2] [4] [3] [5][5] [4] [6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

Sorted side Unsorted side

  • The processti 6070

continues... 4050

1000 [1] [0] [2] [1] [3] [2] [4][4] [3] [5][5] [4] [6][6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

Sorted sideis bigger Sorted side Unsorted side

  • The processti 6070

continues... 4050

1000 [1] [0] [2] [1] [3] [2] [4][4] [3] [5][5] [4] [6][6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

  • The processk ddi^ Sorted side^ Unsorted side

kone moreeeps addingb h 6070

number to thesorted side. 4050

  • The sorted sidehas the smallest 2030

numbers,arranged fromsmall to large. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5][5] [4] [6][6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

  • We can stoph h^ Sorted side^ Unsorted sid

when theunsorted sideh j 6070

hnumber, sincehas just one b 4050

that numbermust be thel b 2030

largest number. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6][6] [5]

The Selection Sort AlgorithmThe Selection Sort Algorithm

  • The array isd
  • now sorted.We repeatedly 6070

selectedsmallest the 4050

element, andmoved this 2030

element to thefront of theunsorted side. 1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

Selection Sort

Again, we search the unsorted list for the smallest element. Wethen exchange the smallest element (23) with the first element^ We start with an unsorted list. We search this list for thesmallest element. We then exchange the smallest element (8)This process continues until the list is fully sorted.

then exchange the smallest element (23) with the first elementin the unsorted list (78) and move the conceptual wall.with the first element in the unsorted list (23) and move theconceptual wall.

current = 0Selection Sort Pseudocode loop (current < last)smallest = current walker = current + 1lloop (walker <= last) ( lk < l t) if( list[walker] < list[smallest] )smallest = walkersmallest = walker end ifwalker = walker + 1 end loopswap(list,current,smallest) end loop^ current = current + 1

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

  • The InsertionS l i h

Salso views theort algorithmh i 6070

array as havinga sorted sided 4050

and anunsorted side. 2030

1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

Sorted side Unsorted side

  • The sortedside starts 6070

with just thefirst 4050

element,which is not 2030

necessarilythe smallestelement. 1000 [1][1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

Sorted side Unsorted side

  • The sortedside grows 6070 4050

by taking the^ g

front

elementfrom the 2030

unsortedside... 1000 [1][1] [0] [2][2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

Sorted side Unsorted side

  • ...andinserting it 6070 4050

in the place^ g

that keeps

the sorted^ p

side

arrangedfrom small to large. 1000 [1][1] [0] [2][2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

Sorted side Unsorted side

1000 [1][1] [0] [2][2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

How to Insert One ElementHow to Insert One Element

Continueshifting

shifting elements... 6070

1000 [1][1] [0] [2][2][2] [1] [3][3] [2] [4][4] [3] [5][5] [4] [6] [5]

How to Insert One ElementHow to Insert One Element

...until youreach the

reachlocation forthe new the 6070

theelement. new 4050

1000 [1][1][1] [0] [2][2] [1] [3][3] [2] [4][4] [3] [5][5] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

žCopy thenew element Sorted side Unsorted sid

newback into thearray at the element 6070

arraycorrect, at the

location

location.

1000 [1][1] [0] [2][2] [1] [3][3] [2] [4][4] [3] [5][5] [4] [6] [5]

How to Insert One ElementHow to Insert One Element

  • The lastl^ Sorted side^ Unsorted sid

elementmust also bei d 6070

iStart bynserted.i i 4050

copying it... 2030

1000 [1] [0] [2] [1] [3] [2] [4] [3] [5] [4] [6] [5]

Sorted ResultSorted Result 6070 4050 2030 (^1000) [0] [1] (^) [1] [2] (^) [2] [3] (^) [3] [4] (^) [4] [5] (^) [5] [6]

Insertion sort algorithm

1. Array consists of two parts: sorted andunsorted. Initially only first elementu so ted. t a y o y st e e e t

2 2. Consider first of unsorted elements. It belongs to the sorted part.C id fi t f t d l t It

leaves the unsorted part and moves to a““proper” position of the sorted part, so that ” iti f th t d t th t

the sorted part must remain sorted. Manyelements have to be shifted to “openl t h t b hift d t “

3. Repeat step 2 until array is sorted.room” for the movement.

current = 1 //Note: first index of array is 0Insertion Sort Pseudocode

loop (current <= last)hold = list[current] current = 1current = 1current = 1current = 2current = 2current = 2current = 2Variables

walker = current – 1;loop (walker >= 0 AND hold < list[walker])hold = 78hold = 78walker = 0hold = 78walker = 0hold = 45walker = 0hold = 45walker = 1hold = 45walker = 0

list[walker+1] = list[walker]walker = walker – 1lk lk 1

end looplist[walker+1]list[walker+1] = hold hold list^230 17845

end loopend loopcurrent = current + 1

return