Count
Count
Page 17 #4:
function solve_quadratic(a, b, c)
if a == 0
if b == 0
if c == 0
print "Infinite solutions"
else
print "No solution"
else
root = -c / b
print "Single real root:", root
else
discriminant = b^2 - 4*a*c
if discriminant > 0
root1 = (-b + sqrt(discriminant)) / (2*a)
root2 = (-b - sqrt(discriminant)) / (2*a)
print "Two real roots:", root1, root2
else if discriminant == 0
root = -b / (2*a)
print "Single real root:", root
else
real_part = -b / (2*a)
imaginary_part = sqrt(-discriminant) / (2*a)
print "Complex roots:", real_part + imaginary_part * i, real_part -
imaginary_part * i
input coefficients a, b, c
solve_quadratic(a, b, c)
Page 23 #1
a. Apply the Algorithm to Sorting the List [60, 35, 81, 98, 14, 47]
Initial Array:
A=[60,35,81,98,14,47]A = [60, 35, 81, 98, 14, 47]A=[60,35,81,98,14,47]
Length of Array n=6n = 6n=6
Count=[3,1,1,1,0,1]\text{Count} = [3, 1, 1, 1, 0,
1]Count=[3,1,1,1,0,1]
Count=[3,1,3,2,0,1]\text{Count} = [3, 1, 3, 2, 0,
1]Count=[3,1,3,2,0,1]
Count=[3,1,3,4,0,1]\text{Count} = [3, 1, 3, 4, 0,
1]Count=[3,1,3,4,0,1]
Count=[3,1,3,4,0,2]\text{Count} = [3, 1, 3, 4, 0,
2]Count=[3,1,3,4,0,2]
Count=[3,1,3,4,0,2]\text{Count} = [3, 1, 3, 4, 0,
2]Count=[3,1,3,4,0,2]
In this case, the algorithm is not stable because when two elements are equal, the
later element may be placed before the earlier element, disrupting the original order.
This algorithm is not in-place because it requires an extra array (S) to store the
sorted output, meaning it uses additional memory proportional to the size of the input
array.
Page 37 #3
a. Stack Operations
Let's go through the stack operations step by step. A stack follows the
Last-In-First-Out (LIFO) principle.
1. Initial Stack:
Stack=[]\text{Stack} = []Stack=[]
(The stack is empty.)
2. Operation: push(a)
Stack=[a]\text{Stack} = [a]Stack=[a]
(Element a is pushed onto the stack.)
3. Operation: push(b)
Stack=[a,b]\text{Stack} = [a, b]Stack=[a,b]
(Element b is pushed onto the stack.)
4. Operation: pop
Stack=[a]\text{Stack} = [a]Stack=[a]
(The top element b is popped off the stack.)
5. Operation: push(c)
Stack=[a,c]\text{Stack} = [a, c]Stack=[a,c]
(Element c is pushed onto the stack.)
6. Operation: push(d)
Stack=[a,c,d]\text{Stack} = [a, c, d]Stack=[a,c,d]
(Element d is pushed onto the stack.)
7. Operation: pop
Stack=[a,c]\text{Stack} = [a, c]Stack=[a,c]
(The top element d is popped off the stack.)
b. Queue Operations
Let's go through the queue operations step by step. A queue follows the
First-In-First-Out (FIFO) principle.
1. Initial Queue:
Queue=[]\text{Queue} = []Queue=[]
(The queue is empty.)
2. Operation: enqueue(a)
Queue=[a]\text{Queue} = [a]Queue=[a]
(Element a is enqueued at the back of the queue.)
3. Operation: enqueue(b)
Queue=[a,b]\text{Queue} = [a, b]Queue=[a,b]
(Element b is enqueued at the back of the queue.)
4. Operation: dequeue
Queue=[b]\text{Queue} = [b]Queue=[b]
(The front element a is dequeued.)
5. Operation: enqueue(c)
Queue=[b,c]\text{Queue} = [b, c]Queue=[b,c]
(Element c is enqueued at the back of the queue.)
6. Operation: enqueue(d)
Queue=[b,c,d]\text{Queue} = [b, c, d]Queue=[b,c,d]
(Element d is enqueued at the back of the queue.)
7. Operation: dequeue
Queue=[c,d]\text{Queue} = [c, d]Queue=[c,d]
(The front element b is dequeued.)