0% found this document useful (0 votes)
28 views9 pages

5 Question Assignment

The document discusses differences between stack and heap memory in computer science. The stack has LIFO allocation of fixed size and faster access, while the heap has dynamic allocation of unlimited size but slower access and requires manual memory management.

Uploaded by

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

5 Question Assignment

The document discusses differences between stack and heap memory in computer science. The stack has LIFO allocation of fixed size and faster access, while the heap has dynamic allocation of unlimited size but slower access and requires manual memory management.

Uploaded by

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

UNIVERSITY OF ENGINEERING AND TECHNOLOGY,

LAHORE
(NAROWAL CAMPUS)

NAME:
ASIM SHAHZAD
REG-NO. :
2022-CS-547 (A)
SUBMITTED TO:
Dr. Muhammad Idress
SUBJECT:
Data Structure and Algorithm
Implementation of priority Queue and its types?

Code for Priority Queue:


#include <iostream>
#include <queue>
using namespace std;
// Function to insert an element into the priority queue
void insertElement(priority_queue<int>& pq) {
int value;
cout << "Enter the element to insert: ";
cin >> value;
pq.push(value);
cout << "Element " << value << " inserted." << endl;
}
void removeTopElement(priority_queue<int>& pq) {
if (!pq.empty()) {
int top = pq.top(); // Get the top element before popping
pq.pop();
cout << "Top element " << top << " removed." << std::endl;
} else {
cout << "Queue is empty. Cannot remove." << std::endl;
}
}
void getTopElement(const priority_queue<int>& pq) {
if (!pq.empty()) {
int top = pq.top();
cout << "Top element: " << top << std::endl;
} else {
cout << "Queue is empty." << std::endl;
}
}
void checkIfQueueEmpty(const priority_queue<int>& pq) {
if (pq.empty()) {
cout << "Queue is empty." << endl;
} else {
cout << "Queue is not empty." << endl;
}
}
void getSizeOfQueue(const priority_queue<int>& pq) {
cout << "Size of the queue: " << pq.size() << endl;
}

int main() {
priority_queue<int> pq;
int choice;
do {
cout << "Priority Queue Operations:" << endl;
cout << "1. Insert Element" << endl;
cout << "2. Remove Top Element" << endl;
cout << "3. Get Top Element" << endl;
cout << "4. Check if Queue is Empty" << endl;
cout << "5. Get the Size of Queue" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
insertElement(pq);
break;
case 2:
removeTopElement(pq);
break;
case 3:
getTopElement(pq);
break;
case 4:
checkIfQueueEmpty(pq);
break;
case 5:
getSizeOfQueue(pq);
break;
case 6:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
} while (choice != 6);
return 0;
}
Types of Priority Queue:
Min Heap:
Smallest element is at the root.
Used for ascending order processing (e.g., finding minimum values).
The highest-priority element has the smallest value.
insert and extractMin operations.
Examples: Dijkstra's algorithm, Prim's algorithm.

Max Heap:
Largest element is at the root.
Used for descending order processing (e.g., finding maximum values).
The highest-priority element has the largest value.
insert and extractMax operations.
Examples: Heap sort, job scheduling with maximum priorities.

What are the differences between Recursion and


Iteration explain with examples?
Recursion Iteration
1. Function call itself 1. A set of instructions repeatedly
2. Recursion are applied on function. executed.
3. Through the base case , where there 2. Iteration are applied on loops.
will be no function call. 3. Used when time complexity needs
4. Used when code size needs to be to be balanced against on
small , and time complexity is not expended code size.
issue. 4. Longer code size.
5. Smaller code size. 5. Relative lower time complexity.
1. 6.Very high time complexity. 6. Normally,it is faster than recursion.
6. Execution is slow. 7. Iteration use less memory as
7. Recursion use more memory as compared to Recursion.
compared to iteration.
Recursion:
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int result = factorial(5); // Calculates 5!
cout << result << endl; // Output: 120
return 0;
}

Iteration:
#include <iostream>
using namespace std;
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
int main() {
int result = factorial(5); // Calculates 5!
cout << result << endl; // Output: 120
return 0;}
What are the differences between Time complexity and
Space complexity explain with examples?
Time complexity and space complexity are fundamental concepts in computer science and
algorithm analysis, and they apply to algorithms implemented in various programming
languages, including C++.

Time complexity:
Time complexity measures the amount of time an algorithm takes to execute as a function of the
input size. It helps us understand how the algorithm's runtime grows as the input size increases.
Time complexity is typically expressed using Big O notation.
Just like, Time Complexity of accessing array elements in the worst case is O(1).

Time complexity perimeters:


Common time complexities include:
O(1) or constant time: The algorithm's runtime is not dependent on the input size; it takes a
fixed amount of time to complete.
O(log n) or logarithmic time: The runtime grows slowly as the input size increases. Examples
include binary search and efficient balanced tree operations.
O(n) or linear time: The runtime increases linearly with the input size. Simple linear search and
iteration fall into this category.
O(n log n) or linearithmic time: Slightly slower than linear time. Many efficient sorting
algorithms like merge sort and quicksort belong to this category.
O(n^2) or quadratic time: The runtime grows quadratically with the input size. Inefficient
sorting algorithms like bubble sort and some nested loops fall into this category.
O(2^n) or exponential time: The runtime grows exponentially with the input size. Recursive
algorithms without proper pruning often lead to exponential time.
O(n!) or factorial time: The worst-case runtime grows as a factorial of the input size. Extremely
inefficient algorithms like brute-force permutation generation fall into this category.

Space complexity:
Space complexity measures the amount of memory an algorithm uses as a function of the input
size. It helps us understand how the memory usage of an algorithm scales with input size. Like
time complexity, space complexity is typically expressed using Big O notation.
Just like, The space complexity of the array is O(n).
Space complexity perimeters:
Common space complexities include:
O(1) or constant space: The algorithm uses a fixed amount of memory regardless of the input
size.
O(log n) or logarithmic space: The memory usage grows logarithmically with the input size.
O(n) or linear space: The memory usage increases linearly with the input size.
O(n log n) or linearithmic space: The memory usage grows linearly with a logarithmic factor
relative to the input size.
O(n^2) or quadratic space: The memory usage grows quadratically with the input size.
O(2^n) or exponential space: The memory usage grows exponentially with the input size.

What are the differences between Stack and Heep ?

In computer science, the terms "stack" and "heap" refer to two different types of memory
storage used for various purposes. Here are the key differences between them:

Memory Allocation:
Stack:
The stack is a region of memory that is used for the storage of function call frames and local
variables. Memory for items on the stack is allocated and deallocated in a last-in, first-out (LIFO)
manner. When a function is called, a new stack frame is pushed onto the stack, and when the
function returns, its frame is popped off the stack, releasing the associated memory.

Heap:
The heap is a region of memory used for dynamic memory allocation. It's more flexible than the
stack as memory can be allocated and deallocated in any order. In languages like C and C++,
you use functions like malloc() and free() to allocate and deallocate memory on the heap.

Size Limitations:
Stack:
The stack typically has a fixed and limited size. It's designed for relatively small amounts of data
and function call management. Trying to allocate too much memory on the stack can result in a
stack overflow.
Heap:
The heap, in contrast, can be much larger and is only limited by the total available memory of
the system. This makes it suitable for managing large or dynamically-sized data structures.

Access Speed:
Stack:
Accessing data on the stack is generally faster than accessing data on the heap because it
involves simple pointer manipulation.

Heap:
Accessing data on the heap involves following a pointer to the allocated memory location, which
can be slower than stack-based access.

Memory Management:
Stack:
Memory on the stack is managed automatically by the program's runtime system. You don't
need to explicitly allocate or deallocate memory on the stack.

Heap:
Memory on the heap must be explicitly allocated and deallocated by the programmer, which can
be error-prone if not done correctly.

You might also like