You are on page 1of 9

ALCC8l1MCS 8ASlCCS

(para examen dlagnsuco)


Anllsls y ulseno de AlgorlLmos ll
ura. Laura Cruz 8eyes
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
AlgorlLmo enconLrar el menor
!"#$%&'() mlnlmum (! # $ )
!"#$%% ! and $# where ! ls an array wlLh $ elemenLs
&$%#$%% 1he posluon of Lhe smallesL elemenL ln !

1 &'$ ! ![0], [Assume Lhe rsL ls Lhe smallesL}
2 *$% ' !1 '$ $() +$
3 &* ![']< &'$ [lf Lhe assumpuon ls noL conrmed}
4 &'$ ! !['] [updaLe Lhe mlnlmum}
3 %-'.%/ &'$
2
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
Algoritmo de multiplicacin de matrices
Algorithm matrixMultiplication (A,B,C,m,n,p)
Input: An m ! n matrix A and an n ! p matrix B .
Output: An m " p matrix C

1 for i ! 0 to m-1 do
2 for j ! 0 to p-1 do
3 C[i,j] = 0
4 for k ! 0 to n-1 do
5 C[i,j] ! C[i,j] + A[i,k] B[k,j]
6 return C

3
U1 Intro. anlisis de algoritmos
Algorithm secondLargest (E, n)
Input: E and n, where E is an array with n elements.
Output: The second Largest element in the array E.

1 if E[1] < E[0] then
2 first ! E[0]
3 second ! E[1]
4 else
5 first ! E[1]
6 second ! E[0]
7 for i ! 2 to n 1 do
8 if E[i] > second then
9 if E[i] > first then
10 second ! first
11 first ! E[i];
12 else
13 second ! E[i]
14 return second
AlgorlLmo enconLrar segundo mayor
Dra. Laura Cruz Reyes 4
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
AlgorlLmo de busqueda secuenclal
!"#$%&'() secuenclalSearch (! # $# * )
!"#$%' An ordered array ! wlLh $ elemenLs and Lhe key * Lo be
searched.
&$%#$%' 1he posluon of * ln Lhe arrray !# or -1 lf * ls noL found.

1 posluon = -1, [Assume fallure}
2 *$% ' !0 Lo $(1 +$
3 &* * = ![']
4 posluon = ', [Success}
3 0%-12,
6 %-'.%/ posluon

5
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
Algoritmo de Bsqueda Binaria Iterativa
!"#$%&'() blnarylLerauveSearch(!, $, *)
!"#$%' An ordered array ! wlLh $ elemenLs and Lhe key * Lo be searched.
&$%#$%' 1he posluon of * ln Lhe arrray !# or -1 lf * ls noL found.

1 +,- ! 0
2 .'/. ! $ - 1
3 3(&"- +,- <= .'/. +$
4 &'0 ! #(+,- + .'/.) / 2$
3 &* ![&'0] < *
6 +,- ! &'0 + 1
7 -"4- &* ![&'0] > *
8 .'/. ! &'0 - 1
9 -"4- %-'.%/ mld
10 %-'.%/ -1 [key noL found}
6
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
Algoritmo de Ordenamiento Burbuja
!"#$%&'() 8ubleSorL(!, $)
!"#$%' ! and $, where ! ls an array wlLh $ elemenLs.
&$%#$%' 1he array ! wlLh elemenLs rearranged ln non-decreaslng
order.

1 *$% ' ! $ - 2 '$ 0 +$
2 *$% 1 ! 0 '$ ' +$
3 &* ![1] > ![121] +$
4 swap(!, 1, 1+1)
7
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
Algoritmo de Ordenamiento por
Insercin
!"#$%&'() lnseruonSorL(L,n)
!"#$%% ! and $# where ! ls an array wlLh $ elemenLs.
&$%#$%% 1he array ! wlLh elemenLs rearranged ln non-decreaslng
order.

1 *$% ' ! 1 '$ $-1 +$
2 3&4 ! ![']
3 1 ! '
4 3(&"- 1 > 0 1/+ ![1-1] > 3&4 +$
3 ![1] ! ![1-1]
6 1 ! 1 - 1
7 ![1] ! 3&4
8
Dra. Laura Cruz Reyes U1 Intro. anlisis de algoritmos
Algoritmo de Ordenamiento por
Seleccin
Algorithm selectionSort(E,n)
Input: E and n, where E is an array with n elements.
Output: The array E with elements rearranged in non-decreasing
order

1 for i ! 0 to n-2 do
2 min ! i
3 for j ! i+1 to n-1 do
4 if E[j] < E[min] then
5 min ! j
6 swap(E[i],E[min])
9

You might also like