You are on page 1of 37

INSERT UNSORTED

Insert Unsorted ( ):

Description: Here A is a sorted linear array with N elements. LOC is the location where
ITEM is to be inserted.

1. Set I = N [Initialize counter]


2. Repeat While (I >= LOC)
3. Set A[I+1] = A[I] [Move elements downward]
4. Set I = I – 1 [Decrease counter by 1]
[End of While Loop]
5. Set A[LOC] = ITEM [Insert element]
6. Set N = N + 1 [Reset N]
7. Exit

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.

1. Set I = N [Initialize counter]


2. Repeat While (ITEM < A[I]) and (I >= 1)
3. Set A[I+1] = A[I] [Move elements downward]
4. Set I = I – 1 [Decrease counter by 1]
[End of While Loop]
5. Set A[I+1] = ITEM [Insert element]
6. Set N = N + 1 [Reset N]
7. Exit

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.

1. Set ITEM = A[LOC] [Assign the element to be deleted to ITEM]


2. Repeat For I = LOC to N
3. Set A[I] = A[I+1] [Move the Ith element upwards]
[End of For Loop]
4. Set N = N – 1 [Reset N]
5. Exit

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.

1. Set I = J = K = 1 [Initialize counters]


2. Repeat While (I <= M) and (J <= N)
3. If (A[I] < B[J]) Then
4. Set C[K] = A[I] [Assign elements of array A to array C]
5. Set I = I + 1
6. Else
7. Set C[K] = B[J] [Assign elements of array B to array C]
8. Set J = J + 1
[End of If]
9. Set K = K + 1
[End of While Loop]
10. If (I > M) Then [Array A is empty]
11. Repeat While (J <= N)
12. Set C[K] = B[J] [Assign the remaining elements of array B to array C]
13. Set J = J+1 and K = K+1
[End of While Loop]
[End of If]
14. If (J > N) Then [Array B is empty]
15. Repeat While (I <= M)
16. Set C[K] = A[I] [Assign the remaining elements of array A to array C]
17. Set I = I+1 and K = K+1
[End of While Loop]
[End of If]
18. Exit

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:

C[I][J] = A[I][J] + B[I][J]


MULTIPLICATION
Multiply ( ):
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 multiplies these two arrays.

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:

C[I][J] = C[I][J] + A[I][K] * B[K][J]


TRAVERSE A LINKED – LIST
Traverse ( ):
Description: Here START is a pointer variable which contains the address of first node.
PROCESS is any operation that is to be performed on the node.

1. Set PTR = START


2. Repeat While (PTR != NULL)
3. Apply PROCESS to PTR->INFO
4. PTR = PTR->LINK
[End of While Loop]
5. Exit
REVERSE A LINKED – LIST
Reverse ( ):
Description: Here START is a pointer variable which contains the address of first node. PTR
will point to the current node and PREV will point to the previous node. REV will maintain
the reverse list.

1. Set PTR = START, PREV = NULL


2. Repeat While (PTR != NULL)
3. REV = PREV
4. PREV = PTR
5. PTR = PTR->LINK
6. PREV->LINK = REV
[End of While Loop]
7. START = PREV
8. Exit
INSERT AS FIRST NODE IN A LINKED – LIST
Insert First ( ):
Description: Here START is a pointer variable which contains the address of first node.
ITEM is the value to be inserted.

1. If (START == NULL) Then


2. START = New Node [Create a new node]
3. START->INFO = ITEM [Assign ITEM to INFO field]
4. START->LINK = NULL [Assign NULL to LINK field]
5. Else
6. Set PTR = START [Initialize PTR with START]
7. START = New Node [Create a new node]
8. START->INFO = ITEM [Assign ITEM to INFO field]
9. START->LINK = PTR [Assign PTR to LINK field]
[End of If]
10. Exit
INSERT AS LAST NODE IN A LINKED – LIST
Insert Last ( ):
Description: Here START is a pointer variable which contains the address of first node.
ITEM is the value to be inserted.

1. If (START == NULL) Then [Check whether list is empty]


2. START == New Node
3. START->INFO = ITEM
4. START->LINK = NULL
5. Else
6. Set PTR = START [Initialize PTR with START]
7. Repeat While (PTR->LINK != NULL)
8. PTR = PTR->LINK [Until PTR reaches last node]
[End of While Loop]
9. PTR->LINK = New Node [Assign address of new node to
PTR]
10. PTR = PTR->LINK [Move PTR to next node]
11. PTR->INFO = ITEM
12. PTR->LINK = NULL
[End of If]
13. Exit
INSERT AT ANY SPECIFIC NODE IN A LINKED – LIST
Insert Specific ( ):
Description: Here START is a pointer variable which contains the address of first node.
NEW is a pointer variable which will contain address of new node. N is the value after which
new node is to be inserted and ITEM is the value to be inserted.

1. If (START == NULL) Then


2. Print: Linked-List is empty. It must have at least one node
3. Else
4. Set PTR = START, NEW = START
5. Repeat While (PTR != NULL)
6. If (PTR->INFO == N) Then
7. NEW = New Node
8. NEW->INFO = ITEM
9. NEW->LINK = PTR->LINK
10. PTR->LINK = NEW
11. Print: ITEM inserted
12. ELSE
13. PTR = PTR->LINK
[End of Step 6 If]
[End of While Loop]
[End of Step 1 If]
14. Exit
INSERT INTO SORTED LINKED – LIST
Insert Sorted ( ):
Description: Here START is a pointer variable which contains the address of first node.
PREV is a pointer variable which contains address of previous node. ITEM is the value to be
inserted.

1. If (START == NULL) Then [Check whether list is empty]


2. START = New Node [Create a new node]
3. START->INFO = ITEM [Assign ITEM to INFO field]
4. START->LINK = NULL [Assign NULL to LINK field]
5. Else
6. If (ITEM < START->INFO) Then [Check whether ITEM is less then
value in first node]
7. PTR = START
8. START = New Node
9. START->INFO = ITEM
10. START->LINK = PTR
11. Else
12. Set PTR = START, PREV = START
13. Repeat While (PTR != NULL)
14. If (ITEM < PTR->INFO) Then
15. PREV->LINK = New Node
16. PREV = PREV->LINK
17. PREV->INFO = ITEM
18. PREV->LINK = PTR
19. Return
20. Else If (PTR->LINK == NULL) Then [Check whether PTR
reaches last node]
21. PTR->LINK = New Node
22. PTR = PTR->LINK
23. PTR->INFO = ITEM
24. PTR->LINK = NULL
25. Return
26. Else
27. PREV = PTR
28. PTR = PTR->LINK
[End of Step 14 If]
[End of While Loop]
[End of Step 6 If]
[End of Step 1 If]
29. Exit
DELETE FIRST NODE IN A LINKED – LIST
Delete First ( ):
Description: Here START is a pointer variable which contains the address of first node.
ITEM is the value to be deleted.

1. If (START == NULL) Then [Check whether list is empty]


2. Print: Linked-List is empty.
3. Else
4. PTR = START
5. ITEM = START->INFO [Assign INFO of first node to ITEM]
6. START == START->LINK [START now points to 2nd node]
7. Delete PTR [Delete first node]
8. Print: ITEM deleted
[End of If]
9. Exit
DELETE LAST NODE IN A LINKED – LIST
Delete Last ( ):
Description: Here START is a pointer variable which contains the address of first node. PTR
is a pointer variable which contains address of node to be deleted. PREV is a pointer variable
which points to previous node. ITEM is the value to be deleted.

1. If (START == NULL) Then [Check whether list is empty]


2. Print: Linked-List is empty.
3. Else
4. PTR = START, PREV = START
5. Repeat While (PTR->LINK != NULL)
6. PREV = PTR [Assign PTR to PREV]
7. PTR = PTR->LINK [Move PTR to next node]
[End of While Loop]
8. ITEM = PTR->INFO [Assign INFO of last node to ITEM]
9. If (START->LINK == NULL) Then [If only one node is left]
10. START = NULL [Assign NULL to START]
11. Else
9. PREV->LINK = NULL [Assign NULL to link field of second last node]
[End of Step 9 If]
10. Delete PTR
11. Print: ITEM deleted
[End of Step 1 If]
12. Exit
DELETE ANY SPECIFIC NODE IN A LINKED – LIST
Delete Specific ( ):
Description: Here START is a pointer variable which contains the address of first node. PTR
is a pointer variable which contains address of node to be deleted. PREV is a pointer variable
which points to previous node. ITEM is the value to be deleted.

1. If (START == NULL) Then [Check whether list is empty]


2. Print: Linked-List is empty.
3. Else If (START->INFO == ITEM) Then [Check if ITEM is in 1st node]
4. PTR = START
5. START = START->LINK [START now points to 2nd node]
6. Delete PTR
7. Else
8. PTR = START, PREV = START
9. Repeat While (PTR != NULL)
10. If (PTR->INFO == ITEM) Then [If ITEM matches with PTR->INFO]
11. PREV->LINK = PTR->LINK [Assign LINK field of PTR to PREV]
12. Delete PTR
13. Else
14 PREV = PTR [Assign PTR to PREV]
15. PTR = PTR->LINK [Move PTR to next node]
[End of Step 10 If]
[End of While Loop]
16. Print: ITEM deleted
[End of Step 1 If]
17. Exit
SEARCH SORTED LINKED – LIST
Search Sorted ( ):
Description: Here START is a pointer variable which contains the address of first node.
ITEM is the value to be searched.

1. Set PTR = START, LOC = 1 [Initialize PTR and LOC]


2. Repeat While (PTR != NULL)
3. If (ITEM > PTR->INFO) Then [Check if ITEM is greater then INFO field]
4. PTR = PTR->LINK [Move PTR to next node]
5. LOC = LOC + 1 [Increment LOC]
6. Else If (ITEM == PTR->INFO) Then [Check if ITEM matches with INFO field]
7. Print: ITEM is present at location LOC
8. Return
9. Else
10. Print: ITEM is not present in the list
11. Return
12. [End of If]
13. [End of While Loop]
14. Exit
SEARCH UNSORTED LINKED – LIST
Search Unsorted ( ):
Description: Here START is a pointer variable which contains the address of first node.
ITEM is the value to be searched.

1. Set PTR = START, LOC = 1 [Initialize PTR and LOC]


2. Repeat While (PTR != NULL)
3. If (ITEM == PTR->INFO) Then [Check if ITEM matches with INFO field]
4. Print: ITEM is present at location LOC
5. Return
6. Else
7. PTR = PTR->LINK [Move PTR to next node]
8. LOC = LOC + 1 [Increment LOC]
9. [End of If]
10. [End of While Loop]
11. Print: ITEM is not present in the list
12. Exit
PUSH
Push ( ):
Description: Here STACK is an array with MAX locations. TOP points to the top most
element and ITEM is the value to be inserted.

1. If (TOP == MAX) Then [Check for overflow]


2. Print: Overflow
3. Else
4. Set TOP = TOP + 1 [Increment TOP by 1]
5. Set STACK[TOP] = ITEM [Assign ITEM to top of STACK]
6. Print: ITEM inserted
[End of If]
7. Exit
POP
Pop ( ):
Description: Here STACK is an array with MAX locations. TOP points to the top most
element.

1. If (TOP == 0) Then [Check for underflow]


2. Print: Underflow
3. Else
4. Set ITEM = STACK[TOP] [Assign top of STACK to ITEM]
5. Set TOP = TOP - 1 [Decrement TOP by 1]
6. Print: ITEM deleted
[End of If]
7. Exit
TRANSFORM
Transform ( ):
Description: Here I is an arithmetic expression written in infix notation and P is the
equivalent postfix expression generated by this algorithm.

1. Push “(“ left parenthesis onto stack.


2. Add “)” right parenthesis to the end of expression I.
3. Scan I from left to right and repeat step 4 for each element of
I until the stack becomes empty.
4. If the scanned element is:
(a) an operand then add it to P.
(b) a left parenthesis then push it onto stack.
(c) an operator then:
(i) Pop from stack and add to P each operator
which has the same or higher precedence then
the scanned operator.
(ii) Add newly scanned operator to stack.
(d) a right parenthesis then:
(i) Pop from stack and add to P each operator
until a left parenthesis is encountered.
(ii) Remove the left parenthesis.
[End of Step 4 If]
[End of step 3 For Loop]
5. Exit.
EVALUATE
Evaluate ( ):
Description: Here P is a postfix expression and this algorithm evaluates it.

1. Add a “)” right parenthesis at the end of P.


2. Scan P from left to right and repeat steps 3 & 4 for each
element of P until “)” is encountered.
3. If an operand is encountered, push it onto stack.
4. If an operator is encountered then:
(a) Pop the top two elements from stack, where A is the
top element and B is the next to top element.
(b) Evaluate B A.
(c) Place the result of (b) back on stack.
[End of Step 4 If]
[End of step 2 For Loop]
5. Set VALUE equal to the top element on the stack.
6. Exit.
INSERT
Insert ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear of the QUEUE. ITEM is the value to be inserted.

1. If (REAR == N) Then [Check for overflow]


2. Print: Overflow
3. Else
4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else
6. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM inserted
[End of Step 1 If]
9. Exit
DELETE
Delete ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear of the QUEUE.

1. If (FRONT == 0) Then [Check for underflow]


2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [Check if only one element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else
7. Set FRONT = FRONT + 1 [Increment FRONT by 1]
[End of Step 5 If]
8. Print: ITEM deleted
[End of Step 1 If]
9. Exit
INSERT INTO CIRCULAR QUEUE
Insert Circular ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear elements of the QUEUE. ITEM is the value to be inserted.

1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then


2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted
[End of Step 1 If]
11. Exit
DELETE FROM CIRCULAR QUEUE
Delete Circular ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear elements of the QUEUE.

1. If (FRONT == 0) Then [Check for Underflow]


2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT by 1]
[End of Step 5 If]
10. Print: ITEM deleted
[End of Step 1 If]
11. Exit
LINEAR SEARCH
Linear Search ( ):
Description: Here A is an array having N elements. ITEM is the value to be searched.

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. Set BEG = 1 and END = N


2. Set MID = (BEG + END) / 2
3. Repeat While (BEG <= END) and (A[MID] ≠ ITEM)
4. If (ITEM < A[MID]) Then
5. Set END = MID – 1
6. Else
7. Set BEG = MID + 1
[End of If]
8. Set MID = (BEG + END) / 2
[End of While Loop]
9. If (A[MID] == ITEM) Then
10. Print: ITEM exists at location MID
11. Else
12. Print: ITEM doesn’t exist
[End of If]
13. Exit
BUBBLE SORT
Bubble Sort ( ):
Description: Here A is an unsorted array having N elements.

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.

1. If (BEG < END) Then


2. Set MID = (BEG + END) / 2
3. Call Merge Sort (A, BEG, MID)
4. Call Merge Sort (A, MID + 1, END)
5. Call Merge Array (A, BEG, MID, END)
[End of If]
6. Exit
Merge Array ( A, BEG, MID, END )
Description: Here A is an unsorted array. BEG is the lower bound, END is the upper bound
and MID is the middle value of array. B is an empty array.

1. Repeat For I = BEG to END


2. Set B[I] = A[I] [Assign array A to B]
[End of For Loop]
3. Set I = BEG, J = MID + 1, K = BEG
4. Repeat While (I <= MID) and (J <= END)
5. If (B[I] <= B[J]) Then [Assign smaller value to A]
6. Set A[K] = B[I]
7. Set I = I + 1 and K = K + 1
8. Else
9. Set A[K] = B[J]
10. Set J = J + 1 and K = K + 1
[End of If]
[End of While Loop]
11. If (I <= MID) Then [Check whether first half
12. Repeat While (I <= MID) has exhausted or not]
13. Set A[K] = B[I]
14. Set I = I + 1 and K = K + 1
[End of While Loop]
15. Else
16. Repeat While (J <= END)
17. Set A[K] = B[J]
18. Set J = J + 1 and K = K + 1
[End of While Loop]
[End of If]
19. Exit
QUICK SORT
Quick Sort ( A, BEG, END ):
Desciption: Here A is an unsorted array having N elements. BEG is the lower bound and END
is the upper bound.

1. If ( BEG < END ) then


2. Find the element that divides the array into two parts using subfunction Partition( ).
3. Quick Sort ( Left Half )
4. Quick Sort ( Right Half )
[ End of If ]
5. Exit

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

Partition ( A, BEG, END )


Description: Here A is an unsorted array. BEG is the lower bound, END is the upper bound.

1. Set LOC = BEG


2. Repeat While (True)
3. Repeat While (A[LOC] <= A[END]) and (LOC != END)
4. END = END – 1
[End of While Loop]
5. If (LOC == END) Then
6. Return LOC
[End of If]
7. Interchange A[LOC] and A[END]
8. Set LOC = END
9. Repeat While (A[LOC] >= A[BEG]) and (LOC != BEG)
10. BEG = BEG + 1
[End of While Loop]
11. If (LOC == BEG) Then
12. Return LOC
[End of If]
13. Interchange A[LOC] and A[BEG]
14. Set LOC = BEG
[End of While Loop]
15. Exit

You might also like