DS Alogrithms
DS Alogrithms
Algorithm: Let A [LB:UB] be a linear array. Any operation say PROCESS is to be applied to each element of array A.
Step 1 Set I:= LB , Step 2 Repeat step 3 and 4 while I <= UB , Step 3 Apply PROCESS to A [1]
Array Inserting
Here A is a linear array with M elements. A new element with value NEW is to be inserted at the Jth position.
Step 1 [Initialize counter] set I:= M , Step 2 Repeat steps 3 and 4 while I >= J
Step 3 Set A [1+1]:= A [1] , Step 4 Set I:=1-1 [end of step 2 loop] , Step 5 Set A [J]:= NEW
Array Deleting
Here A is a linear array with M elements. We want to delete the Jth element and we want to store it into the variable
DEL.
Step 3 Repeat steps 4 and 5 while I < M-1 , Step 4 Set A [I] = A [1+1]
Selection sort:
Algorithm: (Selection sort) Selection_Sort (A,N) Let A be a linear array with N elements i.e. A [1: N].
Step 3 [Make a pass and obtain element with smallest value] Repeat for I = K+1 TO N If MIN > A [I] then MIN: A [I] and
POSITION := 1 [End of If statement] [End of step 3 for Loop]
Step 1 Repeat steps 2 and 3 for I = 1 to N-1 Step 2 ,Step2 3 Repeat step 3 for J = 1 to N –I ,Step 3 [Exchange elements]
If A [J] > A [J+1] then TEMP : = A [J], A [J]:= A [J+1] ,A [J+1]:= TEMP [End of If statement] [End of step 2 for statement]
In insertion sort; we insert an element into its proper place in the previously sorted sublist. Consider an array A with N
elements. The steps can be stated as
Step 2 A[2] is inserted before or after A[1], so that A[1] and A[2] are sorted.
Step 3 Similarly A[3] is inserted so that A[1], A[2] and A[3] are sorted.
Step 4 This process is continued till all the elements are sorted.
Algorithm: (Insertion Sort) Insertion_Sort (A, N) Let A is a linear array with N elements i.e A [1: N]
[End of loop]
Merge sort
Procedure: merge (A, first, mid, last) Here, first, mid and last represent the first, middle and last position of array
A. In this procedure, we use an auxiliary array TEMP. The variables i, j, k are local integer variables.
Step 1 [Initialize]
i := first , j := mid + 1
k := first , Step 2
A[i] := TEMP[i] , [End of for loop] , Step 5 , [Finished] , Return , [End of while loop]
Write an algorithm for quick sort. Explain its complexity also. Algorithm Quicksort (A, LB, UB)
Given an array A having elements. This algorithm sorts this array in ascending orderusing quick sort method Band UB
denote position of the first and last elements respertively: I and I are array indicte. PIVOT contains the element to be
placed in its final position within the sorted subtable. TEMP is used for swapping of elements. FLAG is a logicalvariable
which indicates the end of the process that places the PIVOT in its final position.When FLAG becomes false, the given
array has been partitioned into three segments.
1. [Initialize]FLAG = TRUE
3. I = LB
Repeat While ( A[I] < PIVOT AND I <= UB) ,I = I + 1Repeat While ( A[J] > PIVOT AND J > LB )
8. End.
The above algorithm is used initially by the statement CALL Quicksort (A, 1, N) ,
Algorithm: postfix
Step 2 Scan E from left to right and repeat step 3 and 4 for each element of E until the sentinel ")" is encountered.
Step 3 , If element = operand then , PUSH the element on STACK , [End of If statement] , If element = operator then
Step 4 , (i) POP two top element from STACK. , (ii) Perform the desired operation. , (iii) PUSH the result of (ii) back on
STACK. [End of If Statement] ,[End of step 2 for loop]
Step 5 POP the top element on STACK and set it equal to VALUE of E.
Step 6 , Exit
Transforming Infix Expressions into Postfix Expressions The following algorithm transforms the infix
expression E into its equivalent postfix expression P using stack
Algorithm: , Step 1 , Push "(" onto STACK, and add ")" to the end of E. , Step 2
Scan E from left to right and repeat step 3 to 6 for , each element of E until the STACK is empty. , Step 3
If element = "(" [left parenthesis] then PUSH element on the STACK [End of If Statement]
(i) Repeatedly pop from STACK and add to P each operator which have the same or higher precedence than.
(ii) PUSH on the STACK. , [End of If Statement] , Step 6 , If element = ")" [right parenthesis] then
(i) Repeatedly pop from STACK and add to P each element until a left parenthesis "(" is encountered.
Transforming Infix Expressions into prefix or polish exp. The following algorithm transforms the infix
expression E into its equivalent pre polish expression P using stack .
Scan E' (reverse infix expression) from left to right and repeat step 3 to 7 for each element of E' until the STACK is empty.
Step 3 , If element = operand then Add the element to P [End of If Statement) , Step 4
If element = ")" [right parenthesis] then PUSH element on the STACK , [End of If Statement] , Step 5
(i) Repeatedly pop from STACK and add to P each operator which have the higher precedence than.
(i) Repeatedly pop from STACK and add to P each element until a right parenthesis ")" is encountered.
(ii) Remove")" [Do not add the ")" to P]. , [End of If Statement] , Step 7
If there is no more element in E', UNSTACK (Pop from STACK) the remaining elements and add them to P.
[End of step 2 loop] , Step 8 , Reverse the output expression P to get the desired result. , Step 9 , Exit
QUEUE INSERTION
Procedure: INSERT (Q, F, R, N, X) ,Here Q = Name of queue N = Maximum size of queue X = element to be inserted F =
Front Pointer Variable R = Rear Pointer Variable
Step 1 [Overflow condition ?] If R >N then write "overflow" and Return [End of If Statement]
Step 3 [Increment rear pointer] R:= R +1, Step 4 [Insert New element] Q[R] := X Step 5 [Finished] Return
QUEUE DELETION
Here Q = Name of queue ,X = Variable used to store the deleted element, F = Front Pointer Variable, R = Rear Pointer
Variable
Step 1 [Underflow ?] If F = 0 then write "underflow" [End of If Statement] ,Step 2 [Delete front element] X := Q (F)
Step 3 [Is queue now empty ?] If F = R then [ queue has only one element in start ] F: 0, R:= 0 and Return
[End of If Statement] , Step 4 [Increment front pointer] F:= F + 1 , Step 5 [Finished] Return
Step 1 Set PTR := FIRST , Step 2 Repeat steps 3 and 4 while PTR <> NULL , Step 3 Apply CHANGE to INFO (PTR)
Step 4 Set PTR := LINK (PTR) [End of Step 2 loop] , Step 5 Exit
Step 1 Set PTR := FIRST , Step 2 Repeat steps 3 while PTR < > NULL , Step 3 If INFO (PTR) = FIND then
Set LOC: PTR and exit ,Else Set PTR : LINK (PTR) [End of If Statement] [End of Step 2 Loop]
Algorithm: Searching sorted list SEARCH (INFO, LINK, FIRST, FIND, LOC)
Step 1 Set PTR := FIRST , Step 2 Repeat Steps 3 to 5 while PTR < > NULL ,Step 3 If INFO (PTR) > FIND then Set LOC:= NULL
and Exit [End of If Statement] , step 4 If INFO (PTR) = FIND then set LOC:= PTR and Exit [End of If Statement]
step 5 Set PTR := LINK (PTR) , step 6 Set LOC:= NULL , step 7 exit
Step 1 IF AVAIL = NULL then write 'OVERFLOW' and Exit , Step 2 [Obtain address of the new node] NEW: AVAIL
Step 3 [Remove the node from available list] AVAIL : LINK [AVAIL] , Step 4 [Initialize information and link field]
INFO (NEW) := NX ,LINK (NEW) = FIRST , Step 5 [Update FIRST] Set FIRST := NEW , Step 6 Exit
Algorithm: Delete (INFO, LINK, FIRST, AVAIL, P, PRAV) P is the location of node N (to be deleted) and PREV is the location
of the node preceding N
Step 1 If PREV = NULL then Set FIRST := LINK [FIRST] [Deletion of first node] ,Else Set LINK [PREV] := LINK [P] [Deletion of
node N] [End of If Statement] , STEP2 [Insert deleted node to the free-storage list] Set LINK [P] := AVAIL ,AVAIL := P
STEP 3 Exit
DOUBLY LINK LIST INSERTION:
Algorithm: Insert-double (FIRST, LAST, P, NEW,X Here X contains the Information contents of the new node.
Step 1 NEW NODE NODE , Step 2 INFO (NEW) := X , Step 3 [Empty list ?] If LAST = NULL then FIRST := NEW,
LAST := NEW,LPTR (NEW) := NULL ,RPTR [NEW] := NULL and Exit [End of If Statement]
STEP 4 [Right most insertion] If P = LAST then LAST := NEW LPTR (NEW) := P ,RPTR (NEW) := NULL ,RPTR (P):= NEW ,
Exit ,[End of If Statement] , Step 5 [Insert within list] LPTR (NEW) := P,RPTR (NEW) := RPTR (P),LPTR (RPTR (P)) := NEW
Step 1 [Delete node] RPTR (LPTR (P)) := RPTR (P) LPTR (RPTR (P)) := LPTR (P)
Step 2 [Insert the deleted node into AVAIL List] RPTR [P]:= AVAIL and AVAIL := P
Step 3 Exit