You are on page 1of 32

LAB TASK

ALGORITHMS & CODES


 Insertion Sort
 Tree Traversal
 Linked List(Singly)
 Stack (Only Algo)
 Recursion-Example
INSERTION
SORT
ALGORITHM
• Step 1 − If it is the first element, it is already sorted. return 1;
• Step 2 − Pick next element
• Step 3 − Compare with all elements in the sorted sub-list
• Step 4 − Shift all the elements in the sorted sub-list that is
greater than the value to be sorted
• Step 5 − Insert the value
• Step 6 − Repeat until list is sorted
Code
void insertionSort(int array[], int size) {
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
while (key < array[j] && j >= 0) {
array[j + 1] = array[j];
--j; }
array[j + 1] = key; }
}
TREE TRAVERSAL
Pre order
Root – Left – Right
Traversing
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE DATA
Step 3: PREORDER(TREE LEFT)
Step 4: PREORDER(TREE RIGHT) [END OF LOOP]
Step 5: End
Pre order:
This traversal first accesses the current node value then traverse the left and right subtrees
respectively.

Code:
void preorder( node *root)
{
if( root!=NULL) // Base Condition
{
cout<<root->key<<" ";
preorder(root->left);
preorder(root->right); //Recursive Function Call
}
}
In order Traversing
Left – Root– Right

Step 1: Repeat Steps 2 to 4 while TREE != NULL


Step 2: INORDER(TREE LEFT)
Step 3: Write TREE DATA
Step 4: INORDER(TREE RIGHT) [END OF LOOP]
Step 5: END
In order:
This traversal first goes over the left subtree of the root node, then accesses the current node,
followed by the right subtree of the current node.

Code:
void inorder(node *root)
{
if (root != NULL)
{
inorder(root->left);
cout<<root->key<<" ";
inorder(root->right);
}
}
Post order
Left – Right – Root
Traversing
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: POSTORDER(TREE LEFT)
Step 3: POSTORDER(TREE RIGHT)
Step 4: Write TREE DATA [END OF LOOP]
Step 5: END
Post order:
This traversal first goes to the left and right subtrees and then accesses the root value at last.

Code:
void postorder( node *root)
{
if( root!=NULL)
{
postorder(root->left);
postorder(root->right );
cout<<root->key;cout<<" ";
}
}
LINKED LIST
Insertion At Start
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
Insertion At Start
Code
struct node* inser_at_Start(node* head,string name,int rol){
node* n1=new node();
n1->name=name;
n1->roll=rol;
n1->next=head;
head=n1;
return head;
}
Insertion At Index
Algorithm
STEP 1: IF PTR = NULL
WRITE OVERFLOW
GOTO STEP 12
END OF IF
STEP 2: SET NEW_NODE = PTR
STEP 3: NEW_NODE → DATA = VAL
STEP 4: SET TEMP = HEAD
STEP 5: SET I = 0
STEP 6: REPEAT STEP 5 AND 6 UNTIL I<loc< li=""></loc<>
STEP 7: TEMP = TEMP → NEXT
STEP 8: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
END OF LOOP
STEP 9: PTR → NEXT = TEMP → NEXT
STEP 10: TEMP → NEXT = PTR
STEP 11: SET PTR = NEW_NODE
STEP 12: EXIT
Insertion At Index
Code
struct node* insert_at_index(node* head,int index,string name,int rol){
node* p1=new node();
node* p2=head;
int i=0;
while(i!=index-1) {
p2=p2->next:
i++; }
p1->name=name;
p1->roll=rol;
p1->next=p2->next;
p2->next=p1;
return head;
}
Insertion At End
Algorithm
Step 1: IF PTR = NULL Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR - > NEXT != NULL
Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
Step 9: SET PTR - > NEXT = NEW_NODE
Step 10: EXIT
Insertion At End
Code
struct node* insert_at_end(node* head,string name,int rol){
node* p1= new node();
node* p2=head;
p1->name=name;
p1->roll=rol;
p1->next=NULL;
if(head==NULL) {
head=p1;
return head;
}
while(p2!=NULL) {
p2=p2->next;
}
p2->next=p1;
return head;
}
Deletion At Start
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
Deletion At Start
Code
struct node* delete_at_Start(node* head){
node* p1=head;
head=p1->next;
delete p1;
return head;
}
Deletion At Index
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
STEP 2: SET TEMP = HEAD
STEP 3: SET I = 0
STEP 4: REPEAT STEP 5 TO 8 UNTIL I<loc< li=""></loc<>
STEP 5: TEMP1 = TEMP
STEP 6: TEMP = TEMP → NEXT
STEP 7: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
STEP 8: I = I+1
END OF LOOP
STEP 9: TEMP1 → NEXT = TEMP → NEXT
STEP 10: FREE TEMP
STEP 11: EXIT
Deletion At Index
Code
struct node * delete_at_index(node* head, int index){
node *p1 = head;
node *p2 = head->next;
for (int i = 0; i < index-1; i++) {
p1 = p1->next;
p2 = p2->next; }
p1->next = p2->next;
delete p2;
return head;
}
Deletion At End
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Deletion At End
Code
struct node* delete_at_end(node* head){
node *p1 = head;
node *p2 = head->next;
while(p2->next !=NULL) {
p1 = p1->next;
p2 = p2->next; }
p1->next = NULL;
delete p2;
return head;
}
STACK
PUSH OPERATION
Algorithm
The process of putting a new data element onto stack is known as a
Push Operation. Push operation involves a series of steps Algorithm
• Step 1 − Checks if the stack is full.  begin procedure push: stack, data
• Step 2 − If the stack is full, produces an error and exit.  if stack is full
 return null
• Step 3 − If the stack is not full, increments top to point next empty  endif
space.  top ← top + 1
 stack[top] ← data
• Step 4 − Adds data element to the stack location, where top is  end procedure
pointing.
• Step 5 − Returns success.
POP OPERATION
Algorithm
Accessing the element while removing it from the stack, is
known as a Pop Operation. In an array implementation of
pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack Algorithm
to point to the next value. But in linked-list  begin procedure pop: stack
implementation, pop() actually removes data element and  if stack is empty
deallocates memory space.  return null
Step 1 − Checks if the stack is empty.  endif
 data ← stack[top]
Step 2 − If the stack is empty, produces an error and  top ← top - 1
 return data
exit.  end procedure
Step 3 − If the stack is not empty, accesses the data
element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Evaluation of Postfix Expression using Stack
Algorithm:
Stack s;
while( not end of input ) {
e = get next element of input
if( e is an operand )
s.push( e );
else {
op2 = s.pop();
op1 = s.pop();
result = result of applying operator ‘e’ to op1 and op2;
s.push(result); } }
finalresult = s.pop();
RECURSION
RECURSIVE ALGORITHM
Initialize the algorithm. Recursive programs often need a seed value to start
with. This is accomplished either by using a parameter passed to the function or
by providing a gateway function that is non-recursive but that sets up the seed
values for the recursive calculation.
Check to see whether the current value(s) being processed match the base case.
If so, process and return the value.
Redefine the answer in terms of a smaller or simpler sub-problem or sub-
problems.
Run the algorithm on the sub-problem.Combine the results in the formulation of
the answer.
Return the results.
EXAMPLE
Factorial (n)
 Step 1: If n==1
Then return 1 //stopping condition (Base case)
Step 2: Else f= n* factorial (n-1)
Step 3: Return f
CODE
#include <iostream>
using namespace std;

int factorial(int n) {
if (n > 1) { OUTPUT
return n * factorial(n - 1); Enter a non-negative
number: 5
} int main() { Factorial of 5 = 120
--------------------------------
int a, result;
cout << "Enter a non-negative number: ";
cin >> a;
result = factorial(a);
cout << "Factorial of " << a << " = " << result; }

You might also like