0% found this document useful (0 votes)
10 views

Algs

The document describes algorithms for common operations on data structures like arrays, linked lists, stacks and queues. These include searching, sorting and traversing algorithms with their time and space complexities.

Uploaded by

Ms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Algs

The document describes algorithms for common operations on data structures like arrays, linked lists, stacks and queues. These include searching, sorting and traversing algorithms with their time and space complexities.

Uploaded by

Ms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

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

Time Complexity: O(n)


Auxiliary Space: O(1)
3.2
ALGORITHM Bubble_Sort( array[0..n-1])
// Input: List: unsorted List of ‘n’ elements
//Output: Sorted list

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)

ALGORITHM Selection_Sort( array[0..n-1])


// Input: List: unsorted List of ‘n’ elements
//Output: Sorted list

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]

Time Complexity: O(n2 )


Auxiliary Space: O(1)

ALGORITHM Insertion_Sort( array[0..n-1])


// Input: List: unsorted List of ‘n’ elements
//Output: Sorted list

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

Time Complexity: O(n2 )


Auxiliary Space: O(1)
4.1
ALGORITHM Binar_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

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)

Time Complexity: O(log(n))


Auxiliary Space: O(1)

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)

ALGORITHM merge( array[0..n-1],low,mid,high)


// Input: List: two sorted Lists.
//Output: : Sorted List of ‘n’ elements.
i=low
k=low
j=mid+1
while(i<=mid and j<=high):
if(array[i]<array[j]):
temp.insert(k,array[i])
i=i+1
k=k+1
else:
temp.insert(k,array[j])
j=j+1
k=k+1
while(i<=mid):
temp.insert(k,array[i])
k=k+1
i=i+1
while(j<=high):
temp.insert(k,array[j])
k=k+1
j=j+1
l=low
m=0
while(l<k):
array[l]=temp[m]
l=l+1
m=m+1

Time Complexity: O(n*log(n))


Auxiliary Space: O(n)

ALGORITHM partition( array[0..n-1],low,high)


// Input: List: List of ‘n’ elements with starting and ending index
//Output: divides the list into 2 parts

if(low<high):
pivot=divide(a,low,high)
quick_sort(a,low,pivot-1)
quick_sort(a,pivot+1,high)

ALGORITHM divide( array[0..n-1],low,high)


// Input: List: List of ‘n’ elements with starting and ending index
//Output: pivot element

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]

Time Complexity : O(n*log(n))


Auxiliary Space : O(n)

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)

Time Complexity : O(n)


Auxiliary Space : O(1)

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

Time Complexity : O(1)


Auxiliary Space : O(1)

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

Time Complexity : O(n)


Auxiliary Space : O(n)

Algorithm: search( key )


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 searching for a key (temp.data==key), until key found or
temp reaches to the last node
Time Complexity : O(n)
Auxiliary Space : O(n)

Algorithm: delete( key )


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 searching for a key (temp.data==key), until key found or
temp reaches to the last node
Step 4: if key is found delete that node by unlinking from the list

Time Complexity : O(n)


Auxiliary Space : O(n)
6.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

Time Complexity : O(1)


Auxiliary Space : O(1)

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

Time Complexity : O(n)


Auxiliary Space : O(n)

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

Time Complexity : O(1)


Auxiliary Space : O(1)

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

Time Complexity : O(n)


Auxiliary Space : O(n)

Algorithm: search( key )


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 searching for a key (temp.data==key), until key found or
temp reaches to the last node
Time Complexity : O(n)
Auxiliary Space : O(n)
Algorithm: delete( key )
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 searching for a key (temp.data==key), until key found or
temp reaches to the last node
Step 4: if key is found delete that node by unlinking from the list

Time Complexity : O(n)


Auxiliary Space : O(n)

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

Time Complexity : O(1)


Auxiliary Space : O(1)

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

Time Complexity : O(n)


Auxiliary Space : O(n)

Algorithm: search( key )


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 searching for a key (temp.data==key), until key found or
temp reaches to the last node
Time Complexity : O(n)
Auxiliary Space : O(n)

Algorithm: delete( key )


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 searching for a key (temp.data==key), until key found or
temp reaches to the last node
Step 4: if key is found delete that node by unlinking from the list

Time Complexity : O(n)


Auxiliary Space : O(n)

8.1 Algorithm: push( data )


Step 1: insert element to end of list using append function
Step 2: return

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)

8.2 Algorithm: bracket_matching( expr )


1. Declare a character stack S.
2. Now traverse the expression string exp.
a)If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack.
b)If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop from stack and if
the popped character is the matching starting bracket then continue else brackets are not
balanced.
3. After complete traversal, if there is some starting bracket left in stack then given expression is
“not balanced” otherwise given expression is balanced

Time Complexity: O(1)


Auxiliary Space: O(1)

9.1 Algorithm Fibonacci ( n )


9.2
Algorithm hanoi(disk,source,dest,temp)
if disk==1 then
Move disk from source to dest
else
Hanoi(disk-1,source,temp,destination)
Move disk from source to dest

Hanoi(disk-1,temp,destination,source)

Time Complexity: O(2n)


Auxiliary Space: O(n)

10.1 Algorithm: enqueue( data )


Step 1: insert element to end of list using append function
Step 2: return

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)

10.2 Algorithm: enqueue( data )


Step 1: insert element to end of list using append function
Step 2: return

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

Time Complexity: O(n)


Auxiliary Space: O(n)

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.

Time Complexity: O(n)

Auxiliary Space: O(1).

You might also like