Professional Documents
Culture Documents
DS Doc
DS Doc
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
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)
MERGE SORT
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 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
5. [Reset top to point to the new node at the top of the stack]
Set top = new
6. Exit.
5. Return.
Deque
ALGORITHM TO IMPLEMENT A QUEUE USING ARRAYS.
Procedure: Insert an element in the queue.
4. Return.
procedure AddToHead(element)
head = new Node(element, head)
if(not the Tail)
tail = head
end if
end procedure
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
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
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
5. Exit.
5. Exit.
if node.next = null
list.lastNode := node.prev
else
node.next.prev := node.prev
destroy node
6. Scan the leftmost symbol in the infix expression and let it be the
current input symbol.
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.
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