Insertion Sort :

1. Set A[0] := -∞
2. Repeat Steps 3 to 5 for K=2,3,....N
3.    Set TEMP := A[K] and PTR := K-1.
4.    Repeat while TEMP<A[PTR] :
     (a) Set A[PTR+1] := A[PTR].
    (b) Set PTR := PTR -1.
5. Set A[PTR +1] : = TEMP
6. Return.

Complexity :

worst case : O(n")
average case : O(n")

Selection Sort :

Procedure 9.2:

1. Set MIN := A[K] , A[K+1],... A[N]
2. Repeat for J= K+1, K+2,....N
    If MIN>A[J], then : Set MIN := A[J] and LOC := A[J] and LOC:=J
4. Exit

Selection Sort :

1. Repeat steps 2 and 3 for K = 1,2,3,... N-1
2.    Call MIN(A,K,N,LOC)
3.    Set TEMP := A[K], A[K] := A[LOC], A[LOC] := TEMP.
4. Exit.

 Merging :

1. Set NA := 1, NB := 1 and PTR := 1.
2. Repeat while NA <=R and NB <= S:
     If A[NA] < B[NB], then :
       (a) Set C[PTR] := A[NA]
(b)Set PTR := PTR +1 and NA := NA +1.
Else
(a) Set C[PTR] := B[NB]
(b) Set PTR := PTR + 1 and NB := NB + 1
3. Tf NA > R, then :
Repeat for K = 0,1,2,...,S- NB :
Set C[PTR + K] := A[NB+K].
Else
Repeat for K= 0,1,2,...,R-NA :
Set C[PTR+K]:= A[NA+K]
4. Exit.

Breadh-first Search :

1. Initialize all nodes to the ready state(STATE=1)
2. Put the starting node A in QUEUE and change its status to the waiting state(STATUS = 2)
3. Repeat steps 4 and 5 until QUEUE is empty.
4. Remove the front node N of QUEUE . Process N and change the status of N to thee processed state(STATUS =3).
4. Add to the rear of QUEUE all the neighbors of N that are in steady state(STATUS=1), and change their status to the waiting state (STATUS =2)
6. Exit.

Depth First Search :
1. Initialize all nodes to the ready state(STATUS=1)
2. Push the starting node A onto STACK and change its status to the waiting status(STATUS=2)
3. Repeat steps 4 and 5 until STACK is empty.
4. Pop the top node N of STACK. Process N and change its status to the processed state(STATUS=3)
5. Push onto STACK all the neighbors of N that are in steady state(STATUS=1), and change their status to the waiting state (STATUS =2).
6. Exit.

Warshell algorithm :

1. Repeat for I, J=1,2,..., M
If A[I,J]=0, then : Set P[I,J] := 0;
Else : Set P[I,J] := 1.
2. Repeat steps 3 and 4 for K=1,2,....,M :
3. Repeat Step 4 for I=1,2,...M :
4. Repeat for J=1,2,....M :
Set P[I,J] := P[I,J]^(ulta)(P[I,K]^P[K,J]).
5. Exit.

Shortest path algorithm :

1. Repeat for I,J = 1,2,....M :
W[I,J] = 0, then : Set Q[I,J] := INFINITY ;
Else : Set Q[I,J] := W[I,J].
2. Repeat Steps 3 and 4 for K=1,2,....,M :
3. Repeat Step 4 for I=1,2,...M :
4. Repeat for J=1,2,....M :
Set Q[I,J] :=MIN (Q[I,J]Q,I,K[] + Q[K,J])
5. Exit.

 


XtGem Forum catalog