Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

LINEAR SEARCH

START LOCATION= -1
READ THE ARRAY A[MAX]
ENTER THE VALUE TO BE SEARCHED, M
I= 1
DO WHILE I <=MAX && LOCATION =-1
COMPARE A[I] AND M
IF A[I] =M, THEN
LOCATION = I
I = I+1
END DO
IF LOCATION = -1
PRINT VALUE NOT FOUND
ELSE
PRINT VALUE FOUND
PRINT THE LOCATION
END IF
STOP

BINARY SEARCH

READ THE SORTED ARRAY OF SIZE N


LB = 0, UB = N-1
LOC = -1
ENTER THE ELEMENT TO BE SEACHED, M
DO WHILE LB<= UB && LOC = -1
MID = (LB+UB)/2
IF A[MID] =M
LOC=MID
ELSE
IF A[MID] < M
LB = MID+1
ELSE
UB=MID -1
END IF
END IF
END DO
IF LOC = -1
PRINT VALUE NOT FOUND
ELSE
PRINT VALUE FOUND
PRINT THE LOC
END IF
END
INSERTION SORT

FOR I =1 TO N-1
TEMP = A[I] //FIRST ELEMENT OF UNSORTED PART
J = I-1 // LAST EMEMENT OF SORTED PART
WHILE (TEMP<= A[J]&&J>=0)
A[J+1] = A[J]
J=J-1
END WHILE
A[J+1] = TEMP
END FOR
PRINT THE LIST

BUBBLE SORT
C=1
DO WHILE (C<= SIZE - 1)
I=0
DO WHILE (I<SIZE - C)
IF LIST[I] < LIST[I+1]
TEMP = LIST[I]
LIST[I] = LIST[I+1]
LIST[I+1] = TEMP
END IF
I = I+1
END DO
C = C+1
END DO

SELECTION SORT

FOR I = 0 TO N – 2
MIN=ARR[I]
LOC = I
FOR J = I+1 TO N – 1
IF ARR[J] <MIN ,THEN
MIN=ARR[J]
LOC = J
END IF
END FOR
TEMP = ARR[I]
ARR[I] = MIN
ARR[LOC] = TEMP
TEMP
QUICK SORT
Q_SORT(ARR, FIRST, LAST)
LOW=FIRST,HIGH = LAST, PIVOT = ARR[(LOW+HIGH)/2]
DO WHILE (LOW <= HIGH)

DO WHILE (ARR[LOW]> PIVOT)


LOW = LOW+1
END DO

DO WHILE (ARR[HIGH] > PIVOT)


HIGH = HIGH – 1
END DO

IF LOW <= HIGH, THEN


TEMP = ARR[LOW]
ARR[LOW] = ARR[HIGH]
ARR[HIGH] = TEMP
LOW = LOW + 1
HIGH = HIGH – 1
END IF
END DO

IF FIRST < HIGH, THEN CALL Q_SORT(ARR,FIRST,HIGH)

IF LOW < LAST, THEN CALL Q_SORT(ARR,LOW,LAST)

MERGE SORT

MERGE (LOW, MID, HIGH, LIST, FINAL)


MID = (LOW + HIGH)/2
A = LOW
B = LOW
C = MID +1

WHILE (A<= MID &&C <= HIGH)


IF LIST[A] <= LIST[C]
FIND [B] = LIST[C]
A = A+1
ELSE
FIND[B] = LIST [C]
C = C+1
END IF
B=B+1
END WHILE

IF (A>B)
FOR(D = C TO HIGH ), DO
FIND[B] = LIST[D]
B = B +1
END FOR
ELSE
FOR(D = A TO MID ), DO
FIND[B] = LIST[D]
B = B+1
END FOR
END IF

SHELL SORT
S_SORT( ARR, SIZE)
ARR -> REPRESENTS THE LIST OF ELEMENTS
SIZE-> RWPRESENTS THE SIZE OF THE LIST
INITIALIZE THE GAP = GAP/2
DO WHILE GAP = GAP/2
SWAP = 0
DO WHILE (SWAP)
FOR I = 1 TO SIZE – GAP, DO
IF(ARR[I] > ARR[I+1])
TEMP = ARR[I]
ARR[I] = ARR[I+1]
ARR[I+1] = TEMP
SWAP = 1
END IF
END FOR
END OF DO
END OF DO

RADIX SORT

Step 1 : larg = Large (rec)


m = num (larg)

Step 2: Repeat through step 5 for j = 0,1,2,……..,m

Step 3: Repeat for I = 0,1,2,……,9


pocket [i] = NULL
pocket1[i] = NULL

Step 4: r = rec
repeat while r <> NULL
dig = digit (link [r], j)
Update (dig, r)
nex = link [r]
r = nex
if (r <> NULL)
dig = digit (link[r], j)
Update(dig, r)

Step 5: poc = 0
Repeat while pocket1[poc] = NULL
poc = poc + 1
rec = Make_link (poc, rec)

Step 6: Return

ALGORITHM TO IMPLEMENT A STACK USING SINGLY LINKED LIST.

Procedure: Push an element into the stack.

1. [Check overflow condition]


If Stk = NULL
Output “Overflow” and exit.

2. [Remove first node from stk lit]


Set new = Stk
Stk = link[Stk]

3. [Copy item into new node]


Set info[new] = item

4. [New node points to the original top node in the stack]


Set link[new] = top

5. [Reset top to point to the new node at the top of the stack]
Set top = new

6. Exit.

Procedure: Pop an element from the stack.

1. [Checks underflow condition]


If top = NULL
Output “Underflow” and return.

2. [Copy the top elementof stack into item]


Set item = info[top]

3. [Assign old top value to temp and reset top pointer]


temp = top
top = link[top]
4. [Return deleted node to the Stk list]
Set link[temp] = Stk
Stk = temp

5. Return.

ALGORITHM TO IMPLEMENT A QUEUE USING


ARRAYS
Procedure : Insert an element in the queue

if (rear >= size) // [check overflow condition]


print OVERFLOW and exit
else
rear = rear + 1
Q[rear = value] // insert an element
If (front = 0) //set front pointer
Front = 1
End if
Return

Procedure : Delete an element from the queue


If (front = 0) // check underflow condition
Print UNDERFLOW and exit
Else
Value = Q[front]
If (front = rear) //check for empty queue
Front = 0
Rear = 0
Else
Front = front +1
End if
End if
Return

Deque
ALGORITHM TO IMPLEMENT A QUEUE USING ARRAYS.
Procedure: Insert an element in the queue.

1. [Check overflow condition]


If rear >= size
Output “Overflow” and exit.

2. [Increment rear pointer]


rear = rear+1
3. [Insert an element]
Q[rear] = value

4. [Set the front pointer]


If front = 0
front=1
Return.

Procedure: Delete an element from the queue.

1. [Check underflow condition]


If front = 0

Output “Underflow” and return.


2. [Remove an element]
value = Q[front]

3. [Check for empty queue]


If front = rear
front = 0
rear = 0
Else
Front = front+1

4. Return.

IMPLEMENT SINGLE LINKED LIST


//Add an element to the head of the linkded list
procedure AddTohead (element)
head = new Node (element, head)
if (not the Tail)
Tail = head
End if
End procedure

// Add an element to the tail of the linked list


procedure AddToTail (element)
if (this is the Tail)
Tail -> next = new Node (element)
Tail = Tail-> next
End if
Else head = Tail = new Node (element)
End procedure

// Remove an element from the head of the linked list


procedure RemoveFromHead () : integer
if (this is the Head)
element = head ->info
temp = head
if (this is the last item in the list)
Break pointer connection between head and tail to terminate list
Else
Set = head->next = next item in the list
End if
Release temp from memory pool
Return element
Else
Return 0
End if
End procedure

// Remove an element from the tail of the linked list


procedure RemoveFromTail () : integer
if (this is the Tail)
element = Tail->info
if (this is the last item in the Tail)
Break pointer connection between head and Tail to terminate list
Release head from memory pool
Else
Traverse list until we reach the Tail
Release Tail from the list
Set Tail pointer to previous element if previous element exist
Set Tail pointer to next to null
End if
Return element
Else
Return 0
End if
End procedure

ALGORITHM :IMPLEMENT SINGLY LINKED LIST


//Add an element to the start of the linked list

procedure AddToHead(element)
head = new Node(element, head)
if(not the Tail)
tail = head
end if
end procedure

// Add an element to the tail of the linked list

procedure AddToTail(element)
if(this is the Tail)
tail->next = new Node(el)
tail = tail->next
endif
else head = tail = new Node(el)
end procedure

// Remove an element from the head of the linked list

procedure RemoveFromHead():integer
if(this is the Head)
element = head->info
temp = head
if(this is the last item in the list)
Break pointer connection between head and tail to
terminate list
else
set head.next to equal next item in list
end if
release temp from from memory pool
return element
else
return 0
end if
end procedure

// Remove an element from the tail of the linked list

procedure RemoveFromTail():integer
if(this is the Tail)
element = tail->info
if(this is the last item in the list)
Break pointer connection between head and tail to terminate
list release head from memory pool.
else
Traverse list until you reach the tail release tail from
memory pool
set tail pointer to previous element if one exists set
tail's pointer to next element to null.
end if
return element
else
return 0
end if
end procedure

ALGORITHM TO IMPLEMENT A QUEUE USING SINGLY LINKED LIST


Procedure: Insert an element into the queue.

1. [Check overflow condition]


If Q = NULL
Output “Overflow” and return

2. [Remove first node from Q list]


Set new= Q
Q = link[Q]

3. [Copy item into new node]


info[nwew] = item
link[new] = NULL

4. [If Q is empty then item is the first element in the queue Q]


If (front = NULL)
Front = rear = new
Else
Set link[rear] = new
rear = new

5. Exit.

Procedure: Delete an element from the queue.


1. [Check underflow condition]
If (front = NULL)
Output “Underflow” and return.

2. [If Q nonempty, copy front in temp]


Set temp = front
item=info[temp]

3. [Reset front to point to the next element in the Q]


front = link[temp]

4. [Return the deleted node temp to the list Q]


link[temp] = Q
Q = temp

5. Exit.

IMPLEMENT DOUBLY LIST


//Add an element after specified position in the list
procedure insertAfter (List list, Node node, Node newNode)
newNode->prev = node
newNode->next = node->next
if (node->next = NULL)
list->lastNode
else
node->next = newNode
node->next = newNode

//Add an element before specified position in the linked list

procedure insertBefore (List list, Node node, Node newNode)


newNode->prev = node->prev
newNode->next = node
if (node->prev = NULL)
list->firstNode = newNode
else
node->prev->next = newNode
node->prev = newNode

//Add an element to the start of the linked list

procedure insetrtBeginning (List list, Node node, Node newNode)


if (list->firstNode = NULL)
list->firstNode = newNode
list->lastNode = newNode
newNode->prev = NULL
newNode->next = NULL
else
insert (list, list->firstNode, newNode)

//add an element to the end of the linked list

procedure insertEnd (List list, Node newNode)


if (list->lastNode = NULL)
insertBeginning (list, newNode)
else
insertAfter (list, list->lastNode, newNode)

ALGORITHM : IMPLEMENT DOUBLY LINKED LIST


//Add an element after specified position in the linked list

procedure insertAfter(List list, Node node, Node newNode)


newNode.prev := node
newNode.next := node.next
if node.next = null
list.lastNode := newNode
else
node.next.prev := newNode
node.next := newNode

//Add an element before specified position in the linked list

procedure insertBefore(List list, Node node, Node newNode)


newNode.prev := node.prev
newNode.next := node
if node.prev is null
list.firstNode := newNode
else
node.prev.next := newNode
node.prev := newNode

//Add an element to the start of the linked list

procedure insertBeginning(List list, Node newNode)


if list.firstNode = null
list.firstNode := newNode
list.lastNode := newNode
newNode.prev := null
newNode.next := null
else
insertBefore(list, list.firstNode, newNode)

//Add an element to the end of the linked list

procedure insertEnd(List list, Node newNode)


if list.lastNode = null
insertBeginning(list, newNode)
else
insertAfter(list, list.lastNode, newNode)
// Remove an element from the linked list

procedure remove(List list, Node node)


if node.prev = null
list.firstNode := node.next
else
node.prev.next := node.next

if node.next = null
list.lastNode := node.prev
else
node.next.prev := node.prev
destroy node

ALGORITHM TO CONVERT INFIX EXPRESSION TO POSTFIX FORM.


1. Initialize the stack.

2. Scan the leftmost symbol in the given infix expression and


denote it as current input symbol.

3. Repeat through step 6 while infix expression.

4. Remove and output all stack symbols whose precedence values


are greater than or equal to the precedence of the current input
symbol.

5. Push the current input symbols onto the stack.

6. Scan the leftmost symbol in the infix expression and let it be the
current input symbol.

ALGORITHM TO CONVERT INFIX EXPRESSION TO POSTFIX FORM

1. Initialize the stack


2. Scan the leftmost symbol in the given infix expression and denote it as
current input symbol.
3. Repeat through step 6 while infix expression.
4. Remove and output all stack symbols whose precedence values are
greater than or equal to the precedence of the current input symbol.
5. Push the current input symbols onto the stack.
6. Scan the leftmost symbol in the infix expression and let it be the
current input symbol.

ALGORITHM TO EVALUATE THE POSTFIX EXPRESSION.


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

2. Scan P from left to right and repeat steps 3 and 4 for each element
of P until the sentinel “)” is encountered.
3. If an operand is encountered, put it on STACK.

4. If an operator x is encountered , then:


a) Remove the two top elements of STACK, where A is the top
element and B is the next-to-top element.
b) Evaluate B x A.

5. Place the result of (b) back on Stack.

6. Set Value equal to the top element on STACK.

7. Exit.

ALGORITHM TO EVALUATE THE POSTFIX EXPRESSION

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


2. Scan P from left to right and repeat steps 3 and 4 for each element of P until
the sentinel “)” is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator X is encountered, then :
remove the two top elements of stack and perform the operation X
5. Place the evaluated result back on the STACK
6. Set value equal to the top element on STACK
7. Exit

BFS
Input : V, the starting vertex
Output : A list VISIT giving the order of visit of vertices during traversal
Data Structure : linked structure of graph . gptr is the pointer to a Graph.

Steps :
If gptr = NULL then
Print graph is empty and exit
Else
u=v // starts from v
open.ENQUENE (u) enter the starting vertex in openq
while (openq.STATUS != EMPTY) // till the openq is not empty
u = openq.DEQUENE ()
if (SEARCH_SL_(VISIT, u) = FALSE) then //if u is not VISIT
// then VISIT it
INSERT_SL_END (VISIT, u) // store u in VISIT
ptr = GPTR[u]
while (ptr.link!= NULL) do // to store all the adjacent
//vertices of V in openq
vptr = ptr.link
openq.ENQUENE (vptr.LABEL)
end while
end if
end while
end if
return (VISIT)
Stop

DFS
Input : v, the starting vertex
Output : A list VISIT giving the order of visit of vertices during Traversal
Data Structure : linked structure of graph. GPTR is the pointer to a Graph

Steps:
1 if GPTR = NULL
1 print GRAPH IS EMPTY
2 EXIT
2 end if
3 u = v //start from V
4 open.push (u) //push the starting vertex in OPEN

5 while (Open.Top!= NULL) //till the stack is not empty


1 u = Open.pop () //pop the top element from Open
2 if (SEARCH_SL_END (VISIT, u) = FALSE) then //if u is not visited
1 INSERT_SL_END (VISIT , u) //Store u in VISIT
2 Ptr = GPTR [u] //To push all adjacent vertices of u into
stack
3 While (ptr.link!=NULL) do
vptr = ptr.link
Open.push (vptr.LABLE)
4 end while
3 end if
6 End while
7 Return (VISIT)
8 STOP

You might also like