0% found this document useful (0 votes)
50 views8 pages

DS Alogrithms

The document discusses various algorithms for array and linked list operations like traversing, searching, sorting, insertion, and deletion. Array operations covered include traversing, inserting, deleting and sorting algorithms like selection sort, bubble sort, insertion sort, merge sort and quicksort. Linked list operations covered include traversing, searching, insertion and their algorithms.

Uploaded by

Nikhil Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views8 pages

DS Alogrithms

The document discusses various algorithms for array and linked list operations like traversing, searching, sorting, insertion, and deletion. Array operations covered include traversing, inserting, deleting and sorting algorithms like selection sort, bubble sort, insertion sort, merge sort and quicksort. Linked list operations covered include traversing, searching, insertion and their algorithms.

Uploaded by

Nikhil Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Array traversing

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]

Step 4 Set I:=1+1 , Step 5 [End of Step2 Loop] , Exit

Array Inserting

Algorithm: INSERT (A, M, J, NEW)

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

Step 6 Set M: M +1 , Step 7 Exit

Array Deleting

Algorithm: DELETE (A, M, J, DEL)

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 1 Set DEL: A [J] , Step 2 Set I: = J

Step 3 Repeat steps 4 and 5 while I < M-1 , Step 4 Set A [I] = A [1+1]

Step 5 Set I:= 1 + 1 , [end of step 3 loop]

Step 6 Set M: M-1 , Step 7 End

Selection sort:

Algorithm: (Selection sort) Selection_Sort (A,N) Let A be a linear array with N elements i.e. A [1: N].

Step 1 Repeat steps 2 to 4 for K =1 TO N – 1 , Step 2 Set MIN: A [K] POSITION : K

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 4 [Exchange elements] If POSITION <> K then

TEMP: A [K] A [K]:= A [POSITION] ,A [POSITION] := TEMP

[End of If statement] [End of Step 1 for loop]

Step 5 [Finished] Exit

Algorithm: (Bubble Sort) Bubble_Sort (A,N)

Let A be a linear array with N elements i.e. A [1: N].

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]

[End of step 1 for statement] ,Step 4 Exit


Insertion sort:

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 1 A[1] by itself is sorted

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]

Insertion sort algorithm

Step 1 Repeat Step 2 to 4 for I =2 To N

Step 2 Set TEMP: A [1] , POSITION:=1-1

Step 3 [Move down 1 Position all elements greater than TEMP]

Repeat While TEMP < A [POSITION] and POSITION > 1

(i) Set A [POSITION + 1]:= A [POSITION]

(ii) Set POSITION:= POSITION - 1

[End of loop]

[Insert TEMP at proper position]

Step 4 Set A [POSITION +1] := TEMP

[End of Step 1 for Loop]

Step 5 [Finished] Exit

 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

[Compare elements and output the smaller in

array TEMP] , Repeat while (i < mid) and (j < last)

If (A[i] < A[j]) then , TEMP[k] := A[i]

i :=i+1 , k := k+1 , ELSE

TEMP [k] := A[j] , j:=j+1 , k := k +1

[End of if statement] , Step 3


[Copy the remaining unprocessed elements into the array TEMP]

Repeat while (i < = mid) , TEMP [k] := A[i] , k := k + 1 , i := i +1

[End of loop] , Repeat while (j <= last) , TEMP [k] := A[j]

k := k + 1 , j := j +1 , [End of loop] , Step 4

[Copy array TEMP into array A] , Repeat for i = first to last

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

2. [Perform sorting]If (LB >= UB) Thengoto step 8

3. I = LB

J = UB , PIVOT = A[LB] , 4. Repeat While (FLAG){I = I + 1

Repeat While ( A[I] < PIVOT AND I <= UB) ,I = I + 1Repeat While ( A[J] > PIVOT AND J > LB )

J = J – 1 , Sortin , If (I=> J) ThenFlag FALSEElse

{ , [Swap the elements],TEMP = A[I] , A[I] = A[J] , A[J] = TEMP ,},

}5. [Place PIVOT at its proper position]

A[IB] = A[J] , A[J] = PIVOT , 6. [Sort left segment]

CALL Quicksort (A, LB, J-1) , 7. [Sort right segment]

CALL Quicksort (A, J + 1 , UB)

8. End.

The above algorithm is used initially by the statement CALL Quicksort (A, 1, N) ,

Algorithm: postfix

Step 1 Add a right parenthesis ")" at the end of expression E.

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 = operand then Add the element to P [End of If Statement] , Step 4

If element = "(" [left parenthesis] then PUSH element on the STACK [End of If Statement]

Step 5 , If element = operator (say) then

(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.

(ii) Remove "(" [Do not add the "(" to P].

[End of If Statement] , [End of step 2 loop] , Step 7 , Exit

 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 .

Algorithm: , Step 1 , Reverse the given infix expression (E') , Step 2

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

If element = operator (say) then

(i) Repeatedly pop from STACK and add to P each operator which have the higher precedence than.

(ii) PUSH on the STACK. , [End of If Statement] , Step 6

If element = "(" [left parenthesis] then

(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 2 [Is the queue empty ?] If F = 0 then F:=1 [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

Procedure: DELETE (Q, F, R, X)

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

LINKED LIST OPERATIONS:

Algorithm: TRAVERS (INFO, PTR, FIRST)

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

Algorithm SEARCH (INFO, LINK, FIRST, FIND, LOC)

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]

Step 4 Set LOC:= NULL , Step 5 Exit

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

LINKED LIST INSERTION:

Algorithm: INSERT_FIRST (INFO, LINK, FIRST, AVAIL, NX)

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

LINKED LIST DELETION

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

RPTR (P) := NEW, Exit

DOUBLY LINK LIST DELETION:

Algorithm: Delete_double (FIRST, LAST, P)

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

You might also like