, PSEUDOCODELO0) : VARIA-
INSERTION SORT :
SO216 PROBLEIS :
1. Efficient algorithm For SMALL NUMBERS OF ELEMENTS
Sorting .
1 We call I
the numbers that we want to sort as KEYS
.
2. we will use PSEUDOCODE to express our algorithm
. PSEUDOCODE :
r
>
↓ IN-PLACE vs OUT-PLACE
= SECTIO-SORTIA) :
read 1 FOR j 2 to A
length
↓
it's easy to =
.
WHII C
For human eye
.
A & 2 key Alj] =
IsOddOrEven
o alalas
EX :
&
PROGRAM :
1) Insert Atj) into sorted sequence A[1 -1
...;
Read A , (a) 5 24 613
i
IF (Al2 gives remainder) ↳
i =
j -
1
2151416/1/s is
i
Odd", (b) o Atene
THEN print "It's
i ELSE w
A[i + 1)
&
I
print "It's Even"; &
=
ATi]
(C) 24 5613
ENDIF i = i 1 -
END
↳
(0) 145613 g Ati + 1] =
key
-
*
1) 124563
v v va
⑰ A =
[ 31 41 , 59, ,
26 , 41, 58] Alength =
6
LOOP INVARIANT :
* j :
2 All] : 41 =
key * j = 3 At3) = Key =
59
i 1 A [17 All]
Y
31 i 2 41 NO BUCLE
Y
= = =
=
NO BUCLE
x9A[i) <
key
1 .
We want to see if the algorithm always works All] =
key =
41
x9A[1) < key
A[3] =
59
2. Maintenance (Dibujo pag 1 2) * i A[4)
= 4 =
16 :
key
.
En
array, recorrerlo hasta encontrar
un
i = 3 A[3) 59
&
=
el valor
,
si es igual decir el indice y
A[47 A[3] 59 buclexs TitaTes 141501
encuentre
=
(decir Nill siempre
=
si poner Nill
no hasta q se &
i = 2
A [3] = A[2] = 41
-
>
A :
[26 ,
31 , 41, 59941 58] ,
Alent
Forma( WhieIVAti)
is
⑳ j
=
y A[5]
# Initialitation :
10
= 5 = 41 :
key
i 4 A[4] 59
First iteration For =
salso
>
-
=
i 1 AIS) A[4] 59 ↳ si
E
=
Snothing
= =
Bucle X 1
i = 3
& Maintenance [4]
I
: Es seguir literalmente el bucle. Find False key =
A =
59
xq11 41 =
pero no
mayor
cuando encuentro el For =7
Estamos asegurandoq 20 Forma j to A .
length linear search (A , v)
1i !
504
!
A + 16 31 42
valor el array anterior no la tiene
.
IF Alj] V For 1 1 to A E .
:
, , ,
42 59 ,
length
= = : ,
, =
.
I
Find > True IF Alil == V :
j 6 A [6] 58 key
&
=
i
=
=
Termination :
1
return index
i : 'return i
/
I i = 5 A[5] =
59
=
1
&
return Nill
4
i iF Find False /59/58
= ==
:
A[6] A[5] =
59 Brule
8]
=
[3 7
Imagine >
-
v S return Nill
S
=
, ,
i = 4
Subarray : [1 i-1] j > A length
,
A [5] key 59
.
: =
↳ 26, 31 , 31 47 , 58 59
,
,
El programa antes de entrar en
el for te suma la i
a ;
y luego
lo comprueba , x10 tanto al
Final de este
ejemplo quedara
n= 3
n+ 1 4
= = i
INSERTION SORT :
SO216 PROBLEIS :
1. Efficient algorithm For SMALL NUMBERS OF ELEMENTS
Sorting .
1 We call I
the numbers that we want to sort as KEYS
.
2. we will use PSEUDOCODE to express our algorithm
. PSEUDOCODE :
r
>
↓ IN-PLACE vs OUT-PLACE
= SECTIO-SORTIA) :
read 1 FOR j 2 to A
length
↓
it's easy to =
.
WHII C
For human eye
.
A & 2 key Alj] =
IsOddOrEven
o alalas
EX :
&
PROGRAM :
1) Insert Atj) into sorted sequence A[1 -1
...;
Read A , (a) 5 24 613
i
IF (Al2 gives remainder) ↳
i =
j -
1
2151416/1/s is
i
Odd", (b) o Atene
THEN print "It's
i ELSE w
A[i + 1)
&
I
print "It's Even"; &
=
ATi]
(C) 24 5613
ENDIF i = i 1 -
END
↳
(0) 145613 g Ati + 1] =
key
-
*
1) 124563
v v va
⑰ A =
[ 31 41 , 59, ,
26 , 41, 58] Alength =
6
LOOP INVARIANT :
* j :
2 All] : 41 =
key * j = 3 At3) = Key =
59
i 1 A [17 All]
Y
31 i 2 41 NO BUCLE
Y
= = =
=
NO BUCLE
x9A[i) <
key
1 .
We want to see if the algorithm always works All] =
key =
41
x9A[1) < key
A[3] =
59
2. Maintenance (Dibujo pag 1 2) * i A[4)
= 4 =
16 :
key
.
En
array, recorrerlo hasta encontrar
un
i = 3 A[3) 59
&
=
el valor
,
si es igual decir el indice y
A[47 A[3] 59 buclexs TitaTes 141501
encuentre
=
(decir Nill siempre
=
si poner Nill
no hasta q se &
i = 2
A [3] = A[2] = 41
-
>
A :
[26 ,
31 , 41, 59941 58] ,
Alent
Forma( WhieIVAti)
is
⑳ j
=
y A[5]
# Initialitation :
10
= 5 = 41 :
key
i 4 A[4] 59
First iteration For =
salso
>
-
=
i 1 AIS) A[4] 59 ↳ si
E
=
Snothing
= =
Bucle X 1
i = 3
& Maintenance [4]
I
: Es seguir literalmente el bucle. Find False key =
A =
59
xq11 41 =
pero no
mayor
cuando encuentro el For =7
Estamos asegurandoq 20 Forma j to A .
length linear search (A , v)
1i !
504
!
A + 16 31 42
valor el array anterior no la tiene
.
IF Alj] V For 1 1 to A E .
:
, , ,
42 59 ,
length
= = : ,
, =
.
I
Find > True IF Alil == V :
j 6 A [6] 58 key
&
=
i
=
=
Termination :
1
return index
i : 'return i
/
I i = 5 A[5] =
59
=
1
&
return Nill
4
i iF Find False /59/58
= ==
:
A[6] A[5] =
59 Brule
8]
=
[3 7
Imagine >
-
v S return Nill
S
=
, ,
i = 4
Subarray : [1 i-1] j > A length
,
A [5] key 59
.
: =
↳ 26, 31 , 31 47 , 58 59
,
,
El programa antes de entrar en
el for te suma la i
a ;
y luego
lo comprueba , x10 tanto al
Final de este
ejemplo quedara
n= 3
n+ 1 4
= = i