Algs
Algs
2
Algorithm: insert( data )
Step 1: insert element to ADT using append function
Step 2: return
Algorithm: pop( )
Step 1: Check whether ADT is Empty
Step 2 If it is Empty then, display ADT is Empty!!!' and terminate the function.
Step 3: If ADT is Not Empty then, delete element by using pop function and return
Algorithm: display( )
Step 1: Check whether ADT is Empty
Step 2 If it is Empty then, display ADT is Empty!!!' and terminate the function.
Step 3: If ADT is Not Empty then, display the contents of a ADT
Time Complexity: O(1) for insert O(n) for display and delete
Auxiliary Space: O(1)
2.1
Step 1: create a student class
Step 2: create __init__ function to read register number , name, and marks of student
Step 3: create a display function to print student register number , name, and marks of student
Step 4:
Algorithm: pop( )
Step 1: Check whether ADT is Empty
Step 2 If it is Empty then, display ADT is Empty!!!' and terminate the function.
Step 3: If ADT is Not Empty then, delete element by using pop function and return
Algorithm: display( )
Step 1: Check whether ADT is Empty
Step 2 If it is Empty then, display ADT is Empty!!!' and terminate the function.
Step 3: If ADT is Not Empty then, display the contents of a ADT
Time Complexity: O(1) for insert O(n) for display and delete
Auxiliary Space: O(1)
3.1
ALGORITHM Linear_Search( array[0..n-1],key)
// Input: List: List of ‘n’ elements and key value
//Output: Returns Boolean value True if target is found in List otherwise it will return False
for i in range(n):
if list[i] == key:
return True
return False
for i in range(n):
for j in range(n-1-i):
if (array[j]>array[j+1]):
swap array[j] & array[j+1]
2
Time Complexity: O(n )
Auxiliary Space: O(1)
for i in range(n-1):
min=i
for j in range(i+1,n):
if (array[min]>array[j]):
min=j
swap array[i] & array[min]
for i in range(1,n):
temp=array[i]
j=i-1
while j>0 and array[j]>temp:
array[j+1]=array[j]
j=j-1
array[j+1]=temp
if(low<=high):
mid=(low+high)//2
if key is array[mid]:
return mid
elif key<array[mid]:
return binary_search(array,low,mid-1,key)
else:
return binary_search(array,mid+1,high,key)
4.2
ALGORITHM divide( array[0..n-1],low,high)
// Input: List: List of ‘n’ elements with starting and ending index
//Output: divides the list into 2 parts
def divide(array,low,high):
if(low<high):
mid=(low+high)//2
divide(array,low,mid)
divide(array,mid+1,high)
merge(array,low,mid,high)
if(low<high):
pivot=divide(a,low,high)
quick_sort(a,low,pivot-1)
quick_sort(a,pivot+1,high)
pivot=a[low]
i=low+1
j=high
while 1:
while(i<high)and(pivot>=a[i]):
i=i+1
while(pivot<a[j]):
j=j-1
if i<j:
swap a[i] & a[j]
else:
swap a[low] & a[j]
ALGORITHM fibonacci( n)
// Input: positive integer n
//Output: Fibonacci series of n
def fib(n):
if (n==0):
return 0
if (n==1):
return 1
else:
return fibonacci (n-1)+ fibonacci (n-2)
5.1
Algorithm: prepending a node to the linked list: (new_node)
Step 1: Check whether list is Empty ( IF head is None )
Step 2: If list is Empty then, set head=new_node
Step 3: If list is Not Empty then, set new_node.next=head & head=new_node
Algorithm: display( )
Step 1: Check whether list is Empty ( IF head is None )
Step 2 If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3: If list is Not Empty then, Keep displaying temp.data, until temp reaches to the last node
Algorithm: display( )
Step 1: Check whether list is Empty ( IF head is None )
Step 2 If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3: If list is Not Empty then, Keep displaying temp.data, until temp reaches to the last node
7.1
Algorithm: appending a node to the linked list: (new_node)
Step 1: Check whether list is Empty ( IF head is None )
Step 2: If list is Empty then, set head=new_node & tail=new_node
Step 3: If list is Not Empty then, set new_node as tail
Algorithm: display( )
Step 1: Check whether list is Empty ( IF head is None )
Step 2 If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3: If list is Not Empty then, Keep displaying temp.data, until temp reaches to the last node
7.2
Algorithm: appending a node to the linked list: (new_node)
Step 1: Check whether list is Empty ( IF head is None )
Step 2: If list is Empty then, set head=new_node & tail=new_node
Step 3: If list is Not Empty then, set new_node as tail
Algorithm: display( )
Step 1: Check whether list is Empty ( IF head is None )
Step 2 If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3: If list is Not Empty then, Keep displaying temp.data, until temp reaches to the last node
Algorithm: pop( )
Step 1: Check whether stack is Empty
Step 2 If it is Empty then, display stack is Empty!!!' and terminate the function.
Step 3: If stack is Not Empty then, delete element by using pop function and return
Algorithm: display( )
Step 1: Check whether stack is Empty
Step 2 If it is Empty then, display stack is Empty!!!' and terminate the function.
Step 3: If stack is Not Empty then, display the contents of a list
Time Complexity: O(1) for insert and delete O(n) for display
Auxiliary Space: O(1)
Hanoi(disk-1,temp,destination,source)
Algorithm: dequeue( )
Step 1: Check whether queue is Empty
Step 2 If it is Empty then, display queue is Empty!!!' and terminate the function.
Step 3: If queue is Not Empty then, delete element at a front by using pop(0) function and return
Algorithm: display( )
Step 1: Check whether queue is Empty
Step 2 If it is Empty then, display queue is Empty!!!' and terminate the function.
Step 3: If queue is Not Empty then, display the contents of a queue
Time Complexity: O(1) for insert and delete O(n) for display
Auxiliary Space: O(1)
Algorithm: dequeue( )
Step 1: Check whether queue is Empty
Step 2 If it is Empty then, display queue is Empty!!!' and terminate the function.
Step 3: If queue is Not Empty then, find the highest priority element and delete that element using pop()
function and return
Algorithm: display( )
Step 1: Check whether queue is Empty
Step 2 If it is Empty then, display queue is Empty!!!' and terminate the function.
Step 3: If queue is Not Empty then, display the contents of a queue
Time Complexity: O(1) for insert O(n) for display and delete
Auxiliary Space: O(1)
11
Algorithm: insert a node to the binary serach tree: (new_node)
Step 1: Check whether bst is Empty ( IF root is None )
Step 2: If tree is Empty then, set root=new_node
Step 3: If list is Not Empty then,
a) If the given value is lesser than the root’s, insert the new node to the left subtree.
b) If the given value is greater than the root’s, insert the new node to the right subtree
12.1
Algorithm: For each node, first, the node is visited and then it’s child nodes are put in a FIFO queue.
bfs(tree)
1) Create an empty queue q
2) temp = root /*start from root*/
3) Loop while temp is not NULL
a) print temp.data.
b) Enqueue temp node’s children
(first left then right children) to q
c) Dequeue a node from q.
Time Complexity: O(n) where n is the number of nodes in the binary tree.
Auxiliary Space: O(n) where n is the number of nodes in the binary tree.
12.2
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.