Professional Documents
Culture Documents
Insert Unsorted ( ):
Description: Here A is a sorted linear array with N elements. LOC is the location where
ITEM is to be inserted.
Explanation: Here A is an unsorted array stored in memory with N elements. This algorithm
inserts a data element ITEM into the loc th position in an array A. The first four steps create
space in A by moving downward the elements of A. These elements are moved in reverse
order i.e. first A[N], then A[N–1], A[N–2]. . . . . and last A[LOC], otherwise data will be
overwritten. We first set I=N and then, using as a counter, decrease it each time the loop is
executed until I reaches LOC. In the next step, Step 5, it inserts ITEM into the array in the
space just created. And at last, the total number of elements N is increased by 1.
INSERT SORTED
Insert Sorted ( ):
Description: Here A is a sorted linear array (in ascending order) with N elements. ITEM is
the value to be inserted.
Explanation: Here A is a sorted array stored in memory. This algorithm inserts a data
element ITEM into the (I + 1)th position in an array A. I is initialized from N i.e. from total
number of elements. ITEM is compared with each element until it finds an element which is
smaller than A[I] or it reaches the first element. During this process, the elements are
moved downwards and I is decremented. When it finds an element smaller then ITEM, it
inserts it in the next location i.e. I + 1 because I will be one position less where ITEM is
to be inserted. And finally, total number of elements is increased by 1.
DELETE
Delete ( ):
Description: Here A is a linear array with N elements. LOC is the location from where ITEM
is to be deleted.
Explanation: First, the element to be deleted is assigned to ITEM from location A [LOC].
Then I is set to LOC from where ITEM is to be deleted and it iterated to total number of
elements i.e. N. During this loop, the elements are moved upwards. And lastly, total number of
elements is decreased by 1.
TRAVERSE
Traverse ( ):
Description: Here A is a linear array with lower bound LB and upper bound UB. This
algorithm traverses array A and applies the operation PROCESS to each element of the
array.
1. Repeat For I = LB to UB
2. Apply PROCESS to A[I]
[End of For Loop]
3. Exit
Explanation: Here A is a linear array stored in memory with lower bound LB and upper bound
UB. The for loop iterates from LB to UB and visits each element of the array. During this visit, it
applies the operation PROCESS to the elements of the array A.
Note: The operation PROCESS in the traversal algorithm may use certain variables which must
be initialized before PROCESS is applied to any of the elements in the array. Therefore, the
algorithm may need to be preceded by such an initialization step.
TRAVERSE
Traverse ( ):
Description: Here A is a two – dimensional array with M rows and N columns. This algorithm
traverses array A and applies the operation PROCESS to each element of the array.
1. Repeat For I = 1 to M
2. Repeat For J = 1 to N
3. Apply PROCESS to A[I][J]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
4. Exit
Explanation: The first for loop iterates from 1 to M i.e. to the total number of rows and the
second for loop iterates from 1 to N i.e. to the total number of columns. In step 3, it applies
the operation PROCESS to the elements of the array A.
MERGE UNSORTED
Merge Unsorted ( ):
Description: Here A is an array with M elements and B is an array with N elements. C is an
empty array with P locations where P >= M + N.
1. Repeat For I = 1 to M
2. Set C[I] = A[I] [Assign the elements of array A to array C]
[End of For Loop]
3. Set K = 1 [Initialize counter]
4. Repeat For J = M+1 to M+N
5. Set C[J] = B[K] [Assign the elements of array B to array C]
6. Set K = K + 1 [Increase the counter by 1]
[End of For Loop]
7. Exit
Explanation: In step 1 & 2, all the elements of array A are assigned to array C. Then K is
initialized from 1 which will keep track of the elements of array B. In step 4 for loop, J is
initialized from next empty location in C i.e. M+1 and it will iterate to total number of
elements of array A & B i.e. M+N. In step 5, all the elements of array B are assigned to array C
and in next step, K is incremented by 1.
MERGE SORTED
Merge Sorted ( ):
Description: Here A is a sorted array with M elements and B is a sorted array with N
elements. C is an empty array with P locations where P >= M + N.
Explanation: The above algorithm merges the elements of sorted array A and sorted array B
into a sorted array C. First of all, we must keep track of the locations of the smallest element of
A and the smallest element of B which have not yet been placed in C. I and J denote these
locations respectively, and K denotes the location in C to be filled. Therefore, initially, we set
I = J = K = 1. In step 3, we compare A [I] and B [J] and assign the smallest
element to C [K]. Then we either increment I by setting I = I + 1 or increment J by
setting J = J + 1, according to whether the new element in C has come from A or B. And
also we increment K by setting K = K + 1. If I > M, then it means array A has become
empty and the remaining elements of B are assigned to C, or if J > N, then it means array B
has become empty and the remaining elements of A are assigned to C.
TRANSPOSE
Transpose ( ):
Description: Here A is a two – dimensional array with M rows and N columns. This algorithm
transposes the array.
1. Repeat For I = 1 to M
2. Repeat For J = 1 to N
3. Set B[J][I] = A[I][J]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
4. Exit
Explanation: The first for loop iterates from 1 to M i.e. total number of rows and second for
loop iterates from 1 to N i.e. total number of columns. In step 3, the element at location
A[I][J] is assigned to B[J][I] by the statement B[J][I] = A[I][J].
ADDITION
Add ( ):
Description: Here A is a two – dimensional array with M rows and N columns and B is a two
–dimensional array with X rows and Y columns. This algorithm adds these two arrays.
1. If (M ≠ X) or (N ≠ Y) Then
2. Print: Addition is not possible.
3. Exit
[End of If]
4. Repeat For I = 1 to M
5. Repeat For J = 1 to N
6. Set C[I][J] = A[I][J] + B[I][J]
[End of Step 5 For Loop]
[End of Step 6 For Loop]
7. Exit
Explanation: First, we have to check whether the rows of array A are equal to the rows of
array B or the columns of array A are equal to the columns of array B. if they are not equal,
then addition is not possible and the algorithm exits. But if they are equal, then first for loop
iterates to the total number of rows i.e. M and the second for loop iterates to the total number
of columns i.e. N. In step 6, the element A[I][J] is added to the element B[I][J] and is
stored in C[I][J] by the statement:
1. If (M ≠ Y) or (N ≠ X) Then
2. Print: Multiplication is not possible.
3. Else
4. Repeat For I = 1 to N
5. Repeat For J = 1 to X
6. Set C[I][J] = 0
7. Repeat For K = 1 to Y
8. Set C[I][J] = C[I][J] + A[I][K] * B[K][J]
[End of Step 7 For Loop]
[End of Step 5 For Loop]
[End of Step 4 For Loop]
[End of If]
9. Exit
Explanation: First we check whether the rows of A are equal to columns of B or the
columns of A are equal to rows of B. If they are not equal, then multiplication is not possible.
But, if they are equal, the first for loop iterates to total number of columns of A i.e. N and the
second for loop iterates to the total number of rows of B i.e. X. In step 6, all the elements of C
are set to zero. Then the third for loop iterates to total number of columns of B i.e. Y. In step
8, the element A[I][K] is multiplied with B[K][J] and added to C[I][J] and the
result is assigned to C[I][J] by the statement:
1. Repeat For J = 1 to N
2. If (ITEM == A[J]) Then
3. Print: ITEM found at location J
4. Return
[End of If]
[End of For Loop]
5. If (J > N) Then
6. Print: ITEM doesn’t exist
[End of If]
7. Exit
BINARY SEARCH
Binary Search ( ):
Description: Here A is a sorted array having N elements. ITEM is the value to be searched.
BEG denotes first element and END denotes last element in the array. MID denotes the
middle value.
1. Repeat For J = 1 to N
2. Repeat For K = 1 to N-J
3. If (A[K] > A[K+1]) Then
4. Interchange A[K] and A[K+1]
[End of If]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
5. Exit
SELECTION SORT
Selection Sort ( ):
Description: Here A is an unsorted array having N elements.
1. Repeat For J = 1 to N
2. Set MIN = J
3. Repeat For K = J+1 to N
4. If (A[K] < A[MIN]) Then
5. Set MIN = K
[End of If]
[End of Step 3 For Loop]
6. Interchange A[J] and A[MIN]
[End of Step 1 For Loop]
7. Exit
INSERTION SORT
Insertion Sort ( ):
Description: Here A is an unsorted array having N elements.
1. Repeat For J = 2 to N
2. Set TEMP = A[J]
3. Set K = J - 1
4. Repeat While (K >= 1) and (A[K] > TEMP)
5. Set A[K+1] = A[K]
6. Set K = K - 1
[End of While Loop]
6. Set A[K+1] = TEMP
[End of For Loop]
8. Exit
MERGE SORT
Merge Sort ( A, BEG, END ):
Description: Here A is an unsorted array. BEG is the lower bound and END is the upper
bound.
Partition ( ):
1. Set LEFT = BEG, RIGHT = END and LOC = BEG
2. Beginning with the element pointed by RIGHT, scan the array from right to left, comparing
each element with the element pointed by LOC until:
(a) Element smaller than the element pointed by LOC is found.
(b) Interchange elements pointed by LOC and RIGHT.
(c) If RIGHT becomes equal to LOC, terminate the subfunction Partition ( ).
3. Beginning with the element pointed by LEFT, scan the array from left to right, comparing
each element with the element pointed by LOC until:
(a) Element greater than the element pointed by LOC is found.
(b) Interchange elements pointed by LOC and LEFT.
(c) If LEFT becomes equal to LOC, terminate the subfunction Partition ( ).
4. Exit
QUICK SORT
Quick Sort ( A, BEG, END ):
Description: Here A is an unsorted array. BEG is the lower bound and END is the upper
bound.
1. If (BEG < END) Then
2. X = Partition (A, BEG, END)
3. Call Quick Sort (A, BEG, X – 1)
4. Call Quick Sort (A, X + 1, END)
[End of If]
5. Exit