0% found this document useful (0 votes)
10 views4 pages

Count

Uploaded by

Windy Joseph
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)
10 views4 pages

Count

Uploaded by

Windy Joseph
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/ 4

Windy Joseph

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

1. Initialize Count Array:


Count=[0,0,0,0,0,0]\text{Count} = [0, 0, 0, 0, 0, 0]Count=[0,0,0,0,0,0]
2. Count Comparison Step:

For i=0i = 0i=0 (60):

Compare with 35: 60 > 35, so increment Count[0]

Compare with 81: 60 < 81, so increment Count[2]

Compare with 98: 60 < 98, so increment Count[3]


Compare with 14: 60 > 14, so increment Count[0]

Compare with 47: 60 > 47, so increment Count[0]

Count=[3,0,0,0,0,0]\text{Count} = [3, 0, 0, 0, 0, 0]Count=[3,0,0,0,0,0]

For i=1i = 1i=1 (35):

Compare with 81: 35 < 81, so increment Count[2]

Compare with 98: 35 < 98, so increment Count[3]

Compare with 14: 35 > 14, so increment Count[1]

Compare with 47: 35 < 47, so increment Count[5]

Count=[3,1,1,1,0,1]\text{Count} = [3, 1, 1, 1, 0,
1]Count=[3,1,1,1,0,1]

For i=2i = 2i=2 (81):

Compare with 98: 81 < 98, so increment Count[3]

Compare with 14: 81 > 14, so increment Count[2]

Compare with 47: 81 > 47, so increment Count[2]

Count=[3,1,3,2,0,1]\text{Count} = [3, 1, 3, 2, 0,
1]Count=[3,1,3,2,0,1]

For i=3i = 3i=3 (98):

Compare with 14: 98 > 14, so increment Count[3]

Compare with 47: 98 > 47, so increment Count[3]

Count=[3,1,3,4,0,1]\text{Count} = [3, 1, 3, 4, 0,
1]Count=[3,1,3,4,0,1]

For i=4i = 4i=4 (14):

Compare with 47: 14 < 47, so increment Count[5]

Count=[3,1,3,4,0,2]\text{Count} = [3, 1, 3, 4, 0,
2]Count=[3,1,3,4,0,2]

For i=5i = 5i=5 (47):

(No comparisons needed since jjj starts after iii)

Count=[3,1,3,4,0,2]\text{Count} = [3, 1, 3, 4, 0,
2]Count=[3,1,3,4,0,2]

3. Construct the Sorted Array SSS using Count:


Place 60 at position Count[0] (index 3): S[3]=60S[3] = 60S[3]=60

Place 35 at position Count[1] (index 1): S[1]=35S[1] = 35S[1]=35

Place 81 at position Count[2] (index 3): S[3]=81S[3] = 81S[3]=81 (shifts


60 down to next position)

Place 98 at position Count[3] (index 4): S[4]=98S[4] = 98S[4]=98

Place 14 at position Count[4] (index 0): S[0]=14S[0] = 14S[0]=14

Place 47 at position Count[5] (index 2): S[2]=47S[2] = 47S[2]=47

Final Sorted Array:


S=[14,35,47,60,81,98]S = [14, 35, 47, 60, 81, 98]S=[14,35,47,60,81,98]

b. Is this Algorithm Stable?

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.

c. Is this Algorithm In-Place?

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.)

You might also like