0% found this document useful (0 votes)
4 views5 pages

Assignments 3

The document outlines various algorithms and data structures, including their complexities. It covers sorting of functions, implementations of queue and stack using both array and linked list representations, and their respective enqueue, dequeue, push, and pop operations. Each operation is accompanied by its time complexity, demonstrating efficient handling of data structures.

Uploaded by

merikatori02
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)
4 views5 pages

Assignments 3

The document outlines various algorithms and data structures, including their complexities. It covers sorting of functions, implementations of queue and stack using both array and linked list representations, and their respective enqueue, dequeue, push, and pop operations. Each operation is accompanied by its time complexity, demonstrating efficient handling of data structures.

Uploaded by

merikatori02
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/ 5

Exercise 1 :

The following order of functions after sorting in the ascending order :


210 < 3n + 100logn < 4n < nlogn < 4nlogn + 2n < n2+10n < n3 < 2logn < 2n
Exercise 2 :
1st Algorithm:
Function Calculate_2n(n:integer)
result = 1
for i from 1 to n do
result = result * 2
return result
end Function
Complexity : O(n)
2nd Algorithm:
Function Calculate_2n(n:integer)
result = 1
a = 2
WHILE n differs from 0
IF n is not divisible by 2
result = result * a
end IF
n = n / 2
a = a * 2
return result
end Function
Complexity : O(logn)
Exercise 3 :
CLASS Queue{
Q: Array of integers
front: integer
rear: interger
}
1. Enqueue
Function Enqueue(queue: Queue, data: integer)
IF queue.rear >= queue.Q.size - 1
PRINT(“Queue overflow”)
ELSE
queue.rear = queue.rear + 1
queue.Q[queue.rear] = data
end IF
end Function
Complexity : O(1)
2. Dequeue
Function Dequeue(queue: Queue)
queue.front = queue.front + 1
IF queue.front > queue.rear
queue.front = 0
queue.rear = -1
end IF
end Function
Complexity : O(1)
Exercise 4 :
CLASS Node{
data: integer
next: Node*
}
CLASS Queue{
front: Node*
rear: Node*
}
1. Enqueue
Function Enqueue(queue: Node*, data: integer)
new = new Node()
new.data = data
new.next = nil
IF queue is nil
queue.front = new
queue.rear = new
ELSE
queue.rear.next = new
queue.rear = new
end IF
end Function
Complexity : O(1)
2. Dequeue
Function Dequeue(queue: Node*)
IF queue.front = queue.rear
queue.front = nil
queue.rear = nil
ELSE
temp: Node*
temp = queue.front
queue.front = queue.front.next
delete temp
end IF
end Function
Complexity : O(1)
Exercise 5 :
CLASS Stack{
S: Array of integers
top: Integer
}
1. Push
Function Push(stack: Stack, data: integer)
IF stack.top >= stack.S.size – 1
Print(“Stack overflow”)
ELSE
stack.top = stack.top + 1
stack.S[stack.top] = data
end IF
end Function
Complexity : O(1)
2. Pop
Function Pop(stack: Stack)
stack.top = stack.top - 1
end Function
Complexity : O(1)
Exercise 6 :
CLASS Node{
data: integer
previous: Node*
}
CLASS Stack{
top: Node*
}
1. Push
Function Push(stack: Node*, data: integer)
new = new Node()
new.data = data
new.previous = nil
IF stack.top = nil
stack.top = new
ELSE
new.previous = stack.top
stack.top = new
end IF
end Function
Complexity : O(1)
2. Pop
Function Pop(stack: Node*)
IF stack.top.previous = nil
stack.top = nil
ELSE
temp: Node*
temp = stack.top
stack.top = stack.top.previous
delete temp
end IF
end Function
Complexity : O(1)

You might also like